Here is a visual representation of what squeeze/unsqueeze do for an effectively 2d matrix:

When you are unsqueezing a tensor, it is ambiguous which dimension you wish to 'unsqueeze' it across (as a row or column etc). The dim argument dictates this - i.e. position of the new dimension to be added.

Hence the resulting unsqueezed tensors have the same information, but the indices used to access them are different.

Answer from End genocide - save Gaza on Stack Overflow
Top answer
1 of 3
73

Here is a visual representation of what squeeze/unsqueeze do for an effectively 2d matrix:

When you are unsqueezing a tensor, it is ambiguous which dimension you wish to 'unsqueeze' it across (as a row or column etc). The dim argument dictates this - i.e. position of the new dimension to be added.

Hence the resulting unsqueezed tensors have the same information, but the indices used to access them are different.

2 of 3
28

Simply put, unsqueeze() "adds" a superficial 1 dimension to tensor (at the specified dimension), while squeeze removes all superficial 1 dimensions from tensor.

A 1 dimension is superficial in the sense that it does not add any more elements to the tensor than would be there without it. Whenever this is true of a tensor dimension, it will always have the size 1 - hence the name "1 dimension".

You should look at tensor's shape attribute to see it easily. In your last case it would be:

import torch

tensor = torch.tensor([1, 0, 2, 3, 4])
tensor.shape # torch.Size([5])
tensor.unsqueeze(dim=0).shape # [1, 5]
tensor.unsqueeze(dim=1).shape # [5, 1]

It is useful for providing single sample to the network (which requires first dimension to be batch), for images it would be:

# 3 channels, 32 width, 32 height
tensor = torch.randn(3, 32, 32)
# 1 batch, 3 channels, 32 width, 32 height
tensor.unsqueeze(dim=0).shape

unsqueeze can be seen if you create tensor with 1 dimensions, e.g. like this:

# 3 channels, 32 width, 32 height and some 1 unnecessary dimensions
tensor = torch.randn(3, 1, 32, 1, 32, 1)
# 1 batch, 3 channels, 32 width, 32 height again
tensor.squeeze().unsqueeze(0) # [1, 3, 32, 32]
Discussions

