0
0
PyTorchml~20 mins

Flatten layer in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flatten Layer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape after Flatten layer in PyTorch
Given the following PyTorch code, what is the shape of the tensor after applying the Flatten layer?
PyTorch
import torch
import torch.nn as nn

x = torch.randn(10, 3, 28, 28)  # batch of 10 images, 3 channels, 28x28 pixels
flatten = nn.Flatten()
out = flatten(x)
print(out.shape)
Atorch.Size([10, 2352])
Btorch.Size([10, 3, 28, 28])
Ctorch.Size([10, 28, 28, 3])
Dtorch.Size([10, 3, 784])
Attempts:
2 left
💡 Hint
Flatten combines all dimensions except the batch dimension into one.
Model Choice
intermediate
2:00remaining
Choosing Flatten layer position in a CNN
In a convolutional neural network (CNN), where should you place the Flatten layer?
AAfter the last convolutional or pooling layer and before the fully connected layers
BBefore the first convolutional layer
CAfter the output layer
DBetween two convolutional layers
Attempts:
2 left
💡 Hint
Flatten prepares data for fully connected layers by converting multi-dimensional data to 2D.
Hyperparameter
advanced
2:00remaining
Effect of start_dim parameter in PyTorch Flatten
What is the effect of setting start_dim=2 in nn.Flatten(start_dim=2) when applied to a tensor of shape (5, 4, 3, 2)?
AThe tensor shape remains unchanged
BThe tensor is flattened starting from dimension 1, resulting in shape (5, 24)
CThe tensor is flattened starting from dimension 0, resulting in shape (120,)
DThe tensor is flattened starting from dimension 2, resulting in shape (5, 4, 6)
Attempts:
2 left
💡 Hint
start_dim defines the first dimension to flatten from.
🔧 Debug
advanced
2:00remaining
Debugging Flatten layer usage error
What error will this PyTorch code raise and why? import torch import torch.nn as nn x = torch.randn(8, 3, 32, 32) flatten = nn.Flatten(start_dim=4) out = flatten(x)
AIndexError: start_dim must be less than or equal to the number of dimensions minus 1
BRuntimeError: input tensor must be 2D or more
CNo error, output shape is (8, 3, 32, 32)
DTypeError: Flatten() got an unexpected keyword argument 'start_dim'
Attempts:
2 left
💡 Hint
start_dim can equal input.dim(); it flattens nothing.
🧠 Conceptual
expert
2:00remaining
Why Flatten layer is crucial before fully connected layers
Why is the Flatten layer necessary before feeding data into fully connected (linear) layers in neural networks?
ABecause Flatten normalizes the data to have zero mean and unit variance
BBecause fully connected layers require 2D input: (batch_size, features), and Flatten converts multi-dimensional tensors to this shape
CBecause Flatten reduces the number of parameters in the model
DBecause Flatten applies non-linear activation to the input
Attempts:
2 left
💡 Hint
Think about the input shape requirements of linear layers.