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 main purpose of batch normalization in neural networks?
Batch normalization helps to stabilize and speed up training by normalizing the inputs of each layer, keeping their mean close to 0 and variance close to 1.
Click to reveal answer
intermediate
How does nn.BatchNorm1d differ from nn.BatchNorm2d in PyTorch?
nn.BatchNorm1d is used for 2D inputs like (batch_size, features), often in fully connected layers, while nn.BatchNorm2d is for 4D inputs like (batch_size, channels, height, width), used in convolutional layers.
Click to reveal answer
beginner
What are the two main parameters learned by batch normalization layers?
Batch normalization layers learn two parameters: gamma (scale) and beta (shift), which allow the network to adjust normalized outputs if needed.
Click to reveal answer
intermediate
Why is batch normalization usually turned off during model evaluation?
During evaluation, batch normalization uses running estimates of mean and variance instead of batch statistics to ensure consistent outputs for single samples.
Click to reveal answer
beginner
Show a simple PyTorch example of applying nn.BatchNorm1d to a tensor of shape (batch_size=4, features=3).
import torch
import torch.nn as nn
x = torch.randn(4, 3) # random input
batch_norm = nn.BatchNorm1d(3) # 3 features
output = batch_norm(x)
print(output)
# This normalizes each feature across the batch.
Click to reveal answer
What does batch normalization normalize in a neural network?
AThe loss function
BThe outputs of the entire network
CThe weights of the network
DThe inputs to each layer
✗ Incorrect
Batch normalization normalizes the inputs to each layer to have a stable mean and variance.
Which PyTorch class is used for batch normalization on 2D convolutional outputs?
Ann.BatchNorm1d
Bnn.LayerNorm
Cnn.BatchNorm2d
Dnn.BatchNorm3d
✗ Incorrect
nn.BatchNorm2d is designed for 4D tensors from convolutional layers with shape (batch, channels, height, width).
What are the learnable parameters in a batch normalization layer?
AGamma (scale) and beta (shift)
BWeights and biases
CMean and variance
DLearning rate and momentum
✗ Incorrect
Batch normalization learns gamma and beta to scale and shift the normalized output.
Why do we use running mean and variance during evaluation in batch normalization?
ATo avoid using batch statistics which may vary
BTo speed up training
CTo increase model complexity
DTo reduce model size
✗ Incorrect
Using running mean and variance ensures consistent normalization when batch size is 1 or during inference.
What happens if you forget to call model.eval() before evaluation when using batch normalization?
B. The input feature size (3) does not match BatchNorm1d's expected size (5)
C. BatchNorm1d cannot process random tensors
D. BatchNorm1d requires input to be 3D tensor
Solution
Step 1: Check BatchNorm1d expected feature size
BatchNorm1d(5) expects input with 5 features per sample.
Step 2: Compare input tensor shape
Input tensor shape is (10, 3), meaning 3 features per sample, which mismatches 5.
Final Answer:
The input feature size (3) does not match BatchNorm1d's expected size (5) -> Option B
Quick Check:
Feature size mismatch causes runtime error [OK]
Hint: BatchNorm feature size must match input feature dimension [OK]
Common Mistakes:
Thinking batch size causes error
Believing BatchNorm needs 3D input always
Assuming random tensors cause errors
5. You want to apply batch normalization to a convolutional layer output with shape (batch_size, 16, 32, 32). Which PyTorch batch normalization layer should you use and why?
hard
A. nn.BatchNorm1d(16), because it normalizes over 1D features
B. nn.BatchNorm(16), because it works for any input shape
C. nn.BatchNorm3d(16), because the input has 4 dimensions
D. nn.BatchNorm2d(16), because it normalizes over 2D feature maps with 16 channels
Solution
Step 1: Analyze input tensor shape
The tensor shape is (batch_size, channels=16, height=32, width=32), typical for images.
Step 2: Choose correct BatchNorm type
For 4D tensors with channels and 2D spatial dimensions, nn.BatchNorm2d is appropriate.
Final Answer:
nn.BatchNorm2d(16), because it normalizes over 2D feature maps with 16 channels -> Option D
Quick Check:
Conv output uses BatchNorm2d with channel count [OK]
Hint: Use BatchNorm2d for conv layers with 2D spatial data [OK]