Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of a Flatten layer in a neural network?
A Flatten layer converts a multi-dimensional input (like an image) into a one-dimensional vector so it can be fed into a fully connected layer.
Click to reveal answer
beginner
How does the Flatten layer affect the shape of the input tensor?
It reshapes the input tensor from shape (batch_size, channels, height, width) to (batch_size, channels * height * width), keeping the batch size the same.
Click to reveal answer
beginner
Show a simple PyTorch code snippet to add a Flatten layer in a model.
import torch.nn as nn
model = nn.Sequential(
nn.Flatten(),
nn.Linear(28*28, 10)
)
Click to reveal answer
beginner
Why do we need to flatten data before feeding it to a fully connected layer?
Fully connected layers expect 1D input vectors. Flattening changes multi-dimensional data into a 1D vector so the layer can process it.
Click to reveal answer
beginner
Can the Flatten layer change the batch size of the input?
No, the Flatten layer keeps the batch size unchanged. It only reshapes the other dimensions into one dimension.
Click to reveal answer
What does the Flatten layer do to the input tensor?
ANormalizes the data
BChanges the batch size
CAdds more dimensions
DConverts it to a 1D vector per sample
✗ Incorrect
The Flatten layer reshapes each sample into a 1D vector without changing batch size.
In PyTorch, which class is used to add a Flatten layer?
Ann.Flat
Bnn.Reshape
Cnn.Flatten
Dnn.Vectorize
✗ Incorrect
PyTorch provides nn.Flatten to flatten input tensors.
Why is flattening necessary before a fully connected layer?
Flatten output shape = (batch, product of other dims) [OK]
Hint: Multiply all dims except batch for flattened size [OK]
Common Mistakes:
Forgetting to keep batch size dimension
Using original shape without flattening
Dropping batch dimension by mistake
4. Given the code below, what is the error and how to fix it?
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Conv2d(1, 10, kernel_size=3),
nn.Flatten(start_dim=0),
nn.Linear(10*26*26, 100)
)
medium
A. Conv2d output channels must match Linear input features
B. Flatten start_dim=0 flattens batch dimension; use start_dim=1 instead
C. Linear input size is incorrect; should be 10*28*28
D. Missing activation function after Conv2d
Solution
Step 1: Identify Flatten usage error
Using start_dim=0 flattens batch dimension, which breaks batch processing.
Step 2: Correct Flatten start_dim
Change start_dim=0 to start_dim=1 to keep batch size intact and flatten only feature dims.
Final Answer:
Flatten start_dim=0 flattens batch dimension; use start_dim=1 instead -> Option B
Quick Check:
Flatten start_dim=1 keeps batch size [OK]
Hint: Never flatten batch dimension; start_dim=1 keeps batch [OK]
Common Mistakes:
Setting start_dim=0 flattens batch dimension
Ignoring shape mismatch errors in Linear layer
Assuming activation functions fix shape errors
5. You have a batch of images with shape (32, 3, 64, 64). You want to connect a convolutional network to a fully connected layer. Which PyTorch code correctly flattens the output before the dense layer?
hard
A. nn.Sequential(nn.Conv2d(3, 16, 3), nn.Flatten(start_dim=1), nn.Linear(16*62*62, 128))
B. nn.Sequential(nn.Conv2d(3, 16, 3), nn.Flatten(start_dim=0), nn.Linear(16*62*62, 128))
C. nn.Sequential(nn.Conv2d(3, 16, 3), nn.Flatten(), nn.Linear(3*64*64, 128))
D. nn.Sequential(nn.Conv2d(3, 16, 3), nn.Flatten(start_dim=1), nn.Linear(3*64*64, 128))
Solution
Step 1: Calculate output shape after Conv2d
Conv2d with kernel_size=3 reduces each spatial dim by 2: 64 -> 62. Output shape: (32, 16, 62, 62).
Step 2: Flatten correctly and match Linear input
Flatten with start_dim=1 keeps batch size 32 and flattens (16*62*62). Linear input features must match this product.
Final Answer:
nn.Sequential(nn.Conv2d(3, 16, 3), nn.Flatten(start_dim=1), nn.Linear(16*62*62, 128)) -> Option A
Quick Check:
Flatten start_dim=1 + correct Linear input size [OK]
Hint: Calculate Conv output size, flatten from dim=1, match Linear input [OK]