Compare batch norm, layer norm, and group norm — when do you use each?
Written by the mlprep team · Updated
Compare batch normalization, layer normalization, and group normalization. When would you use each, and why did transformers switch from batch norm to layer norm?
The problem all normalization solves#
Deep networks suffer from unstable training: as parameters update, the distribution of each layer's input shifts, forcing later layers to constantly adapt. Normalizing activations stabilizes these distributions, enables higher learning rates, and reduces sensitivity to initialization.
Batch Normalization (BatchNorm)#
Normalizes across the batch dimension for each feature:
For each feature j:
μ_j = (1/N) ∑_i x_ij (batch mean)
σ²_j = (1/N) ∑_i (x_ij - μ_j)²
x̂_ij = (x_ij - μ_j) / √(σ²_j + ε)
y_ij = γ_j · x̂_ij + β_j (learned scale and shift)
Where N is the batch size. The statistics are computed across examples in the batch.
Strengths: very effective regularizer (batch statistics introduce noise), enables high learning rates, greatly accelerated early DL training.
Weaknesses:
- Batch size dependent: with small batches (fewer than 8-16), statistics are noisy and training destabilizes
- Inference vs training mismatch: at inference, uses running averages of μ and σ estimated during training — can mismatch if distribution shifts
- Fails for variable-length sequences: you can't batch-normalize across positions in NLP — each position would have different statistics
- Doesn't work for RNNs: the "batch" at each timestep varies
Layer Normalization (LayerNorm)#
Normalizes across the feature dimension for each example:
For each example i:
μ_i = (1/H) ∑_j x_ij (feature mean for this example)
σ²_i = (1/H) ∑_j (x_ij - μ_i)²
x̂_ij = (x_ij - μ_i) / √(σ²_i + ε)
No dependence on batch size — statistics are computed per example. Identical behavior at train and inference time. Works for any sequence length.
Why transformers use it: attention operates on sequences of varying length, batch sizes may be small, and training-inference consistency is critical. LayerNorm became the default for all transformer variants.
Often applied before the sublayer (Pre-LN) rather than after (Post-LN from original transformer) — Pre-LN gives more stable training gradients.
Group Normalization (GroupNorm)#
Normalizes across groups of channels for each example (compromise between batch and layer norm):
Divide C channels into G groups.
Normalize within each group across spatial dimensions and the group's channels.
Designed for computer vision with small batch sizes (e.g., object detection with 1-2 images per GPU). Instance Norm (G=C) and Layer Norm (G=1) are special cases.
Works well when: batch sizes are forced to be small by memory constraints (large image resolution), and you're doing dense prediction (segmentation, detection).
Quick selection guide#
| Scenario | Normalization | |----------|--------------| | CNNs, large batches | Batch Norm | | CNNs, small batches | Group Norm | | Transformers / NLP | Layer Norm | | RNNs | Layer Norm | | Generative models (GANs) | Instance Norm or Layer Norm |
The original BatchNorm paper claim vs reality#
The paper claimed BatchNorm works by reducing "internal covariate shift." Later research showed this explanation isn't quite right — BatchNorm improves training by smoothing the loss landscape (making it more Lipschitz), which allows larger, more confident gradient steps. The mechanism is different but the empirical benefits are real.
Senior/staff considerations#
Normalization choice is about which statistics are stable and available. BatchNorm works well when batch statistics are meaningful, batch size is large enough, and train-serving distribution is stable. LayerNorm and RMSNorm work well when each example must be normalized independently, which is why they fit autoregressive language models and variable-length sequences.
In production and large-scale training, mention:
- Small per-device batches make BatchNorm noisy; SyncBatchNorm can recover global batch statistics but adds communication cost.
- BatchNorm couples examples in a batch, which can complicate distributed training, privacy-sensitive settings, and online inference.
- Transformers commonly use Pre-LN or RMSNorm variants for stability at depth; RMSNorm drops mean-centering and keeps the rescaling effect.
- Normalization interacts with residual initialization, learning rate, weight decay, dropout, and mixed precision. It is not an isolated layer choice.
- For transfer learning, BatchNorm running statistics can be a source of silent degradation when the target distribution differs from pretraining.
Common traps#
- Saying BatchNorm is obsolete. It is still strong for many CNN workloads.
- Saying LayerNorm is always better. It removes batch dependence but does not provide the same batch-statistic regularization.
- Ignoring train/inference behavior. BatchNorm uses running stats at inference; LayerNorm computes per-example stats both times.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
BatchNorm: normalize across batch (per feature). Effective for CNNs, large batches; fails with small batches and variable-length sequences, train-inference mismatch. LayerNorm: normalize across features (per example). Batch-size-independent, train=inference behavior — default for transformers and NLP. GroupNorm: normalize across channel groups per example — for CV with small batches. Pick BatchNorm for large-batch CV, LayerNorm for transformers, GroupNorm for small-batch detection.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- Pre-layer norm vs post-layer norm in transformers — what's the difference and why does it matter for training stability?
- Why does batch norm act as a regularizer, and how does this interact with dropout?
- In what scenarios would you use RMS Norm instead of Layer Norm, and what does it drop from LayerNorm?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions