Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.squeeze

torch.squeeze removes dimensions of size 1 from a tensor. Supplying dim limits removal to the selected dimensions.

Since
2.0 (tuple support for dim)

How it works

torch.squeeze returns a tensor formed by removing dimensions of size 1 from the input. With dim specified, only those dimensions are considered; the result shares storage with the input.

Syntax

torch.squeeze(input, dim=None)

Parameters

NameTypeDescription
inputTensorThe input tensor.
dimint or tuple of intsOptional dimension or dimensions in which to squeeze the input.

Returns

A tensor with the specified singleton dimensions removed.

Key points

  • The operation removes dimensions of size 1 from the input.
  • Providing dim restricts squeezing to the listed dimension or dimensions.
  • dim is optional and accepts an integer or a tuple of integers.
  • Tuple-valued dim has been supported since PyTorch 2.0.

Common mistakes

  • The result aliases the input storage, so modifying one tensor can affect the other.
  • Calling squeeze without dim can remove every singleton dimension, potentially changing more of the shape than intended.

Example: Squeeze a selected dimension

import torch

x = torch.zeros(2, 1, 2, 1, 2)
y = torch.squeeze(x, dim=1)
print(x.shape)  # torch.Size([2, 1, 2, 1, 2])
print(y.shape)  # torch.Size([2, 2, 1, 2])

This removes only dimension 1 because its size is 1; the other singleton dimension remains.

When to use it

Normalize tensor shapes

Remove an extra batch, channel, or sequence axis introduced during preprocessing.

Targeted dimensionality reduction

Delete known singleton axes while preserving unrelated singleton dimensions.

Explicit multi-axis squeezing

Collapse several known singleton axes in one call using a tuple of dimensions.

Interview questions

QWhat does torch.squeeze do?

It removes dimensions whose size is 1. Without dim, all such dimensions are removed; with dim, only the specified dimensions are processed.

Follow-up: What happens if a specified dimension is not size 1? The dimension is not removed, so the tensor remains unchanged along that dimension.

QWhich arguments does torch.squeeze accept?

The input is a Tensor, and dim is optional. dim can be an integer or a tuple of integers; tuple support was added in version 2.0.

Follow-up: Why use a tuple? It lets one operation explicitly target several singleton dimensions instead of squeezing every singleton dimension.

QDoes torch.squeeze copy the tensor data?

The output shares storage with the input, so it is an alias rather than an independently allocated copy. Changes through either tensor can therefore affect the shared data.

Follow-up: What should be used when fresh output storage is required? torch.squeeze_copy performs the operation while creating fresh output tensors.

  • torch.Tensor.squeeze
  • torch.Tensor.squeeze_
  • torch.squeeze_copy
  • torch.unsqueeze

Source: torch.squeeze — PyTorch main documentation. Checked 2026-10-16.