IndexError: tensors used as indices must be long, byte or bool tensors
Solution 1
Yes it works when I provide a dtype=long to the label tensor and leaving rest of the tensors with default dtypes. thanks!
Solution 2
The previous answer somehow does not work for me in recent versions of pytorch. But I have solved it by passing the indices as long -> i.long()
,lab.long()
So, this should work,
NumClass = 10
mask = torch.zeros(batch_size, self.mem_dim, 4, 4)
ones = torch.ones(1, 4, 4)
NumRows = self.mem_dim
Elements = NumRows//NumClass
for i in range(batch_size):
lab = torch.arange(Elements * label[i], Elements*(label[i]+1), 1)
mask[i.long(),lab.long()] = ones
There is a catch, As I don't know the exact error message, I have no way to be sure, if one or both the indices need to be converted into long.
Related videos on Youtube
feed_me_pi
Updated on June 04, 2022Comments
-
feed_me_pi almost 2 years
I am getting this error only during the testing phase, but I do not face any problem in the training and validation phase.
IndexError: tensors used as indices must be long, byte or bool tensors
I get this error for the last line in the given code snippet.
The code snippet looks like the one below,
NumClass = 10 mask = torch.zeros(batch_size, self.mem_dim, 4, 4) ones = torch.ones(1, 4, 4) NumRows = self.mem_dim Elements = NumRows//NumClass for i in range(batch_size): lab = torch.arange(Elements * label[i], Elements*(label[i]+1), 1) mask[i,lab] = ones
The "lab" is a tensor value and prints out the range in such a way,
tensor([6, 7, 8]) tensor([ 9, 10, 11]) tensor([21, 22, 23])
(Note*: the length of this lab tensor can be of length 'n' based on the value of ElementsPerClass)
-
louisD over 3 yearsDid you try to set the type of the tensor lab as torch.float64 ? (edited after Yunnosch request, although you already read and confirmed this suggestion)
-