Mask tensors from indexes (e.g from multinomial sampling)
Hi, I am looking for a ‘good’ way to transform matrix of indexes to a onehot matrix (line by line). For example,
ludc
<SCODE>>>> x = torch.LongTensor([[1, 3], [2, 4]])
>>> torch.zeros(2, 5).scatter_(1, x, 1)

 0  1  0  1  0
 0  0  1  0  1
[torch.FloatTensor of size 2x5]
<ECODE>
apaszke
1112
You could use this code snippet to get all indices of the value you are comparing to: <SCODE>x = torch.LongTensor([[1, 3], [2, 4]])
res = torch.zeros(2, 5).scatter_(1, x, 1)
(res == 1.).nonzero()
<ECODE>
ptrblck