I’ve recently started using Torch for Deep Learning, and besides the fact that Lua has 1-based array indexing and I used to forget that, I’m not having too much trouble.

However, when I needed to reverse a Torch Tensor in one dimension, I discovered that Torch slicing is not as expressive as the awesome Numpy slicing, so I had to figure out another way to reverse a Tensor.

In Numpy to reverse an array in a specific dimension I would do something like:

But in Torch I haven’t figured out how to do so, and when I googled for a solution I didn’t find many helpful links explaining a good way to reverse a Tensor, I found this question on Torch’s google group for example, so I’m going to share what I’ve been using.

In Torch, for a case of a tensor with size (batch_size, dim, seq_len) which we want to reverse in the 3rd dimension, the code is the following:

Easy, right? I hope it works for you too.