DEV Community

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

Posted on

flatten() and ravel() in PyTorch

*My post explains unflatten().

flatten() can remove zero or more dimensions from a 0D or more D tensor as shown below:

*Memos:

  • flatten() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(int) with torch or the 1st argument(int) with a tensor is start_dim(Optional-Default:0) which is the 1st dimension.
  • The 3rd argument(int) with torch or the 2nd argument(int) with a tensor is end_dim(Optional-Default:-1) which is the last dimension.
  • flatten() can make a 0D tensor a 1D tensor.
import torch

my_tensor = torch.tensor(7) # 0D tensor
                            # Size:[]
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])
# Size:[1]

my_tensor = torch.tensor([7, 1, -8, 3, -6, 0]) # 1D tensor
                                               # Size:[3]
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])
# Size:[6]

my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]]) # 2D tensor
                                                   # Size:[2, 3]
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])
# Size:[6]

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]])
# Size:[2, 3]

my_tensor = torch.tensor([[[7], [1], [-8]], [[3], [-6], [0]]])
                         # 3D tensor
                         # Size:[2, 3, 1]
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])
# Size:[6]

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]]])
# Size:[2, 3, 1]

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]])
# Size:[6, 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=2)
torch.flatten(input=my_tensor, start_dim=-2, end_dim=-1)
# tensor([[7, 1, -8], [3, -6, 0]])
# Size:[2, 3]

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

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

ravel() 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(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
import torch

my_tensor = torch.tensor(7) # 0D tensor
                            # Size:[]
torch.ravel(input=my_tensor)
my_tensor.ravel()
# tensor([7])
# Size:[1]

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

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

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

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

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

my_tensor = torch.tensor([[[True], [False], [True]],   # 3D tensor
                          [[False], [True], [False]]]) # Size:[2, 3, 1]
torch.ravel(input=my_tensor)
# tensor([True, False, True, False, True, False])
# Size:[6]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)