DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

roll in PyTorch

Buy Me a Coffee

*My post explains rot90().

roll() can get the 0D or more D tensor of zero or more right-shifted or left-shifted elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • roll() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is shifts(Required-Type:int, tuple of int or list of int).
  • The 3rd argument with torch or the 2nd argument with a tensor is dims(Optional-Type:int, tuple of int or list of int).
  • The number of shifts and dims must be the same.
import torch

my_tensor = torch.tensor([[0, 1, 2], [3, 4, 5]])

torch.roll(input=my_tensor, shifts=0)
my_tensor.roll(shifts=0)
torch.roll(input=my_tensor, shifts=-1)
torch.roll(input=my_tensor, shifts=0, dims=0)
torch.roll(input=my_tensor, shifts=0, dims=1)
torch.roll(input=my_tensor, shifts=0, dims=-1)
torch.roll(input=my_tensor, shifts=0, dims=-2)
torch.roll(input=my_tensor, shifts=2, dims=0)
torch.roll(input=my_tensor, shifts=2, dims=-2)
torch.roll(input=my_tensor, shifts=3, dims=1)
torch.roll(input=my_tensor, shifts=3, dims=-1)
torch.roll(input=my_tensor, shifts=4, dims=0)
torch.roll(input=my_tensor, shifts=4, dims=-2)
torch.roll(input=my_tensor, shifts=-2, dims=0)
torch.roll(input=my_tensor, shifts=-2, dims=-2)
torch.roll(input=my_tensor, shifts=-3, dims=1)
torch.roll(input=my_tensor, shifts=-3, dims=-1)
torch.roll(input=my_tensor, shifts=-4, dims=0)
torch.roll(input=my_tensor, shifts=-4, dims=-2)
torch.roll(input=my_tensor, shifts=0, dims=(0,))
torch.roll(input=my_tensor, shifts=0, dims=(1,))
torch.roll(input=my_tensor, shifts=0, dims=(-1,))
torch.roll(input=my_tensor, shifts=0, dims=(-2,))
torch.roll(input=my_tensor, shifts=2, dims=(0,))
torch.roll(input=my_tensor, shifts=3, dims=(1,))
torch.roll(input=my_tensor, shifts=3, dims=(-1,))
torch.roll(input=my_tensor, shifts=4, dims=(0,))
torch.roll(input=my_tensor, shifts=4, dims=(-2,))
torch.roll(input=my_tensor, shifts=-2, dims=(0,))
torch.roll(input=my_tensor, shifts=-2, dims=(-2,))
torch.roll(input=my_tensor, shifts=-3, dims=(1,))
torch.roll(input=my_tensor, shifts=-3, dims=(-1,))
torch.roll(input=my_tensor, shifts=-4, dims=(0,))
torch.roll(input=my_tensor, shifts=-4, dims=(-2,))
torch.roll(input=my_tensor, shifts=(0,), dims=0)
torch.roll(input=my_tensor, shifts=(0,), dims=1)
torch.roll(input=my_tensor, shifts=(0,), dims=-1)
torch.roll(input=my_tensor, shifts=(0,), dims=-2)
torch.roll(input=my_tensor, shifts=(2,), dims=0)
torch.roll(input=my_tensor, shifts=(2,), dims=-2)
torch.roll(input=my_tensor, shifts=(3,), dims=1)
torch.roll(input=my_tensor, shifts=(3,), dims=-1)
torch.roll(input=my_tensor, shifts=(4,), dims=0)
torch.roll(input=my_tensor, shifts=(4,), dims=-2)
torch.roll(input=my_tensor, shifts=(-2,), dims=0)
torch.roll(input=my_tensor, shifts=(-2,), dims=-2)
torch.roll(input=my_tensor, shifts=(-3,), dims=1)
torch.roll(input=my_tensor, shifts=(-3,), dims=-1)
torch.roll(input=my_tensor, shifts=(-4,), dims=0)
torch.roll(input=my_tensor, shifts=(-4,), dims=-2)
torch.roll(input=my_tensor, shifts=(0,), dims=(0,))
torch.roll(input=my_tensor, shifts=(0,), dims=(1,))
torch.roll(input=my_tensor, shifts=(0,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(0,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(2,), dims=(0,))
torch.roll(input=my_tensor, shifts=(2,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(3,), dims=(1,))
torch.roll(input=my_tensor, shifts=(3,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(4,), dims=(0,))
torch.roll(input=my_tensor, shifts=(4,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(-2,), dims=(0,))
torch.roll(input=my_tensor, shifts=(-2,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(-3,), dims=(1,))
torch.roll(input=my_tensor, shifts=(-3,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(-4,), dims=(0,))
torch.roll(input=my_tensor, shifts=(-4,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(0, 0))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(0, 1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(0, -1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(0, -2))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(1, 0))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(1, 1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(1, -1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(1, -2))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-1, 0))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-1, 1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-1, -1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-1, -2))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-2, 0))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-2, 1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-2, -1))
torch.roll(input=my_tensor, shifts=(0, 0), dims=(-2, -2))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(0, 0))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(0, -2))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(1, 0))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(1, -2))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-1, 0))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-1, -2))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-2, 0))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-2, -2))
etc.
# tensor([[0, 1, 2], [3, 4, 5]])

torch.roll(input=my_tensor, shifts=1)
torch.roll(input=my_tensor, shifts=-5)
etc.
# tensor([[5, 0, 1], [2, 3, 4]])

torch.roll(input=my_tensor, shifts=2)
torch.roll(input=my_tensor, shifts=-4)
etc.
# tensor([[4, 5, 0], [1, 2, 3]])

torch.roll(input=my_tensor, shifts=3)
torch.roll(input=my_tensor, shifts=-3)
torch.roll(input=my_tensor, shifts=1, dims=0)
torch.roll(input=my_tensor, shifts=1, dims=-2)
torch.roll(input=my_tensor, shifts=3, dims=0)
torch.roll(input=my_tensor, shifts=3, dims=-2)
torch.roll(input=my_tensor, shifts=-1, dims=0)
torch.roll(input=my_tensor, shifts=-1, dims=-2)
torch.roll(input=my_tensor, shifts=-3, dims=0)
torch.roll(input=my_tensor, shifts=-3, dims=-2)
torch.roll(input=my_tensor, shifts=-5, dims=0)
torch.roll(input=my_tensor, shifts=-5, dims=-2)
torch.roll(input=my_tensor, shifts=1, dims=(0,))
torch.roll(input=my_tensor, shifts=1, dims=(-2,))
torch.roll(input=my_tensor, shifts=3, dims=(0,))
torch.roll(input=my_tensor, shifts=3, dims=(-2,))
torch.roll(input=my_tensor, shifts=-1, dims=(0,))
torch.roll(input=my_tensor, shifts=-1, dims=(-2,))
torch.roll(input=my_tensor, shifts=-3, dims=(0,))
torch.roll(input=my_tensor, shifts=-3, dims=(-2,))
torch.roll(input=my_tensor, shifts=-5, dims=(0,))
torch.roll(input=my_tensor, shifts=-5, dims=(-2,))
torch.roll(input=my_tensor, shifts=(1,), dims=0)
torch.roll(input=my_tensor, shifts=(1,), dims=-2)
torch.roll(input=my_tensor, shifts=(3,), dims=0)
torch.roll(input=my_tensor, shifts=(3,), dims=-2)
torch.roll(input=my_tensor, shifts=(5,), dims=0)
torch.roll(input=my_tensor, shifts=(5,), dims=-2)
torch.roll(input=my_tensor, shifts=(-1,), dims=0)
torch.roll(input=my_tensor, shifts=(-1,), dims=-2)
torch.roll(input=my_tensor, shifts=(-3,), dims=0)
torch.roll(input=my_tensor, shifts=(-3,), dims=-2)
torch.roll(input=my_tensor, shifts=(-5,), dims=0)
torch.roll(input=my_tensor, shifts=(-5,), dims=-2)
torch.roll(input=my_tensor, shifts=(1,), dims=(0,))
torch.roll(input=my_tensor, shifts=(1,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(3,), dims=(0,))
torch.roll(input=my_tensor, shifts=(3,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(5,), dims=(0,))
torch.roll(input=my_tensor, shifts=(5,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(-1,), dims=(0,))
torch.roll(input=my_tensor, shifts=(-1,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(-3,), dims=(0,))
torch.roll(input=my_tensor, shifts=(-3,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(-5,), dims=(0,))
torch.roll(input=my_tensor, shifts=(-5,), dims=(-2,))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(0, 0))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(0, -2))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(1, 0))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(1, -2))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-1, 0))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-1, -2))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-2, 0))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-2, -2))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(0, 0))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(0, -2))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(1, 0))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(1, -2))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-1, 0))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-1, -2))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-2, 0))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-2, -2))
etc.
# tensor([[3, 4, 5], [0, 1, 2]])

torch.roll(input=my_tensor, shifts=4)
torch.roll(input=my_tensor, shifts=-2)
etc.
# tensor([[2, 3, 4], [5, 0, 1]])

