LoginSignup
21
10

More than 3 years have passed since last update.

PyTorchでone-hot encoding

Last updated at Posted at 2020-03-07

公式doc

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]])

戻すには、以下。

torch.argmax(one_hot, dim=1)
# output:
# tensor([2, 0, 1])

なぜか検索に引っ掛かりにくいので・・・。

21
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
10