2025, Oct 24 09:00

Rotate PyTorch Tensors in the Opposite Direction with torch.roll: use a negative shift

Learn the simplest way to rotate PyTorch tensors left or up using torch.roll with a negative shift. Avoid reverse/flip mistakes and fix direction bugs fast.

When you rotate elements in a PyTorch tensor with roll, it’s easy to assume there’s only one direction. By default, a positive shift feels like it pushes values “down” or to the right. But sometimes you need the opposite: a leftward or upward rotation. Reversing with inverse is not the same thing — that flips the entire order, which is a different operation. Here’s the minimal, practical way to roll the other way without detours.

Problem setup

Suppose you start with a 1D tensor:

import torch
vec = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
shifted = vec.roll(1, 0)
print(shifted)

This moves the last element to the front, effectively rotating to the right:

tensor([6., 1., 2., 3., 4., 5.])

The goal, however, is the opposite order:

[2.0, 3.0, 4.0, 5.0, 6.0, 1.0]

In some workflows, the last element will then be zero’d after the rotation, but the key part here is changing the roll direction itself.

What’s actually happening

roll performs a circular shift along a given dimension. The sign of the shift controls the direction: positive values rotate one way, negative values rotate the other way. inverse does a full reversal of the sequence, which isn’t equivalent to a directional shift.

Solution

Use a negative shift to roll in the opposite direction. That’s it.

import torch
src = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
rotated_left = src.roll(-1, 0)
print(rotated_left)

Output:

tensor([2., 3., 4., 5., 6., 1.])

The official documentation shows an identical pattern for multi-dimensional tensors. For example, a 4x2 tensor rotated upward along dimension 0 with a shift of -1:

import torch
mat = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8]).view(4, 2)
print(mat)
print(torch.roll(mat, -1, 0))

Output:

tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])
tensor([[3, 4],
        [5, 6],
        [7, 8],
        [1, 2]])

Reference: https://docs.pytorch.org/docs/stable/generated/torch.roll.html

Why this matters

Directionality in sequence transformations is a frequent source of subtle bugs. Using a negative shift with roll makes the intent explicit and removes the need to reverse or otherwise post-process to compensate. It’s also an easy detail to miss when skimming documentation for a quick “pop-like” behavior, even though the exact use case is covered there.

Takeaways

If you need to rotate a PyTorch tensor in the opposite direction, don’t switch to inverse or build a workaround. Call roll with a negative shift on the target dimension. It communicates intent clearly, matches the documented behavior, and keeps the operation both correct and concise.

The article is based on a question from StackOverflow by FlumeRS and an answer by Aadvik.