Numpy or pytroch squeeze
The use case explains itself, so whenever you would need to remove a size 1 dimension, you could use squeeze. E.g. if you are dealing with a single sample somewhere in your code and would like to remove the batch size in order to plot the image tensor via plt.imshow More on discuss.pytorch.org
🌐 discuss.pytorch.org
3
0
October 30, 2021
Hot take: never use squeeze
I think squeeze is much more readable when you have multiple dimensions with size 1. Perhaps your lesson learned is 'never call squeeze on tensors with dynamic size which occasionally have size 1' More on reddit.com
🌐 r/pytorch
8
4
December 7, 2024
python - What does "unsqueeze" do in Pytorch? - Stack Overflow
That is opposite of np.squeeze() (nomenclature borrowed from MATLAB) which removes axes of size 1 (singletons). ... It indicates the position on where to add the dimension. torch.unsqueeze adds an additional dimension to the tensor. More on stackoverflow.com
🌐 stackoverflow.com
How do I learn when to unsqueeze, squeeze etc? And overall dimension "stuff".
This really comes with practice imo. What can help is really making sure you really know all the shapes in each line, maybe even write them as a comment More on reddit.com
🌐 r/learnmachinelearning
4
1
January 22, 2023
🌐
ProjectPro
projectpro.io › recipes › what-does-squeeze-function-do-torch
What does squeeze function do in torch -
August 3, 2022 - This is achieved by using torch.squeeze(input, dim, out) which will return a tensor, with all the dimensions of input of size 1 removed. For e.g, if the input tensor is of shape A×1×B×C×1×D then the out tensor will be of shape A×B×C×D. ...
🌐
Codecademy
codecademy.com › docs › pytorch › tensor operations › .squeeze()
PyTorch | Tensor Operations | .squeeze() | Codecademy
November 25, 2024 - The PyTorch .squeeze() function removes dimensions of size 1 from a given tensor.
🌐
DEV Community
dev.to › hyperkai › squeeze-in-pytorch-nkl
squeeze in PyTorch - DEV Community
December 30, 2024 - 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: ... The 1st argument(input) with torch or ...
🌐
Sprintchase
sprintchase.com › home › how to squeeze and unsqueeze a tensor in pytorch
How to Squeeze and Unsqueeze a Tensor in PyTorch
May 21, 2025 - Squeezing a tensor means removing a single dimension. Unsqueezing a tensor means adding a single dimension at specified index.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-squeeze-and-unsqueeze-a-tensor-in-pytorch
How to squeeze and unsqueeze a tensor in PyTorch? - GeeksforGeeks
May 23, 2022 - The elements of the original tensor are arranged with the remaining dimensions. For example, if the input tensor is of shape: (m×1×n×1) then the output tensor after squeeze will be of shape: (m×n). The following is the syntax of the torch.squeeze() method.
Find elsewhere
🌐
YouTube
youtube.com › watch
Squeezing and Unsqueezing Tensors in PyTorch - YouTube
Let's squeeze and unsqueeze tensors in PyTorch
Published: October 26, 2023
🌐
EDUCBA
educba.com › home › software development › software development tutorials › pytorch tutorial › pytorch squeeze
PyTorch Squeeze | Overviews and PyTorch Squeeze Function Code
April 7, 2023 - Whenever the input is of shape: ... to the shape (X × Y) (X \times Y) (X×Y). ... torch.squeeze() eliminates each of the 1-dimensional parts from a tensor, view and permute are slightly unique....
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
TutorialsPoint
tutorialspoint.com › how-to-squeeze-and-unsqueeze-a-tensor-in-pytorch
How to squeeze and unsqueeze a tensor in PyTorch?
November 6, 2021 - The squeeze operation removes dimensions of size 1, while unsqueeze adds new dimensions of size 1 at specified positions. The torch.squeeze() method removes all dimensions of size 1 from a tensor.
🌐
DEV Community
dev.to › balapriya › useful-tensor-manipulation-functions-in-pytorch-4g4c
Useful Tensor Manipulation Functions in PyTorch - DEV Community
December 28, 2022 - In essence, squeeze function helps remove all dimensions of size 1 or along a specific dimension. ... Here, dim denotes the index at which we want the dimension of size 1 to be inserted.
🌐
Medium
medium.com › dejunhuang › learning-day-6-pytorch-unsqueeze-squeeze-transpose-and-broadcasting-88e75dc8facb
Learning Day 6: Pytorch unsqueeze, squeeze, transpose and broadcasting | by De Jun Huang | dejunhuang | Medium
April 24, 2021 - For the purpose of systematic learning, I will write down my daily learning notes on data science and machine learning ... import torch b = torch.rand(32) # bias print(b.shape) f = torch.rand(4, 32, 14, 14) # 4 images 32 channels, b = b.unsqueeze(0).unsqueeze(2).unsqueeze(3) # need to match f's dimension print(b) print(b.shape) It is easier to understand as compared to unsqueeze. It will just eliminate the dimension at the specific index if dim=1. If not, it won’t change anything: print(b.squeeze().shape) # eliminate all dim=1 print(b.squeeze(0).shape) # eliminate at specific dimension print(b.squeeze(-1).shape) print(b.squeeze(1).shape) print(b.squeeze(-4).shape)
🌐
Clay-Technology World
clay-atlas.com › home › [pytorch] how to use `squeeze()` and `unsqueeze()` functions
[PyTorch] How To Use `squeeze()` and `unsqueeze()` Functions - Clay-Technology World
August 7, 2024 - In fact, the use method of squeeze() and unsqueeze() functions is very easy-to-know: squeeze() can remove dimensions, and unsqueeze() can increase dimensions, just like squeeze and unsqueeze.
🌐
PyTorch Forums
discuss.pytorch.org › t › numpy-or-pytroch-squeeze › 135451
Numpy or pytroch squeeze - PyTorch Forums
October 30, 2021 - The use case explains itself, so whenever you would need to remove a size 1 dimension, you could use squeeze. E.g. if you are dealing with a single sample somewhere in your code and would like to remove the batch size in order to plot the image tensor via plt.imshow
🌐
Reddit
reddit.com › r/pytorch › hot take: never use squeeze
r/pytorch on Reddit: Hot take: never use squeeze
December 7, 2024 -

Idk if I if I am misunderstanding something, but torch.squeeze just seems like a less transparent alternative to getting a view via indexing into 0 elements. Just had to a fix a bug caused by squeeze getting called on a tensor with dynamic size along a dimension, that would occasionally be 1.

🌐
DeepLizard
deeplizard.com › learn › video › fCVuiW9AFzY
Flatten, Reshape, and Squeeze Explained - Tensors for Deep Learning with PyTorch - deeplizard
Squeezing a tensor removes the dimensions or axes that have a length of one. Unsqueezing a tensor adds a dimension with a length of one. These functions allow us to expand or shrink the rank (number of dimensions) of our tensor.
🌐
Ducat India
tutorials.ducatindia.com › pytorch › pytorch-squeeze
PyTorch Squeeze - Tutorial - DUCAT
Learn Python, Java, HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap and more with Ducat Tutorials. Up-to-date guides on top programming languages and tech trends.
🌐
ProjectPro
projectpro.io › recipes › unsqueeze-torch-tensor
How To Unsqueeze A Torch Tensor? -
December 29, 2023 - The difference between torch.unsqueeze() and torch.squeeze() is that torch.unsqueeze() adds a new dimension of size 1 to a tensor at the specified position, whereas torch.squeeze() removes dimensions of size 1 from a tensor.