reverse the sequence in tensor

1,533 views
Skip to first unread message

Liang Lu

unread,
Sep 3, 2015, 6:55:28 AM9/3/15
to torch7
Hi,

I'm new to Torch. Suppose I have a 3-D tensor input  as (minibatch_size, dim, seq_len), and the seq_len is index from 1, 2, ...,  T. I would like to reverse the time index of this tensor so that the seq_len is indexed from T, T-1, ..., 2, 1. This is used to implement the bi-directional RNN. I was wondering is there any efficient way to do this in Torch.

Thanks,
Liang

Brendan

unread,
Sep 3, 2015, 9:53:52 PM9/3/15
to torch7
Preallocate a tensor of the same size, then use a for loop over t=1...T, using narrow over the time dimension on both tensors (index t on one, T-t+1 on the other), then doing :copy(). The code will be faster if the input is ordered as (seq_len, ...), where ... is either (mbsize, dim) or (dim, mbsize). That way, each copy will be on contiguous data.

Guillem C

unread,
Jul 12, 2016, 10:19:18 AM7/12/16
to torch7
It is an old post, but in case someone gets here looking to reverse a Torch tensor, I use the following:

In your case, imagine a tensor x like

x = torch.Tensor(batch_size, dim, seq_len)

which you want to reverse in the x dimension.

The code to do so is:

x = x:index(3 ,torch.linspace(seq_len,1,seq_len):long()) -- the first argument to index is 3 because you want to reverse the 3rd dimension

This code is using the index function: https://github.com/torch/torch7/blob/master/doc/tensor.md#tensor-indexdim-index
and then creating a tensor of indexes in reverse order with linspace.

Note that the index vector is casted to LongTensor type with :long() because it is the required type for the index function.
Reply all
Reply to author
Forward
0 new messages