PyTorch: one-hot
PyTorch: one-hot
encoding
code:py
one_hot = torch.nn.functional.one_hot(torch.tensor(2, 0, 1), num_classes=4)
one_hot
# output:
# tensor([0, 0, 1, 0,
# 1, 0, 0, 0,
# 0, 1, 0, 0])
decoding
code:py
torch.argmax(one_hot, dim=1)
# output:
# tensor(2, 0, 1)
https://qiita.com/whale9490/items/ade0c201b9d51846dadf
https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.one_hot
Qiita.icon