IniziaInizia gratis

Creating one-hot encoded labels

One-hot encoding converts a single integer label into a vector with N elements, where N is the number of classes. This vector contains zeros and a one at the correct position.

In this exercise, you'll manually create a one-hot encoded vector for y, and then use PyTorch to simplify the process. Your dataset has three classes (0, 1, 2).

numpy (np), torch.nn.functional (F), and torch are already imported for you.

Questo esercizio fa parte del corso

Introduction to Deep Learning with PyTorch

Visualizza il corso

Istruzioni dell'esercizio

  • Manually one-hot encode the ground truth label y using the provided NumPy array and save it as one_hot_numpy.
  • Use PyTorch to one-hot encode y and save it as one_hot_pytorch.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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 = F.____(torch.tensor(y), num_classes=____)

print("One-hot vector using NumPy:", one_hot_numpy)
print("One-hot vector using PyTorch:", one_hot_pytorch)
Modifica ed esegui il codice