0
0
PyTorchml~5 mins

Reshaping (view, reshape, squeeze, unsqueeze) in PyTorch

Choose your learning style9 modes available
Introduction
Reshaping lets you change the shape of your data without changing its content. This helps your model understand and process data in the right format.
When you need to flatten images into a single line before feeding them to a model.
When you want to add or remove dimensions to match model input requirements.
When you want to remove extra dimensions that don't carry information.
When you want to change the shape of a tensor to perform matrix operations.
When you want to prepare data batches with the correct shape for training.
Syntax
PyTorch
tensor.view(new_shape)
tensor.reshape(new_shape)
tensor.squeeze(dim=None)
tensor.unsqueeze(dim)
view and reshape both change shape but view requires the tensor to be contiguous in memory.
squeeze removes dimensions of size 1, unsqueeze adds a dimension of size 1 at the given position.
Examples
Change shape from (2,3,4) to (6,4) using view.
PyTorch
x = torch.randn(2, 3, 4)
y = x.view(6, 4)
Change shape from (2,3,4) to (6,4) using reshape.
PyTorch
x = torch.randn(2, 3, 4)
y = x.reshape(6, 4)
Remove all dimensions of size 1, result shape (3,4).
PyTorch
x = torch.randn(1, 3, 1, 4)
y = x.squeeze()
Add a dimension at position 0, result shape (1,3,4).
PyTorch
x = torch.randn(3, 4)
y = x.unsqueeze(0)
Sample Model
This program shows how to remove a dimension with squeeze, add one with unsqueeze, and change shapes with reshape and view. It prints shapes and tensor content to see the changes clearly.
PyTorch
import torch

# Original tensor of shape (2, 1, 3)
x = torch.tensor([[[1, 2, 3]], [[4, 5, 6]]])
print(f"Original shape: {x.shape}")

# Remove dimension of size 1
x_squeezed = x.squeeze()
print(f"After squeeze: {x_squeezed.shape}")

# Add a dimension at position 1
x_unsqueezed = x_squeezed.unsqueeze(1)
print(f"After unsqueeze at dim 1: {x_unsqueezed.shape}")

# Reshape to (3, 2)
x_reshaped = x_squeezed.reshape(3, 2)
print(f"After reshape to (3,2): {x_reshaped.shape}")

# View to (3, 2) (same as reshape here)
x_viewed = x_squeezed.view(3, 2)
print(f"After view to (3,2): {x_viewed.shape}")

# Print all tensors to verify content
print(f"x_squeezed tensor:\n{x_squeezed}")
print(f"x_unsqueezed tensor shape: {x_unsqueezed.shape}")
print(f"x_reshaped tensor:\n{x_reshaped}")
print(f"x_viewed tensor:\n{x_viewed}")
OutputSuccess
Important Notes
Use view only if the tensor is contiguous in memory; otherwise, use reshape.
Squeeze removes all dimensions of size 1 by default, or a specific dimension if given.
Unsqueeze adds a dimension of size 1 at the specified position, useful for batch or channel dimensions.
Summary
Reshaping changes the shape of tensors without changing data.
view and reshape change shape; squeeze removes size 1 dims; unsqueeze adds size 1 dims.
These operations help prepare data for models and matrix math.