Indexing error - object of type Variable - torch.cuda.ByteTensor

I am trying to index a tensor in the following line:

neg_dists = distances[0, max_pos_distances_idx[0], 1:]

I get the following error:

TypeError: Performing basic indexing on a tensor and encountered an error indexing dim 1 with an object of type Variable. The only supported types are integers, slices, numpy scalars, or if indexing with a torch.cuda.LongTensor or torch.cuda.ByteTensor only a single Tensor may be passed.

The error message says that I can not index with an object of type Variable, but only index with torch.cuda.LongTensor or torch.cuda.ByteTensor, but if I print max_pos_distances_idx, it reports that it is a ByteTensor:

Variable containing:
 0
 2
 0
 0
 1
 2
 0
 6
 2
 0
 0
 0
 0
 1
 5
 2
 4
 0
 0
 4
[torch.cuda.ByteTensor of size 20 (GPU 0)]

What am I doing wrong here?

Thanks in advance,

This seems to be a bug. I’ve filed an issue here: https://github.com/pytorch/pytorch/issues/4219

1 Like

OK, based on discussion on the issue, you can’t do this with ByteTensor; a ByteTensor is always assumed to be a mask. If you change your index tensor into a LongTensor it will work.

1 Like