How to transform labels in pytorch to onehot
How to give target_transform a function for changing the labels to onehot encoding?
For example, the MNIST dataset in torchvision:
train_dataset = torchvision.datasets.MNIST(root='./mnist_data/', 
                                           train=True,
                                           download=True,
                                           transform=train_transform,
                                           target_transform=&lt;????&gt;)

Tried F.onehot() but it didn't work.

This is how I implemented it. Not sure if there's a cleaner way.
train_dataset = torchvision.datasets.MNIST(root='./data/', train=True,
                                 transform=torchvision.transforms.ToTensor(),
                                 target_transform=torchvision.transforms.Compose([
                                 lambda x:torch.LongTensor([x]), # or just torch.tensor
                                 lambda x:F.one_hot(x,10)]),
                                 download=True)


It needs to be an index tensor? i.e. int64

Can't use torchvision.ToTensor because it's not an image

Also torch.LongTensor and torch.tensor behave differently with int input

Need to provide number of classes


