DEV Community

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

Posted on • Edited on

flatten and ravel in PyTorch

Buy Me a Coffee

*Memos:

flatten() can remove zero or more dimensions by selecting 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 or a tensor.
  • The 1st argument(input) with torch or using a tensor(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 change a 0D tensor to a 1D tensor.
  • flatten() does nothing for a 1D tensor.
  • The difference between Flatten() and flatten() is:
    • The default value of start_dim for Flatten() is 1 while the default value of start_dim for flatten() is 0.
    • Basically, Flatten() is used to define a model while flatten() is not used to define a model.
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])
Enter fullscreen mode Exit fullscreen mode

ravel() can remove zero or more dimensions as much as possible from the 0D or more D tensor of zero or more elements, getting the 1D tensor of zero or more elements as shown below:

*Memos:

  • ravel() 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).
  • ravel() can change a 0D tensor to a 1D tensor.
  • ravel() does nothing for a 1D tensor.
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])
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️