Bird
Raised Fist0
PyTorchml~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the torch.squeeze() function do to a tensor?
easy
A. Adds a new dimension of size 1 at a specified position
B. Removes all dimensions of size 1 from the tensor
C. Changes the tensor's data without changing its shape
D. Flattens the tensor into a 1D vector

Solution

  1. Step 1: Understand the purpose of squeeze

    torch.squeeze() removes dimensions that have size 1, effectively reducing unnecessary dimensions.
  2. Step 2: Differentiate from other reshape functions

    Unlike unsqueeze which adds size 1 dims, squeeze removes them without changing data.
  3. Final Answer:

    Removes all dimensions of size 1 from the tensor -> Option B
  4. Quick Check:

    squeeze removes size 1 dims = B [OK]
Hint: Squeeze removes dims with size 1, like cleaning empty boxes [OK]
Common Mistakes:
  • Confusing squeeze with unsqueeze
  • Thinking squeeze changes data values
  • Assuming squeeze flattens the tensor
2. Which of the following is the correct syntax to add a new dimension at position 0 using PyTorch?
easy
A. tensor.unsqueeze(0)
B. tensor.unsqueeze(1)
C. tensor.squeeze(0)
D. tensor.reshape(0)

Solution

  1. Step 1: Recall unsqueeze syntax

    unsqueeze(dim) adds a dimension of size 1 at the specified dim index.
  2. Step 2: Identify correct dimension index

    To add at position 0 (the front), use unsqueeze(0). tensor.unsqueeze(1) adds at position 1, C squeezes, D is invalid.
  3. Final Answer:

    tensor.unsqueeze(0) -> Option A
  4. Quick Check:

    unsqueeze(0) adds dim at front = A [OK]
Hint: unsqueeze(dim) adds size 1 dim at dim index [OK]
Common Mistakes:
  • Using squeeze instead of unsqueeze
  • Passing wrong dimension index
  • Using reshape with a single integer argument
3. Given the tensor t = torch.randn(2, 1, 3), what is the shape after t.view(2, 3)?
medium
A. (2, 3)
B. (2, 1, 3)
C. (1, 6)
D. RuntimeError due to incompatible shape

Solution

  1. Step 1: Understand original tensor shape

    Tensor t has shape (2, 1, 3), total elements = 2*1*3 = 6.
  2. Step 2: Check if view(2, 3) is compatible

    New shape (2, 3) also has 6 elements, so view can reshape without error.
  3. Final Answer:

    (2, 3) -> Option A
  4. Quick Check:

    view reshapes without changing total elements = A [OK]
Hint: view reshapes if total elements match exactly [OK]
Common Mistakes:
  • Confusing view with squeeze or unsqueeze
  • Assuming view changes data order
  • Trying incompatible shapes causing runtime error
4. What is wrong with this code snippet?
import torch
t = torch.randn(3, 1, 4)
t = t.view(3, 3, 1, 1)
medium
A. The new shape has more elements than original tensor
B. view cannot add new dimensions, only reshape existing ones
C. view requires contiguous tensor, but t is not contiguous
D. The new shape is incompatible with the total number of elements

Solution

  1. Step 1: Calculate original tensor elements

    Original shape (3,1,4) has 3*1*4 = 12 elements.
  2. Step 2: Calculate new shape elements

    New shape (3,3,1,1) has 3*3*1*1 = 9 elements, which does not match.
  3. Final Answer:

    The new shape is incompatible with the total number of elements -> Option D
  4. Quick Check:

    view fails if total elements don't match = C [OK]
Hint: Check total elements before view; must match exactly [OK]
Common Mistakes:
  • Assuming view cannot add dimensions
  • Confusing contiguous memory errors
  • Miscounting total elements
5. You have a tensor x of shape (4, 1, 28, 28). You want to prepare it for a fully connected layer that expects input shape (4, 784). Which sequence of PyTorch operations correctly reshapes x without changing data order?
hard
A. x = x.view(4, 28, 28)
B. x = x.unsqueeze(1).view(4, 1, 784)
C. x = x.squeeze(1).view(4, 784)
D. x = x.reshape(4, 1, 784)

Solution

  1. Step 1: Understand original tensor shape

    x has shape (4, 1, 28, 28). The middle dimension is size 1, which is unnecessary for flattening.
  2. Step 2: Remove the size 1 dimension

    Use squeeze(1) to remove the dimension of size 1 at index 1, resulting in shape (4, 28, 28).
  3. Step 3: Flatten the last two dimensions

    Use view(4, 784) to reshape (4, 28, 28) into (4, 784) without changing data order.
  4. Final Answer:

    x = x.squeeze(1).view(4, 784) -> Option C
  5. Quick Check:

    Remove size 1 dim then flatten = D [OK]
Hint: Squeeze first to remove size 1 dims, then view to flatten [OK]
Common Mistakes:
  • Trying to view directly without squeezing
  • Using unsqueeze unnecessarily
  • Confusing reshape and view effects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard