Challenge - 5 Problems
Tensor Shape Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that permute rearranges the dimensions in the order you specify.
✗ Incorrect
The original tensor
x has shape (4, 3, 2). The permute(1, 0, 2) swaps the first and second dimensions, so the new shape is (3, 4, 2).❓ Model Choice
intermediate2: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?
Attempts:
2 left
💡 Hint
PyTorch expects image tensors in the format (batch_size, channels, height, width).
✗ Incorrect
PyTorch convolution layers expect input tensors shaped as (batch_size, channels, height, width). For 16 images with 3 color channels and 64x64 pixels, the shape is (16, 3, 64, 64).
❓ Hyperparameter
advanced2: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?
Attempts:
2 left
💡 Hint
Batch size affects the number of samples processed at once, which is the first dimension.
✗ Incorrect
The batch size corresponds to the first dimension of the input tensor. Increasing batch size from 32 to 64 doubles this dimension, while other dimensions (channels, height, width) remain unchanged.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the total number of elements before and after reshape.
✗ Incorrect
The original tensor has 2*3*4 = 24 elements. The requested shape (3, 7) requires 3*7 = 21 elements, which does not match. PyTorch's `view()` requires the total number of elements to be the same.
🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Broadcasting expands dimensions of size 1 to match the other tensor's size.
✗ Incorrect
Broadcasting rules expand dimensions with size 1 to match the other tensor's size. Here, (5,1,4) and (1,3,4) broadcast to (5,3,4).