torch.roll(input=my_tensor, shifts=1, dims=1)
torch.roll(input=my_tensor, shifts=1, dims=-1)
torch.roll(input=my_tensor, shifts=4, dims=1)
torch.roll(input=my_tensor, shifts=4, dims=-1)
torch.roll(input=my_tensor, shifts=-2, dims=1)
torch.roll(input=my_tensor, shifts=-2, dims=-1)
torch.roll(input=my_tensor, shifts=-5, dims=1)
torch.roll(input=my_tensor, shifts=-5, dims=-1)
torch.roll(input=my_tensor, shifts=1, dims=(1,))
torch.roll(input=my_tensor, shifts=1, dims=(-1,))
torch.roll(input=my_tensor, shifts=4, dims=(1,))
torch.roll(input=my_tensor, shifts=4, dims=(-1,))
torch.roll(input=my_tensor, shifts=-2, dims=(1,))
torch.roll(input=my_tensor, shifts=-2, dims=(-1,))
torch.roll(input=my_tensor, shifts=-5, dims=(1,))
torch.roll(input=my_tensor, shifts=-5, dims=(-1,))
torch.roll(input=my_tensor, shifts=(1,), dims=1)
torch.roll(input=my_tensor, shifts=(1,), dims=-1)
torch.roll(input=my_tensor, shifts=(4,), dims=1)
torch.roll(input=my_tensor, shifts=(4,), dims=-1)
torch.roll(input=my_tensor, shifts=(-2,), dims=1)
torch.roll(input=my_tensor, shifts=(-2,), dims=-1)
torch.roll(input=my_tensor, shifts=(-5,), dims=1)
torch.roll(input=my_tensor, shifts=(-5,), dims=-1)
torch.roll(input=my_tensor, shifts=(1,), dims=(1,))
torch.roll(input=my_tensor, shifts=(1,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(4,), dims=(1,))
torch.roll(input=my_tensor, shifts=(4,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(-2,), dims=(1,))
torch.roll(input=my_tensor, shifts=(-2,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(-5,), dims=(1,))
torch.roll(input=my_tensor, shifts=(-5,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(0, 1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(0, -1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(1, 1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(1, -1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-1, 1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-1, -1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-2, 1))
torch.roll(input=my_tensor, shifts=(0, 1), dims=(-2, -1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(0, 1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(0, -1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(1, 1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(1, -1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-1, 1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-1, -1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-2, 1))
torch.roll(input=my_tensor, shifts=(0, -2), dims=(-2, -1))
etc.
# tensor([[2, 0, 1], [5, 3, 4]])

torch.roll(input=my_tensor, shifts=2, dims=1)
torch.roll(input=my_tensor, shifts=2, dims=-1)
torch.roll(input=my_tensor, shifts=-1, dims=1)
torch.roll(input=my_tensor, shifts=-1, dims=-1)
torch.roll(input=my_tensor, shifts=-4, dims=1)
torch.roll(input=my_tensor, shifts=-4, dims=-1)
torch.roll(input=my_tensor, shifts=2, dims=(1,))
torch.roll(input=my_tensor, shifts=2, dims=(-1,))
torch.roll(input=my_tensor, shifts=2, dims=(-2,))
torch.roll(input=my_tensor, shifts=-1, dims=(1,))
torch.roll(input=my_tensor, shifts=-1, dims=(-1,))
torch.roll(input=my_tensor, shifts=-4, dims=(1,))
torch.roll(input=my_tensor, shifts=-4, dims=(-1,))
torch.roll(input=my_tensor, shifts=(2,), dims=1)
torch.roll(input=my_tensor, shifts=(2,), dims=-1)
torch.roll(input=my_tensor, shifts=(5,), dims=1)
torch.roll(input=my_tensor, shifts=(5,), dims=-1)
torch.roll(input=my_tensor, shifts=(-1,), dims=1)
torch.roll(input=my_tensor, shifts=(-1,), dims=-1)
torch.roll(input=my_tensor, shifts=(-4,), dims=1)
torch.roll(input=my_tensor, shifts=(-4,), dims=-1)
torch.roll(input=my_tensor, shifts=(2,), dims=(1,))
torch.roll(input=my_tensor, shifts=(2,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(5,), dims=(1,))
torch.roll(input=my_tensor, shifts=(5,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(-1,), dims=(1,))
torch.roll(input=my_tensor, shifts=(-1,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(-4,), dims=(1,))
torch.roll(input=my_tensor, shifts=(-4,), dims=(-1,))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(0, 1))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(0, -1))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(1, -1))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-1, 1))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-1, -1))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-2, 1))
torch.roll(input=my_tensor, shifts=(0, -1), dims=(-2, -1))
etc.
# tensor([[1, 2, 0], [4, 5, 3]])

my_tensor = torch.tensor([[0., 1., 2.], [3., 4., 5.]])

torch.roll(input=my_tensor, shifts=0)
# tensor([[0., 1., 2.], [3., 4., 5.]])

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j],
                          [3.+0.j, 4.+0.j, 5.+0.j]])
torch.roll(input=my_tensor, shifts=0)
# tensor([[0.+0.j, 1.+0.j, 2.+0.j],
#         [3.+0.j, 4.+0.j, 5.+0.j]])

my_tensor = torch.tensor([[True, True, True],
                          [False, False, False]])
torch.roll(input=my_tensor, shifts=0)
# tensor([[True, True, True],
#         [False, False, False]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)