a = torch.Tensor([1,2,3])
a[-1] # works, 3
a[[0,1]] # works, 1,2
a[[-1,0]] # not work, raise RuntimeError: index out of range
It would be nice to also support negative values in LongTensor indexing.
a = torch.Tensor([1,2,3])
a[-1] # works, 3
a[[0,1]] # works, 1,2
a[[-1,0]] # not work, raise RuntimeError: index out of range
It would be nice to also support negative values in LongTensor indexing.
Right now it works if you wrap a
in a Variable
:
a = Variable(torch.randn(5))
a[[-1, 0]]
There are plans to merge Variable
and Tensor
for a more unified API. When that happens, negative values in LongTensors will be supported.
Running:
import torch
from torch.autograd import Variable
a = Variable(torch.randn(5))
a[[-1, 0]]
Results in an error:
a[[-1, 0]]
File "/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py", line 78, in __getitem__
return Index.apply(self, key)
File "/usr/local/lib/python2.7/dist-packages/torch/autograd/_functions/tensor.py", line 87, in forward
result = i.index(ctx.index)
RuntimeError: index out of range at /pytorch/torch/lib/TH/generic/THTensorMath.c:277