flipud() and fliplr() in PyTorch
Tags: pytorch, flipud, fliplr, function
Published: 2024-07-12 20:19:25

[Buy Me a Coffee](ko-fi.com/superkai)☕

*[My post](https://dev.to/hyperkai/flip-in-pytorch-3eaa) explains [flip()](https://pytorch.org/docs/stable/generated/torch.flip.html).

[flipud()](https://pytorch.org/docs/stable/generated/torch.flipud.html) 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`).

```python
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]]])
```

[fliplr()](https://pytorch.org/docs/stable/generated/torch.fliplr.html) 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`).

```python
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]]])
```