DEV Community

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

Posted on • Updated on

flip in PyTorch

Buy Me a Coffee

*My post explains flipud().

flip() can get the 0D or more D tensor of reversed zero or more elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • flip() 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 or more arguments with a tensor are dims(Required-Type:int, tuple of int or list of int). *Each number must be unique.
import torch

my_tensor = torch.tensor(2) # 0D tensor

torch.flip(input=my_tensor, dims=(0,))
my_tensor.flip(dims=(0,))
my_tensor.flip(0)
torch.flip(input=my_tensor, dims=(-1,))
# tensor(2)

my_tensor = torch.tensor([2, 7, 4]) # 1D tensor

torch.flip(input=my_tensor, dims=(0,))
torch.flip(input=my_tensor, dims=(-1,))
# tensor([4, 7, 2])

my_tensor = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor

torch.flip(input=my_tensor, dims=(0,))
torch.flip(input=my_tensor, dims=(-2,))
# tensor([[8, 3, 2], [2, 7, 4]])

torch.flip(input=my_tensor, dims=(1,))
torch.flip(input=my_tensor, dims=(-1,))
# tensor([[4, 7, 2], [2, 3, 8]])

torch.flip(input=my_tensor, dims=(0, 1))
torch.flip(input=my_tensor, dims=(0, -1))
torch.flip(input=my_tensor, dims=(1, 0))
torch.flip(input=my_tensor, dims=(1, -2))
torch.flip(input=my_tensor, dims=(-1, 0))
torch.flip(input=my_tensor, dims=(-1, -2))
torch.flip(input=my_tensor, dims=(-2, 1))
torch.flip(input=my_tensor, dims=(-2, -1))
# tensor([[2, 3, 8], [4, 7, 2]])

my_tensor = torch.tensor([[[2, 7, 4], [8, 3, 2]], # 3D tensor
                          [[5, 0, 8], [3, 6, 1]]])
torch.flip(input=my_tensor, dims=(0,))
torch.flip(input=my_tensor, dims=(-3,))
# tensor([[[5, 0, 8], [3, 6, 1]],
#         [[2, 7, 4], [8, 3, 2]]])

torch.flip(input=my_tensor, dims=(1,))
torch.flip(input=my_tensor, dims=(-2,))
# tensor([[[8, 3, 2], [2, 7, 4]],
#         [[3, 6, 1], [5, 0, 8]]])

torch.flip(input=my_tensor, dims=(2,))
torch.flip(input=my_tensor, dims=(-1,))
# tensor([[[4, 7, 2], [2, 3, 8]],
#         [[8, 0, 5], [1, 6, 3]]])

torch.flip(input=my_tensor, dims=(0, 1))
torch.flip(input=my_tensor, dims=(0, -2))
torch.flip(input=my_tensor, dims=(1, 0))
torch.flip(input=my_tensor, dims=(1, -3))
torch.flip(input=my_tensor, dims=(-2, 0))
torch.flip(input=my_tensor, dims=(-2, -3))
torch.flip(input=my_tensor, dims=(-3, 1))
torch.flip(input=my_tensor, dims=(-3, -2))
# tensor([[[3, 6, 1], [5, 0, 8]],
#         [[8, 3, 2], [2, 7, 4]]])

torch.flip(input=my_tensor, dims=(0, 2))
torch.flip(input=my_tensor, dims=(0, -1))
torch.flip(input=my_tensor, dims=(2, 0))
torch.flip(input=my_tensor, dims=(2, -3))
torch.flip(input=my_tensor, dims=(-1, 0))
torch.flip(input=my_tensor, dims=(-1, -3))
torch.flip(input=my_tensor, dims=(-3, 2))
torch.flip(input=my_tensor, dims=(-3, -1))
# tensor([[[8, 0, 5], [1, 6, 3]],
#         [[4, 7, 2], [2, 3, 8]]])

torch.flip(input=my_tensor, dims=(1, 2))
torch.flip(input=my_tensor, dims=(1, -1))
torch.flip(input=my_tensor, dims=(2, 1))
torch.flip(input=my_tensor, dims=(2, -2))
torch.flip(input=my_tensor, dims=(-1, 1))
torch.flip(input=my_tensor, dims=(-1, -2))
torch.flip(input=my_tensor, dims=(-2, 2))
torch.flip(input=my_tensor, dims=(-2, -1))
# tensor([[[2, 3, 8], [4, 7, 2]],
#         [[1, 6, 3], [8, 0, 5]]])

torch.flip(input=my_tensor, dims=(0, 1, 2))
etc.
# tensor([[[1, 6, 3], [8, 0, 5]],
#         [[2, 3, 8], [4, 7, 2]]])

my_tensor = torch.tensor([[[2., 7., 4.], [8., 3., 2.]], # 3D tensor
                          [[5., 0., 8.], [3., 6., 1.]]])
torch.flip(input=my_tensor, dims=(0,))
# tensor([[[5., 0., 8.], [3., 6., 1.]],
#         [[2., 7., 4.], [8., 3., 2.]]])

my_tensor = torch.tensor([[[2.+0.j, 7.+0.j, 4.+0.j], # 3D tensor
                           [8.+0.j, 3.+0.j, 2.+0.j]],
                          [[5.+0.j, 0.+0.j, 8.+0.j],
                           [3.+0.j, 6.+0.j, 1.+0.j]]])
torch.flip(input=my_tensor, dims=(0,))
# tensor([[[5.+0.j, 0.+0.j, 8.+0.j],
#          [3.+0.j, 6.+0.j, 1.+0.j]],
#         [[2.+0.j, 7.+0.j, 4.+0.j],
#          [8.+0.j, 3.+0.j, 2.+0.j]]])
                         # 3D tensor
my_tensor = torch.tensor([[[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]]])
torch.flip(input=my_tensor, dims=(0,))
# tensor([[[False, True, False], [False, True, False]],
#         [[True, False, True], [True, False, True]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)