julliet
julliet

Reputation: 167

How do I one hot encode along a specific dimension using PyTorch?

I have a tensor of size [3, 15, 136], where:

I want to one-hot my tensor using the probabilities in the tokens dimension (136). To do so I want to extract the tokens dimension for each letter in sequence length and put 1 to the largest possibility and mark all other tokens as 0.

Upvotes: 3

Views: 2413

Answers (1)

iacob
iacob

Reputation: 24291

You can use PyTorch's one_hot function to achieve this:

import torch.nn.functional as F

t = torch.rand(3, 15, 136)

F.one_hot(t.argmax(dim=2), 136)

Upvotes: 5

Related Questions