DEV Community

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

Posted on • Updated on

flipud and fliplr in PyTorch

Buy Me a Coffee

*My post explains flip().

flipud() can get the 1D or more D tensor of the zero or more elements reversed in the up/down direction from the 1D or more D tensor of zero or more elements as shown below:

*Memos:

  • flipud() 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).
import torch

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

torch.flipud(input=my_tensor)
my_tensor.flipud()
# tensor([4, 7, 2])

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

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

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

my_tensor = torch.tensor([[[2., 7., 4.], [8., 3., 2.]], # 3D tensor
                          [[5., 0., 8.], [3., 6., 1.]]])
torch.flipud(input=my_tensor)
# 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.flipud(input=my_tensor)
# 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.flipud(input=my_tensor)
# tensor([[[False, True, False], [False, True, False]],
#         [[True, False, True], [True, False, True]]])
Enter fullscreen mode Exit fullscreen mode

fliplr() can get the 2D or more D tensor of the zero or more elements reversed in the left/right direction from the 2D or more D tensor of zero or more elements as shown below:

*Memos:

  • fliplr() 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).
import torch

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

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

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

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

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.fliplr(input=my_tensor)
# tensor([[[8.+0.j, 3.+0.j, 2.+0.j],
#         [2.+0.j, 7.+0.j, 4.+0.j]],
#        [[3.+0.j, 6.+0.j, 1.+0.j],
#         [5.+0.j, 0.+0.j, 8.+0.j]]])
                         # 3D tensor
my_tensor = torch.tensor([[[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]]])
torch.fliplr(input=my_tensor)
# tensor([[[True, False, True], [True, False, True]],
#         [[False, True, False], [False, True, False]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)