flatten() and ravel() in PyTorch
Tags: pytorch, flatten, ravel, flat
Published: 2024-05-23 00:45:34

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

[flatten()](https://pytorch.org/docs/stable/generated/torch.flatten.html) can remove zero or more dimensions from the 0D or more D tensor of zero or more elements, getting the 1D or more D tensor of zero or more elements as shown below:

*Memos:
- `flatten()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- The 2nd argument with `torch` or the 1st argument with a tensor is `start_dim`(Optional-Default:`0`-Type:`int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `end_dim`(Optional-Default:`-1`-Type:`int`).
- `flatten()` can make a 0D tensor a 1D tensor. 

```python
import torch

my_tensor = torch.tensor(7)

torch.flatten(input=my_tensor)
my_tensor.flatten()
torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
# tensor([7])

my_tensor = torch.tensor([7, 1, -8, 3, -6, 0])

torch.flatten(input=my_tensor)
torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
# tensor([7, 1, -8, 3, -6, 0])

my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]])

torch.flatten(input=my_tensor)
torch.flatten(input=my_tensor, start_dim=0, end_dim=1)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-1)
# tensor([7, 1, -8, 3, -6, 0])

torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=1, end_dim=1)
torch.flatten(input=my_tensor, start_dim=1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-2)
# tensor([[7, 1, -8], [3, -6, 0]])

my_tensor = torch.tensor([[[7], [1], [-8]], [[3], [-6], [0]]])

torch.flatten(input=my_tensor)
torch.flatten(input=my_tensor, start_dim=0, end_dim=2)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=2)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=-1)
# tensor([7, 1, -8, 3, -6, 0])

torch.flatten(input=my_tensor, start_dim=0, end_dim=0)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-3)
torch.flatten(input=my_tensor, start_dim=1, end_dim=1)
torch.flatten(input=my_tensor, start_dim=1, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=2, end_dim=2)
torch.flatten(input=my_tensor, start_dim=2, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=2)
torch.flatten(input=my_tensor, start_dim=-1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=0)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=-3)
# tensor([[[7], [1], [-8]], [[3], [-6], [0]]])

torch.flatten(input=my_tensor, start_dim=0, end_dim=1)
torch.flatten(input=my_tensor, start_dim=0, end_dim=-2)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=1)
torch.flatten(input=my_tensor, start_dim=-3, end_dim=-2)
# tensor([[7], [1], [-8], [3], [-6], [0]])

torch.flatten(input=my_tensor, start_dim=1, end_dim=2)
torch.flatten(input=my_tensor, start_dim=1, end_dim=-1)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=2)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-1)
# tensor([[7, 1, -8], [3, -6, 0]])

my_tensor = torch.tensor([[[7.], [1.], [-8.]],
                          [[3.], [-6.], [0.]]])
torch.flatten(input=my_tensor)
# tensor([7., 1., -8., 3., -6., 0.])

my_tensor = torch.tensor([[[7.+0.j], [1.+0.j], [-8.+0.j]],
                          [[3.+0.j], [-6.+0.j], [0.+0.j]]])
torch.flatten(input=my_tensor)
# tensor([7.+0.j, 1.+0.j, -8.+0.j, 3.+0.j, -6.+0.j, 0.+0.j])

my_tensor = torch.tensor([[[True], [False], [True]],
                          [[False], [True], [False]]])
torch.flatten(input=my_tensor)
# tensor([True, False, True, False, True, False])
```

[ravel()](https://pytorch.org/docs/stable/generated/torch.ravel.html) can flat a 0D or more D tensor to a 1D tensor as shown below:

*Memos:
- `ravel()` can be used with `torch` or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).

```python
import torch

my_tensor = torch.tensor(7)

torch.ravel(input=my_tensor)
my_tensor.ravel()
# tensor([7])

my_tensor = torch.tensor([7, 1, -8, 3, -6, 0])

torch.ravel(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])

my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]])

torch.ravel(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])

my_tensor = torch.tensor([[[7], [1], [-8]],
                          [[3], [-6], [0]]])
torch.ravel(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])

my_tensor = torch.tensor([[[7.], [1.], [-8.]],
                          [[3.], [-6.], [0.]]])
torch.ravel(input=my_tensor)
# tensor([7., 1., -8., 3., -6., 0.])

my_tensor = torch.tensor([[[7.+0.j], [1.+0.j], [-8.+0.j]],
                          [[3.+0.j], [-6.+0.j], [0.+0.j]]])
torch.ravel(input=my_tensor)
# tensor([7.+0.j, 1.+0.j, -8.+0.j, 3.+0.j, -6.+0.j, 0.+0.j])

my_tensor = torch.tensor([[[True], [False], [True]],
                          [[False], [True], [False]]])
torch.ravel(input=my_tensor)
# tensor([True, False, True, False, True, False])
```