Challenge - 5 Problems
Reshaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the shape of tensor after view operation?
Given the following PyTorch code, what will be the shape of tensor `b` after the `view` operation?
PyTorch
import torch a = torch.randn(2, 3, 4) b = a.view(3, 8)
Attempts:
2 left
💡 Hint
Remember that view reshapes the tensor without changing the total number of elements.
✗ Incorrect
The original tensor has shape (2, 3, 4), total elements = 2*3*4 = 24. The view reshapes it to (3, 8), which also has 24 elements.
❓ Predict Output
intermediate2:00remaining
What is the output shape after squeeze operation?
What will be the shape of tensor `b` after applying `squeeze` on tensor `a`?
PyTorch
import torch a = torch.randn(1, 5, 1, 3) b = a.squeeze()
Attempts:
2 left
💡 Hint
Squeeze removes all dimensions of size 1.
✗ Incorrect
The original shape is (1, 5, 1, 3). Squeeze removes dimensions with size 1, so the shape becomes (5, 3).
❓ Model Choice
advanced2:00remaining
Which operation correctly adds a new dimension at position 2?
You have a tensor `a` with shape (4, 5). Which PyTorch operation will add a new dimension at position 2 (0-based index), resulting in shape (4, 5, 1)?
PyTorch
import torch a = torch.randn(4, 5)
Attempts:
2 left
💡 Hint
Remember that unsqueeze adds a dimension at the specified index.
✗ Incorrect
The tensor `a` has shape (4, 5). Adding a dimension at position 2 means after the second dimension, so shape becomes (4, 5, 1). `a.unsqueeze(2)` does this. `a.unsqueeze(-2)` inserts at position 1, giving (4, 1, 5).
❓ Hyperparameter
advanced2:00remaining
Choosing correct reshape parameters for a tensor
You have a tensor `a` with shape (2, 3, 4). You want to reshape it to a 2D tensor with 6 rows. Which of the following reshape calls achieves this without raising an error?
PyTorch
import torch a = torch.randn(2, 3, 4)
Attempts:
2 left
💡 Hint
The total number of elements must remain the same after reshape. Use -1 to infer a dimension.
✗ Incorrect
Tensor `a` has 24 elements. A: (3,8) has 6 rows? No. B: (6,-1) infers 4, (6,4)=24, good. C: (6,5)=30 mismatch, error. D: (4,6)=24 but 4 rows.
🔧 Debug
expert2:00remaining
Why does this reshape code raise an error?
Consider the following code snippet. Why does it raise an error?
PyTorch
import torch a = torch.randn(2, 3, 4) b = a.view(4, 3, 3)
Attempts:
2 left
💡 Hint
Check the total number of elements in both shapes.
✗ Incorrect
Original tensor has 2*3*4=24 elements. New shape (4,3,3) has 4*3*3=36 elements. Total number of elements does not match.