# How to Reverse a Torch Tensor

How to Reverse a Torch Tensor

1 Like

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)
``````
8 Likes

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!

1 Like

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)
``````
7 Likes

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)
print(â€śBefore ::â€ť,aa)
print(â€śAfter ::â€ť,inverted_tensor)

Use torch.flip

1 Like

torch.flip does not do the task but fliplr does