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.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. The Internal Covariate Shift Problem
  6. Batch Normalization: Forward Pass
  7. Backward Pass: Full Gradient Derivation
  8. Scale Invariance and Gradient Stabilization
  9. Inference Mode and Running Statistics
  10. Batch Normalization as a Regularizer
  11. Layer Normalization and RMSNorm
  12. Worked Examples
  13. Connection to the Broader Curriculum
  14. Common Pitfalls and Misconceptions
  15. Research Perspective
  16. Summary of Takeaways
  17. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. State the internal covariate shift hypothesis and explain how batch normalization addresses it.
  2. Derive the batch normalization forward pass for a mini-batch.
  3. Derive the complete backward pass through batch normalization, including gradients through batch statistics.
  4. Prove scale invariance: normalized activations are unchanged under wcw\mathbf{w} \to c\mathbf{w}.
  5. Explain inference-mode normalization using running mean and variance.
  6. Compare BatchNorm, LayerNorm, and RMSNorm and identify when each is appropriate.

Prerequisites


Notation

  • x(k)\mathbf{x}^{(k)} — Mini-batch of activations
  • μB,σB2\mu_B, \sigma_B^2 — Batch mean and variance
  • x^i\hat{x}_i — Normalized activation
  • γ,β\gamma, \beta — Learnable scale and shift
  • E[],Var()\mathbb{E}[\cdot], \mathrm{Var}(\cdot) — Population moments at inference

Core Intuition

Deep networks compose many layers: hl=σ(Wlhl1+bl)\mathbf{h}_l = \sigma(\mathbf{W}_l \mathbf{h}_{l-1} + \mathbf{b}_l). During training, as earlier layers update, the distribution of activations hl1\mathbf{h}_{l-1} 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

Before BNAfter BNμ=2.00 σ²=1.43μ=-0.00 σ²=1.00BN(x) = γ · (x − μ) / √(σ² + ε) + β
γ scale
1.00
β shift
0.00
Pre-activationPost-BN
Insight: BN normalizes activations to zero mean and unit variance, then rescales with learnable γ and β. This stabilizes training and allows higher learning rates.

The Internal Covariate Shift Problem

Definition 1 (Covariate Shift). Covariate shift occurs when the input distribution P(x)P(\mathbf{x}) changes between training and test time. In deep networks, the input to layer ll is the output of layer l1l-1, whose distribution changes as Wl1\mathbf{W}_{l-1} 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 z=wThz = \mathbf{w}^T \mathbf{h} have large magnitude, sigmoid/tanh activations saturate: σ(z)0|\sigma'(z)| \approx 0, causing vanishing gradients.

Proposition 2 (Scale Sensitivity). The gradient L/w\partial \mathcal{L}/\partial \mathbf{w} depends on w\|\mathbf{w}\| and h\|\mathbf{h}\|. 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 {zi}i=1B\{z_i\}_{i=1}^B for a single feature (applied independently per feature dimension):

Step 1 — Batch statistics:

μB=1Bi=1Bzi,σB2=1Bi=1B(ziμB)2.(1)\mu_B = \frac{1}{B}\sum_{i=1}^{B} z_i, \qquad \sigma_B^2 = \frac{1}{B}\sum_{i=1}^{B}(z_i - \mu_B)^2. \tag{1}

Step 2 — Normalize:

z^i=ziμBσB2+ϵ,(2)\hat{z}_i = \frac{z_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}, \tag{2}

where ϵ>0\epsilon > 0 ensures numerical stability (typically 10510^{-5}).

Step 3 — Affine transform (learnable):

yi=γz^i+β,(3)y_i = \gamma \hat{z}_i + \beta, \tag{3}

where γ\gamma (scale) and β\beta (shift) are learnable parameters per feature.

Important equation. The affine parameters γ,β\gamma, \beta 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 dd features and batch ZRB×d\mathbf{Z} \in \mathbb{R}^{B \times d}, normalization is applied column-wise (across the batch dimension for each feature).


Backward Pass: Full Gradient Derivation

The backward pass is non-trivial because μB\mu_B and σB2\sigma_B^2 depend on all batch elements.

Setup. Let LL be the loss, gi=L/yig_i = \partial L / \partial y_i the upstream gradient, and we seek L/zi\partial L / \partial z_i.

