Skip to content
E
Educatum

Batch Normalization

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

Batch Normalization stabilizes and accelerates deep network training by normalizing each feature dimension across a mini-batch to zero mean and unit variance, then applying learnable scale (gamma) and shift (beta) parameters. During inference, it replaces mini-batch statistics with exponential moving averages of the population mean and variance accumulated during training.

Reference Diagram & Materials
Batch Normalization matrix calculation diagram

Batch Normalization walkthrough diagram

Step-by-Step Walkthrough

[1] Given

  • A mini-batch of m=4m = 4 training examples, each represented with 33 features (arranged as columns in the visual diagram, so each row corresponds to one feature dimension across the 44 examples).

[2] Linear Layer

  • Multiply the input features by the layer weights and add biases to produce pre-activation features.

[3] ReLU

  • Apply ReLU(z)=max⁡(0,z)\text{ReLU}(z) = \max(0, z), suppressing negative values (e.g., −2→0-2 \to 0).
  • (Note: Batch Normalization can be applied either before or after non-linearities depending on the architecture; in this exercise it follows ReLU.)

[4] Batch Statistics

  • For each feature dimension (each row across the m=4m = 4 examples in the mini-batch), compute the mini-batch mean and variance: μB=1m∑i=1mxi,σB2=1m∑i=1m(xi−μB)2\mu_B = \frac{1}{m} \sum_{i=1}^{m} x_i, \qquad \sigma_B^2 = \frac{1}{m} \sum_{i=1}^{m} (x_i - \mu_B)^2
  • Take the square root (with a small constant ϵ>0\epsilon > 0 for numerical stability) to obtain σB2+ϵ\sqrt{\sigma_B^2 + \epsilon}.

[5] Center to Mean = 0

  • Subtract the per-feature mini-batch mean μB\mu_B from each activation value across the 4 examples so that every feature dimension averages to 00.

[6] Scale to Unit Variance

  • Divide the centered activations by σB2+ϵ\sqrt{\sigma_B^2 + \epsilon}: x^i=xi−μBσB2+ϵ\hat{x}_i = \frac{x_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}
  • This scales each feature dimension across the mini-batch to approximately unit variance (≈1\approx 1) while preventing division by zero when σB2=0\sigma_B^2 = 0.

[7] Learnable Scale & Shift (γ\gamma and β\beta)

  • Apply a per-feature affine transformation using learnable parameters γ\gamma (scale) and β\beta (shift): yi=γ⊙x^i+βy_i = \gamma \odot \hat{x}_i + \beta
  • In the spreadsheet visual, this elementwise scale-and-shift is written in augmented matrix form: the diagonal entries hold the scale parameters γj\gamma_j for each feature, and the final bias column holds the shift parameters βj\beta_j.
  • This allows the network to learn the optimal mean and variance for the next layer—or even recover the identity mapping (γ=σB,β=μB\gamma = \sigma_B, \beta = \mu_B) if normalization is unnecessary.

Training vs. Inference

  • Training: Mean μB\mu_B and variance σB2\sigma_B^2 are computed dynamically from the current mini-batch, and running exponential moving averages (μpop,σpop2\mu_{\text{pop}}, \sigma_{\text{pop}}^2) are updated in the background.
  • Inference (eval mode): Mini-batch statistics are replaced with the stored running mean and variance so predictions for a single input example are deterministic and independent of batch size.

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.