How to Reverse a Torch Tensor
There is not yet slicing with negative indices in pytorch, but this issue is being tracked in https://github.com/pytorch/pytorch/issues/229 .
In the mean time, you can use
index_select for that
tensor = torch.rand(10) # your tensor # create inverted indices idx = [i for i in range(tensor.size(0)-1, -1, -1)] idx = torch.LongTensor(idx) inverted_tensor = tensor.index_select(0, idx)
Hi, it looks like this can’t work on torch.autograd.variable.Variable. Is there any way to reverse a specific dimension in a Variable? Thanks!
It should work once you wrap
idx in a Variable
If your use case is to reverse sequences to use in Bidirectional RNNs, I just create a clone and flip using numpy.
rNpArr = np.flip(fTensor.numpy(),0).copy() #Reverse of copy of numpy array of given tensor rTensor = torch.from_numpy(rNpArr)
Can you try something like:
import torch as pt
aa = pt.tensor([[1,2,3],[4,5,6],[7,8,9]])
idx = [i for i in range(aa.size(0)-1, -1, -1)]
idx = pt.LongTensor(idx)
inverted_tensor = pt.index_select(aa,0,idx)