Step 1 — Gradients of affine parameters:

Lγ=i=1Bgiz^i,Lβ=i=1Bgi.(4)\frac{\partial L}{\partial \gamma} = \sum_{i=1}^{B} g_i \hat{z}_i, \qquad \frac{\partial L}{\partial \beta} = \sum_{i=1}^{B} g_i. \tag{4}

Step 2 — Gradient w.r.t. normalized activations:

Lz^i=giγ.(5)\frac{\partial L}{\partial \hat{z}_i} = g_i \gamma. \tag{5}

Denote gˉi=giγ\bar{g}_i = g_i \gamma.

Step 3 — Gradients through normalization. Since z^i=(ziμB)/σ\hat{z}_i = (z_i - \mu_B)/\sigma where σ=σB2+ϵ\sigma = \sqrt{\sigma_B^2 + \epsilon}:

LσB2=i=1Bgˉi(ziμB)(12)σ3,(6)\frac{\partial L}{\partial \sigma_B^2} = \sum_{i=1}^{B} \bar{g}_i (z_i - \mu_B) \cdot \left(-\frac{1}{2}\right)\sigma^{-3}, \tag{6} LμB=i=1Bgˉi(1σ)+LσB22Bi=1B(ziμB).(7)\frac{\partial L}{\partial \mu_B} = \sum_{i=1}^{B} \bar{g}_i \cdot \left(-\frac{1}{\sigma}\right) + \frac{\partial L}{\partial \sigma_B^2} \cdot \frac{-2}{B}\sum_{i=1}^{B}(z_i - \mu_B). \tag{7}

The second term vanishes because i(ziμB)=0\sum_i (z_i - \mu_B) = 0:

LμB=1σi=1Bgˉi.(8)\frac{\partial L}{\partial \mu_B} = -\frac{1}{\sigma}\sum_{i=1}^{B} \bar{g}_i. \tag{8}

Step 4 — Gradient w.r.t. pre-activations:

Lzi=gˉiσ+LσB22(ziμB)B+LμB1B.(9)\frac{\partial L}{\partial z_i} = \frac{\bar{g}_i}{\sigma} + \frac{\partial L}{\partial \sigma_B^2} \cdot \frac{2(z_i - \mu_B)}{B} + \frac{\partial L}{\partial \mu_B} \cdot \frac{1}{B}. \tag{9}

Theorem 1 (Compact Backward Formula). Defining gˉi=(L/yi)γ\bar{g}_i = (\partial L / \partial y_i)\gamma:

Lzi=1Bσ[Bgˉij=1Bgˉjz^ij=1Bgˉjz^j].(10)\frac{\partial L}{\partial z_i} = \frac{1}{B\sigma}\left[B\bar{g}_i - \sum_{j=1}^{B}\bar{g}_j - \hat{z}_i\sum_{j=1}^{B}\bar{g}_j\hat{z}_j\right]. \tag{10}

Proof. Substitute (6), (8) into (9) and simplify using jz^j=0\sum_j \hat{z}_j = 0 (normalized activations have zero mean). \blacksquare

Interpretation of (10). The gradient is centered (subtract mean gˉ\bar{g}), decorrelated (subtract component along z^i\hat{z}_i), and rescaled by 1/σ1/\sigma — mirroring the forward normalization.


Scale Invariance and Gradient Stabilization

Theorem 2 (Scale Invariance of Normalized Activations). Let z=wThz = \mathbf{w}^T \mathbf{h} and z^=(zμB)/σB\hat{z} = (z - \mu_B)/\sigma_B. Scaling wcw\mathbf{w} \to c\mathbf{w} leaves z^\hat{z} unchanged for all batch elements.

Proof. Under wcw\mathbf{w} \to c\mathbf{w}: zicziz_i \to cz_i, μBcμB\mu_B \to c\mu_B, σBcσB\sigma_B \to c\sigma_B. Thus z^i=(czicμB)/(cσB)=z^i\hat{z}_i = (cz_i - c\mu_B)/(c\sigma_B) = \hat{z}_i. \blacksquare

Corollary 1 (Weight-Independent Gradients). The gradient L/w\partial L / \partial \mathbf{w} scales as 1/w1/\|\mathbf{w}\|, 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 μB,σB2\mu_B, \sigma_B^2 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:

