DEV Community

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

Posted on

squeeze in PyTorch

Buy Me a Coffee

*My post explains unsqueeze().

squeeze() can get the 0D or more D tensor of the zero or more elements whose zero or more dimensions are removed if the size is 1 from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • squeeze() 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 or more arguments with a tensor are dim(Optional-Type:int, tuple of int or list of int): *Memos:
    • Each number must be unique.
    • It can remove the specific zero or more dimensions whose size is 1.
    • If the size is not 1, zero or more dimensions are not removed even if you set it.
import torch

my_tensor = torch.tensor([[[[0], [1]],
                           [[2], [3]],
                           [[4], [5]]]])
torch.squeeze(input=my_tensor)
my_tensor.squeeze()
torch.squeeze(input=my_tensor, dim=(0, 3))
my_tensor.squeeze(dim=(0, 3))
my_tensor.squeeze(0, 3)
torch.squeeze(input=my_tensor, dim=(0, 1, 3))
my_tensor.squeeze(dim=(0, 1, 3))
my_tensor.squeeze(0, 1, 3)
etc.
torch.squeeze(input=my_tensor, dim=(0, 1, 2, 3))
my_tensor.squeeze(dim=(0, 1, 2, 3))
my_tensor.squeeze(0, 1, 2, 3)
etc.
# tensor([[0, 1],
#         [2, 3],
#         [4, 5]])

torch.squeeze(input=my_tensor, dim=0)
torch.squeeze(input=my_tensor, dim=-4)
torch.squeeze(input=my_tensor, dim=(0,))
torch.squeeze(input=my_tensor, dim=(-4,))
torch.squeeze(input=my_tensor, dim=(0, 1))
torch.squeeze(input=my_tensor, dim=(0, 2))
torch.squeeze(input=my_tensor, dim=(0, -2))
torch.squeeze(input=my_tensor, dim=(0, -3))
torch.squeeze(input=my_tensor, dim=(1, 0))
etc.
torch.squeeze(input=my_tensor, dim=(0, 1, 2))
etc.
# tensor([[[0], [1]],
#         [[2], [3]],
#         [[4], [5]]])

torch.squeeze(input=my_tensor, dim=1)
torch.squeeze(input=my_tensor, dim=2)
torch.squeeze(input=my_tensor, dim=-2)
torch.squeeze(input=my_tensor, dim=-3)
torch.squeeze(input=my_tensor, dim=())
torch.squeeze(input=my_tensor, dim=(1,))
torch.squeeze(input=my_tensor, dim=(2,))
torch.squeeze(input=my_tensor, dim=(-2,))
torch.squeeze(input=my_tensor, dim=(-3,))
torch.squeeze(input=my_tensor, dim=(1, 2))
etc.
# tensor([[[[0], [1]],
#          [[2], [3]],
#          [[4], [5]]]])

torch.squeeze(input=my_tensor, dim=3)
torch.squeeze(input=my_tensor, dim=-1)
torch.squeeze(input=my_tensor, dim=(3,))
torch.squeeze(input=my_tensor, dim=(-1,))
torch.squeeze(input=my_tensor, dim=(1, 3))
torch.squeeze(input=my_tensor, dim=(1, -1))
torch.squeeze(input=my_tensor, dim=(2, 3))
torch.squeeze(input=my_tensor, dim=(2, -1))
torch.squeeze(input=my_tensor, dim=(3, 1))
etc.
torch.squeeze(input=my_tensor, dim=(1, 2, 3))
etc.
# tensor([[[0, 1],
#          [2, 3],
#          [4, 5]]])

my_tensor = torch.tensor([[[[0.], [1.]],
                           [[2.], [3.]],
                           [[4.], [5.]]]])
torch.squeeze(input=my_tensor)
# tensor([[0., 1.],
#         [2., 3.],
#         [4., 5.]])

my_tensor = torch.tensor([[[[0.+0.j], [1.+0.j]],
                           [[2.+0.j], [3.+0.j]],
                           [[4.+0.j], [5.+0.j]]]])
torch.squeeze(input=my_tensor)
# tensor([[0.+0.j, 1.+0.j],
#         [2.+0.j, 3.+0.j],
#         [4.+0.j, 5.+0.j]])

my_tensor = torch.tensor([[[[True], [False]],
                           [[False], [True]],
                           [[True], [False]]]])
torch.squeeze(input=my_tensor)
# tensor([[True, False],
#         [False, True],
#         [True, False]])
Enter fullscreen mode Exit fullscreen mode

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

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

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay