0
0
PytorchHow-ToBeginner · 3 min read

How to Squeeze Tensor in PyTorch: Syntax and Examples

In PyTorch, use torch.squeeze(tensor) to remove all dimensions of size 1 from a tensor. You can also specify a dimension with torch.squeeze(tensor, dim) to remove only that dimension if its size is 1.
📐

Syntax

The torch.squeeze function removes dimensions of size 1 from a tensor. It has two forms:

  • torch.squeeze(input): removes all dimensions with size 1.
  • torch.squeeze(input, dim): removes the dimension dim if its size is 1; otherwise, returns the tensor unchanged.

This helps simplify tensor shapes by eliminating unnecessary dimensions.

python
torch.squeeze(input, dim=None)
💻

Example

This example shows how to use torch.squeeze to remove all size-1 dimensions and how to remove a specific dimension.

python
import torch

# Create a tensor with shape (1, 3, 1, 5)
t = torch.randn(1, 3, 1, 5)
print(f"Original shape: {t.shape}")

# Remove all dimensions of size 1
t_squeezed = torch.squeeze(t)
print(f"Shape after squeeze(): {t_squeezed.shape}")

# Remove only dimension 2 (which has size 1)
t_squeezed_dim = torch.squeeze(t, dim=2)
print(f"Shape after squeeze(dim=2): {t_squeezed_dim.shape}")

# Trying to squeeze a dimension that is not size 1 returns the original tensor
no_change = torch.squeeze(t, dim=1)
print(f"Shape after squeeze(dim=1) (no change): {no_change.shape}")
Output
Original shape: torch.Size([1, 3, 1, 5]) Shape after squeeze(): torch.Size([3, 5]) Shape after squeeze(dim=2): torch.Size([1, 3, 5]) Shape after squeeze(dim=1) (no change): torch.Size([1, 3, 1, 5])
⚠️

Common Pitfalls

Common mistakes when using torch.squeeze include:

  • Trying to squeeze a dimension that is not size 1, which does nothing and may confuse you.
  • Assuming squeeze() changes the original tensor; it returns a new tensor instead.
  • Not checking tensor shape before squeezing, which can lead to unexpected shape changes.

Always verify the tensor shape before and after squeezing.

python
import torch

t = torch.randn(2, 3, 1)
print(f"Original shape: {t.shape}")

# Wrong: trying to squeeze dim=0 which is size 2
squeezed_wrong = torch.squeeze(t, dim=0)
print(f"Shape after wrong squeeze(dim=0): {squeezed_wrong.shape}")

# Right: squeeze dim=2 which is size 1
squeezed_right = torch.squeeze(t, dim=2)
print(f"Shape after right squeeze(dim=2): {squeezed_right.shape}")
Output
Original shape: torch.Size([2, 3, 1]) Shape after wrong squeeze(dim=0): torch.Size([2, 3, 1]) Shape after right squeeze(dim=2): torch.Size([2, 3])
📊

Quick Reference

Tips for using torch.squeeze:

  • Use torch.squeeze(tensor) to remove all size-1 dimensions.
  • Use torch.squeeze(tensor, dim) to remove a specific size-1 dimension.
  • If the specified dimension is not size 1, the tensor stays the same.
  • Check tensor shapes before and after squeezing to avoid shape errors.

Key Takeaways

Use torch.squeeze(tensor) to remove all dimensions of size 1 from a tensor.
Specify dim in torch.squeeze(tensor, dim) to remove only that dimension if its size is 1.
Squeezing a dimension that is not size 1 does not change the tensor shape.
torch.squeeze returns a new tensor; it does not modify the original tensor in place.
Always check tensor shapes before and after squeezing to avoid unexpected results.