KFrank:

This is a sufficiently commonly requested manipulation that
I’m surprised that there isn’t a built-in function for it – at
least I haven’t found one.


F.one_hot should yield the same result as the scatter operation  :
x = torch.tensor([1, 2, 0, 2, 2, 2, 2, 1])
F.one_hot(x, num_classes=4)