10

I have a byte tensor of integer class labels, e.g. from the MNIST data set.

 1
 7
 5
[torch.ByteTensor of size 3]

How do use it to create a tensor of 1-hot vectors?

 1  0  0  0  0  0  0  0  0  0
 0  0  0  0  0  0  1  0  0  0
 0  0  0  0  1  0  0  0  0  0
[torch.DoubleTensor of size 3x10]

I know I could do this with a loop, but I'm wondering if there's any clever Torch indexing that will get it for me in a single line.

W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111

2 Answers2

15
indices = torch.LongTensor{1,7,5}:view(-1,1)
one_hot = torch.zeros(3, 10)
one_hot:scatter(2, indices, 1)

You can find the documentation for scatter in the torch/torch7 github readme (in the master branch).

stites
  • 4,903
  • 5
  • 32
  • 43
smhx
  • 2,246
  • 18
  • 22
3

An alternate method is to shuffle rows from an identity matrix:

indicies = torch.LongTensor{1,7,5}
one_hot = torch.eye(10):index(1, indicies)

This was not my idea, I found it in karpathy/char-rnn.

Tarquinnn
  • 501
  • 3
  • 8