0
0
PyTorchml~5 mins

Flatten layer in PyTorch - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
In PyTorch, which class is used to add a Flatten layer?
Ann.Flat
Bnn.Reshape
Cnn.Flatten
Dnn.Vectorize
Why is flattening necessary before a fully connected layer?
ABecause fully connected layers require 1D input vectors
BTo reduce batch size
CTo increase the number of channels
DTo normalize the input
If input shape is (batch_size, 3, 32, 32), what will be the shape after Flatten?
A(batch_size, 3*32*32)
B(3, 32, 32)
C(batch_size, 32, 32)
D(batch_size, 3, 32)
Does Flatten layer change the batch size dimension?
AYes, it doubles the batch size
BNo, batch size stays the same
CYes, it halves the batch size
DYes, it removes the batch size
Explain in your own words what a Flatten layer does and why it is used in neural networks.
Think about how images are prepared before classification.
You got /4 concepts.
    Write a simple PyTorch model snippet that includes a Flatten layer followed by a linear layer.
    Use nn.Sequential for simplicity.
    You got /3 concepts.