Batch Normalization
Volume II, Chapter 7 — Part I. Derivation of batch normalization: forward pass, complete backward pass, scale invariance, internal covariate shift, inference with running statistics, and comparison with LayerNorm and RMSNorm.
Prerequisites
Table of Contents
- Learning Objectives
- Prerequisites
- Notation
- Core Intuition
- The Internal Covariate Shift Problem
- Batch Normalization: Forward Pass
- Backward Pass: Full Gradient Derivation
- Scale Invariance and Gradient Stabilization
- Inference Mode and Running Statistics
- Batch Normalization as a Regularizer
- Layer Normalization and RMSNorm
- Worked Examples
- Connection to the Broader Curriculum
- Common Pitfalls and Misconceptions
- Research Perspective
- Summary of Takeaways
- Exercises
Learning Objectives
After reading this chapter, you should be able to:
- State the internal covariate shift hypothesis and explain how batch normalization addresses it.
- Derive the batch normalization forward pass for a mini-batch.
- Derive the complete backward pass through batch normalization, including gradients through batch statistics.
- Prove scale invariance: normalized activations are unchanged under .
- Explain inference-mode normalization using running mean and variance.
- Compare BatchNorm, LayerNorm, and RMSNorm and identify when each is appropriate.
Prerequisites
- Backpropagation — chain rule through computation graphs
- Multivariate Gaussian — mean, variance (optional)
Notation
- — Mini-batch of activations
- — Batch mean and variance
- — Normalized activation
- — Learnable scale and shift
- — Population moments at inference
Core Intuition
Deep networks compose many layers: . During training, as earlier layers update, the distribution of activations shifts — each layer must continuously adapt to a moving input distribution. This internal covariate shift (Ioffe & Szegedy, 2015) slows training and can saturate activation functions, killing gradients.
Batch Normalization (BatchNorm) normalizes each layer's pre-activations to zero mean and unit variance within each mini-batch, then applies learnable scale and shift. This stabilizes the distribution of layer inputs, enables higher learning rates, and acts as a mild regularizer.
Modern Transformers use Layer Normalization instead (normalize across features, not batch), but BatchNorm remains essential for CNNs and provides the conceptual foundation for all normalization methods.
Series context. Chapter 7 (Training Deep Networks), Part I in Volume II.
Batch Normalization
The Internal Covariate Shift Problem
Definition 1 (Covariate Shift). Covariate shift occurs when the input distribution changes between training and test time. In deep networks, the input to layer is the output of layer , whose distribution changes as updates during training.
Definition 2 (Internal Covariate Shift). The change in the distribution of layer inputs during training, caused by updates to preceding layers, is internal covariate shift.
Proposition 1 (Saturation). If pre-activations have large magnitude, sigmoid/tanh activations saturate: , causing vanishing gradients.
Proposition 2 (Scale Sensitivity). The gradient depends on and . Large weight norms amplify gradient magnitudes, requiring careful learning rate tuning.
BatchNorm addresses both by ensuring normalized, well-conditioned inputs to each layer.
Batch Normalization: Forward Pass
Definition 3 (Batch Normalization). Given a mini-batch of pre-activations for a single feature (applied independently per feature dimension):
Step 1 — Batch statistics:
Step 2 — Normalize:
where ensures numerical stability (typically ).
Step 3 — Affine transform (learnable):
where (scale) and (shift) are learnable parameters per feature.
Important equation. The affine parameters allow the network to undo normalization if the optimal representation requires non-zero mean or non-unit variance. Without them, BatchNorm would constrain representational capacity.
Vector form. For a layer with features and batch , normalization is applied column-wise (across the batch dimension for each feature).
Backward Pass: Full Gradient Derivation
The backward pass is non-trivial because and depend on all batch elements.
Setup. Let be the loss, the upstream gradient, and we seek .
Step 1 — Gradients of affine parameters:
Step 2 — Gradient w.r.t. normalized activations:
Denote .
Step 3 — Gradients through normalization. Since where :
The second term vanishes because :
Step 4 — Gradient w.r.t. pre-activations:
Theorem 1 (Compact Backward Formula). Defining :
Proof. Substitute (6), (8) into (9) and simplify using (normalized activations have zero mean).
Interpretation of (10). The gradient is centered (subtract mean ), decorrelated (subtract component along ), and rescaled by — mirroring the forward normalization.
Scale Invariance and Gradient Stabilization
Theorem 2 (Scale Invariance of Normalized Activations). Let and . Scaling leaves unchanged for all batch elements.
Proof. Under : , , . Thus .
Corollary 1 (Weight-Independent Gradients). The gradient scales as , making effective learning rates approximately independent of weight magnitude.
This provides automatic learning rate adaptation — a key reason BatchNorm enables training with larger learning rates.
Inference Mode and Running Statistics
During training, batch statistics depend on the mini-batch. At inference, batches may be size 1 or have different composition.
Definition 4 (Running Statistics). During training, maintain exponential moving averages:
with momentum (typically 0.1).
Definition 5 (Inference Forward Pass).
Proposition 3 (Inference Fusion). Equation (12) can be rewritten as a single affine transform with:
No runtime overhead beyond a single affine map per feature.
Batch Normalization as a Regularizer
Proposition 4 (Stochastic Regularization). Mini-batch statistics are random (depend on batch composition). This injects noise into the forward pass, acting as a regularizer similar to dropout.
Proposition 5 (Batch Size Dependence). Small batch sizes yield noisier statistics, increasing regularization strength. Large batches yield more stable estimates, reducing the regularization effect.
This explains why BatchNorm performance can degrade with very large batch sizes unless learning rate is adjusted.
Layer Normalization and RMSNorm
Definition 6 (Layer Normalization). Normalize across features for each sample independently:
Definition 7 (RMSNorm). Root Mean Square Normalization (used in LLaMA, modern LLMs) omits mean centering:
- Normalizes over — Batch — Features — Features
- Batch size dependence — Yes — No — No
- Mean subtraction — Yes — Yes — No
- Used in — CNNs — Transformers — Modern LLMs
Transformers prefer LayerNorm/RMSNorm because: (1) variable-length sequences make batch statistics unreliable; (2) inference with batch size 1; (3) no cross-sample dependency.
See Self-Attention and RoPE for normalization in Transformer blocks.
Worked Examples
Example 1: Forward Pass
Batch , pre-activations . Then , , . Normalized: .
Example 2: Scale Invariance
If , all double, but unchanged. The network's normalized representation is invariant to weight scaling.
Example 3: Inference vs. Training
At training with batch : . At inference with single sample : use accumulated over training, not .
Connection to the Broader Curriculum
- Backpropagation — gradients through BatchNorm nodes
- Self-Attention — Pre-LN and Post-LN Transformer architectures
- Flash Attention — orthogonal optimization axis
Common Pitfalls and Misconceptions
Pitfall 1: Using batch statistics at inference. Always switch to running statistics (11) for evaluation.
Pitfall 2: Very small batch sizes. Statistics are unreliable for or ; use GroupNorm or LayerNorm instead.
Pitfall 3: BatchNorm in RNNs. Hidden state statistics vary across time steps; LayerNorm is preferred.
Pitfall 4: Assuming BatchNorm always helps. With sufficient data and careful initialization, benefits diminish. Residual connections and better optimizers reduce the need.
Pitfall 5: Confusing with mean and variance. They are learnable rescaling parameters, not the batch statistics.
Research Perspective
BatchNorm (Ioffe & Szegedy, 2015) enabled training of networks 10× deeper. Subsequent work questioned the internal covariate shift explanation (Santurkar et al., 2018), showing BatchNorm primarily improves the Lipschitz constant of the loss landscape.
LayerNorm (Ba et al., 2016) adapted normalization for sequence models. RMSNorm (Zhang & Sennrich, 2019) simplified LayerNorm for LLMs. Post-Norm vs. Pre-Norm Transformer architectures remain an active research area.
Summary of Takeaways
- Forward — — Normalize activations
- Affine — — Restore capacity
- Backward — (10) — Gradients through statistics
- Scale invariance — invariant to — Stable training
- Inference — Running — Batch-independent eval
- LayerNorm — Normalize over features — Transformers
Next article: Self-Attention →
Exercises
Exercise 1. Derive (10) from (9) without skipping algebraic steps.
Exercise 2. Prove Theorem 2 (scale invariance) for vector inputs .
Exercise 3. Show that and (before affine transform).
Exercise 4. Compare gradient variance with and without BatchNorm for a deep linear network.
Exercise 5. Derive the inference fusion formulas (13).
Exercise 6. Explain why LayerNorm is preferred over BatchNorm for autoregressive language modeling.
Exercise 7. For RMSNorm (15), derive the backward pass (simpler than BatchNorm — no mean term).
Exercise 8 (Conceptual). BatchNorm creates dependency between samples in a batch. Why is this problematic for certain training regimes (e.g., small batches, reinforcement learning)?