μrun(1m)μrun+mμB,σrun2(1m)σrun2+mσB2,(11)\mu_{\text{run}} \leftarrow (1 - m)\mu_{\text{run}} + m \mu_B, \qquad \sigma^2_{\text{run}} \leftarrow (1 - m)\sigma^2_{\text{run}} + m \sigma_B^2, \tag{11}

with momentum mm (typically 0.1).

Definition 5 (Inference Forward Pass).

y=γzμrunσrun2+ϵ+β.(12)y = \gamma \cdot \frac{z - \mu_{\text{run}}}{\sqrt{\sigma^2_{\text{run}} + \epsilon}} + \beta. \tag{12}

Proposition 3 (Inference Fusion). Equation (12) can be rewritten as a single affine transform y=γz+βy = \gamma' z + \beta' with:

γ=γσrun2+ϵ,β=βγμrunσrun2+ϵ.(13)\gamma' = \frac{\gamma}{\sqrt{\sigma^2_{\text{run}} + \epsilon}}, \qquad \beta' = \beta - \frac{\gamma \mu_{\text{run}}}{\sqrt{\sigma^2_{\text{run}} + \epsilon}}. \tag{13}

No runtime overhead beyond a single affine map per feature.


Batch Normalization as a Regularizer

Proposition 4 (Stochastic Regularization). Mini-batch statistics μB,σB2\mu_B, \sigma_B^2 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:

μ=1dj=1dzj,σ2=1dj=1d(zjμ)2,z^j=zjμσ2+ϵ.(14)\mu = \frac{1}{d}\sum_{j=1}^{d} z_j, \quad \sigma^2 = \frac{1}{d}\sum_{j=1}^{d}(z_j - \mu)^2, \quad \hat{z}_j = \frac{z_j - \mu}{\sqrt{\sigma^2 + \epsilon}}. \tag{14}

Definition 7 (RMSNorm). Root Mean Square Normalization (used in LLaMA, modern LLMs) omits mean centering:

RMS(z)=1dj=1dzj2,z^j=zjRMS(z)γj.(15)\text{RMS}(\mathbf{z}) = \sqrt{\frac{1}{d}\sum_{j=1}^{d} z_j^2}, \qquad \hat{z}_j = \frac{z_j}{\text{RMS}(\mathbf{z})} \cdot \gamma_j. \tag{15}
  • 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 B=4B = 4, pre-activations z=(2,4,6,8)z = (2, 4, 6, 8). Then μB=5\mu_B = 5, σB2=5\sigma_B^2 = 5, σ=5\sigma = \sqrt{5}. Normalized: z^=(3/5,1/5,1/5,3/5)\hat{z} = (-3/\sqrt{5},\,-1/\sqrt{5},\,1/\sqrt{5},\,3/\sqrt{5}).

Example 2: Scale Invariance

If w2w\mathbf{w} \to 2\mathbf{w}, all ziz_i double, but z^i\hat{z}_i unchanged. The network's normalized representation is invariant to weight scaling.

Example 3: Inference vs. Training

At training with batch {1,3,5,7}\{1, 3, 5, 7\}: μB=4\mu_B = 4. At inference with single sample z=5z = 5: use μrun\mu_{\text{run}} accumulated over training, not μB=5\mu_B = 5.


Connection to the Broader Curriculum


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 B=1B = 1 or B=2B = 2; 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 γ,β\gamma, \beta 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

  • Forwardz^=(zμB)/σB\hat{z} = (z - \mu_B)/\sigma_B — Normalize activations
  • Affiney=γz^+βy = \gamma\hat{z} + \beta — Restore capacity
  • Backward — (10) — Gradients through statistics
  • Scale invariancez^\hat{z} invariant to  —w —\ — \mathbf{w}\ — — Stable training
  • Inference — Running μrun,σrun2\mu_{\text{run}}, \sigma^2_{\text{run}} — 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 z=Wh\mathbf{z} = \mathbf{W}\mathbf{h}.

Exercise 3. Show that iz^i=0\sum_i \hat{z}_i = 0 and iz^i2=B\sum_i \hat{z}_i^2 = B (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)?