0
0
PyTorchml~20 mins

Tensor shapes and dimensions in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tensor Shape Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the shape of the tensor after this operation?
Consider the following PyTorch code snippet. What is the shape of the tensor y after running this code?
PyTorch
import torch
x = torch.randn(4, 3, 2)
y = x.permute(1, 0, 2)
A(3, 4, 2)
B(4, 2, 3)
C(2, 3, 4)
D(3, 2, 4)
Attempts:
2 left
💡 Hint
Remember that permute rearranges the dimensions in the order you specify.
Model Choice
intermediate
2:00remaining
Choosing the correct input shape for a CNN
You want to build a convolutional neural network (CNN) in PyTorch to classify color images of size 64x64 pixels. Which input tensor shape should you use for a batch of 16 images?
A(64, 64, 3, 16)
B(16, 64, 64, 3)
C(16, 3, 64, 64)
D(3, 16, 64, 64)
Attempts:
2 left
💡 Hint
PyTorch expects image tensors in the format (batch_size, channels, height, width).
Hyperparameter
advanced
2:00remaining
Effect of batch size on tensor shape during training
If you change the batch size from 32 to 64 in a training loop, how does the shape of the input tensor to the model change?
AThe tensor shape remains unchanged
BAll dimensions double
CThe last dimension doubles, others stay the same
DThe first dimension doubles, other dimensions stay the same
Attempts:
2 left
💡 Hint
Batch size affects the number of samples processed at once, which is the first dimension.
🔧 Debug
advanced
2:00remaining
Why does this tensor reshape cause an error?
Given a tensor t with shape (2, 3, 4), why does the following code raise an error?
t.view(3, 7)
PyTorch
import torch
t = torch.randn(2, 3, 4)
t_reshaped = t.view(3, 7)
ABecause view cannot change the number of dimensions
BBecause the total number of elements does not match between original and new shape
CBecause 3 and 8 are not valid dimensions for any tensor
DBecause the tensor must be contiguous before calling view
Attempts:
2 left
💡 Hint
Check the total number of elements before and after reshape.
🧠 Conceptual
expert
2:00remaining
Understanding broadcasting with tensor shapes
You have two tensors: a with shape (5, 1, 4) and b with shape (1, 3, 4). What will be the shape of the result when you add a + b in PyTorch?
A(5, 3, 4)
B(5, 1, 4)
C(1, 3, 4)
D(5, 3)
Attempts:
2 left
💡 Hint
Broadcasting expands dimensions of size 1 to match the other tensor's size.