Creating one-hot encoded labels
One-hot encoding is a technique that turns a single integer label into a vector of N elements, where N is the number of classes in your dataset. This vector only contains zeros and ones. In this exercise, you'll create the one-hot encoded vector of the label y
provided.
You'll practice doing this manually, and then make your life easier by leveraging the help of PyTorch! Your dataset contains three classes, and the class labels range from 0 to 2 (e.g., 0, 1, 2).
NumPy is already imported as np
, and torch.nn.functional
as F
. The torch
package is also imported.
This is a part of the course
“Introduction to Deep Learning with PyTorch”
Exercise instructions
- Manually create a one-hot encoded vector of the ground truth label
y
by filling in the NumPy array provided. - Create a one-hot encoded vector of the ground truth label
y
using PyTorch.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
y = 1
num_classes = 3
# Create the one-hot encoded vector using NumPy
one_hot_numpy = np.array([____, ____, ____])
# Create the one-hot encoded vector using PyTorch
one_hot_pytorch = ____