DEV Community

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

Posted on • Updated on

flip(), flipud() and fliplr() in PyTorch

flip() can reverse a 0D or more D tensor as shown below:

import torch

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

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

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

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

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

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

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

torch.flip(my_tensor, (0, 1))
my_tensor.flip((0, 1))
my_tensor.flip(0, 1)
torch.flip(my_tensor, (0, -1))
my_tensor.flip((0, -1))
my_tensor.flip(0, -1)
torch.flip(my_tensor, (1, 0))
my_tensor.flip((1, 0))
my_tensor.flip(1, 0)
torch.flip(my_tensor, (1, -2))
my_tensor.flip((1, -2))
my_tensor.flip(1, -2)
torch.flip(my_tensor, (-1, 0))
my_tensor.flip((-1, 0))
my_tensor.flip(-1, 0)
torch.flip(my_tensor, (-1, -2))
my_tensor.flip((-1, -2))
my_tensor.flip(-1, -2)
torch.flip(my_tensor, (-2, 1))
my_tensor.flip((-2, 1))
my_tensor.flip(-2, 1)
torch.flip(my_tensor, (-2, -1))
my_tensor.flip((-2, -1))
my_tensor.flip(-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(my_tensor, (0,))
my_tensor.flip((0,))
my_tensor.flip(0)
torch.flip(my_tensor, (-3,))
my_tensor.flip((-3,))
my_tensor.flip(-3)
# tensor([[[5, 0, 8], [3, 6, 1]],
#         [[2, 7, 4], [8, 3, 2]]])

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

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

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

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

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

torch.flip(my_tensor, (0, 1, 2))
my_tensor.flip((0, 1, 2))
my_tensor.flip(0, 1, 2)
etc.
# tensor([[[1, 6, 3], [8, 0, 5]],
#         [[2, 3, 8], [4, 7, 2]]])
etc.
Enter fullscreen mode Exit fullscreen mode

*Memos:

  • flip() can be called both from torch and a tensor.
  • The 2nd argument is one or more dimensions with torch.
  • One or more arguments are one or more dimensions with a tensor.

flipud() can reverse a 1D or more D tensor in the up/down direction as shown below. *flipud() can be called both from torch and a tensor:

import torch

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

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

fliplr() can reverse a 2D or more D tensor in the left/right direction as shown below. *fliplr() can be called both from torch and a tensor:

import torch

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

Top comments (0)