Batch Normalization
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.


Step-by-Step Walkthrough
[1] Given
- A mini-batch of training examples, each represented with features (arranged as columns in the visual diagram, so each row corresponds to one feature dimension across the examples).
[2] Linear Layer
- Multiply the input features by the layer weights and add biases to produce pre-activation features.
[3] ReLU
- Apply , suppressing negative values (e.g., ).
- (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 examples in the mini-batch), compute the mini-batch mean and variance:
- Take the square root (with a small constant for numerical stability) to obtain .
[5] Center to Mean = 0
- Subtract the per-feature mini-batch mean from each activation value across the 4 examples so that every feature dimension averages to .
[6] Scale to Unit Variance
- Divide the centered activations by :
- This scales each feature dimension across the mini-batch to approximately unit variance () while preventing division by zero when .
[7] Learnable Scale & Shift ( and )
- Apply a per-feature affine transformation using learnable parameters (scale) and (shift):
- In the spreadsheet visual, this elementwise scale-and-shift is written in augmented matrix form: the diagonal entries hold the scale parameters for each feature, and the final bias column holds the shift parameters .
- This allows the network to learn the optimal mean and variance for the next layer—or even recover the identity mapping () if normalization is unnecessary.
Training vs. Inference
- Training: Mean and variance are computed dynamically from the current mini-batch, and running exponential moving averages () are updated in the background.
- Inference (
evalmode): 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.