DEV Community

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

Posted on • Updated on

rot90 in PyTorch

Buy Me a Coffee

*My post explains roll().

rot90() can get the 2D or more D tensor of zero or more rotated elements by 90 degrees from the 2D or more D tensor of zero or more elements as shown below:

*Memos:

  • rot90() 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 k(Optional-Default:1-Type:int).
  • The 3rd argument with torch or the 2nd argument with a tensor is dims(Optional-Default:[0, 1]-Type:tuple of int or list of int).
import torch

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

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

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

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

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

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

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

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

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

Top comments (0)