Premium problem38. One-Hot Encoding

Medium Locked

Convert the 1-D integer tensor labels into a one-hot encoded matrix with num_classes columns: row i is all zeros except a 1 at column labels[i].

Input

labels = tensor([0, 2, 1, 3])
num_classes = 4

Output

tensor([[1, 0, 0, 0],
        [0, 0, 1, 0],
        [0, 1, 0, 0],
        [0, 0, 0, 1]])

Premium problem

This one's part of Premium. Unlock the full PyTorch track plus every other premium problem on the site.

Implement solve(...)