Quant Memo
Core

Batch Normalization

Batch normalization rescales the numbers flowing between layers of a neural network so each layer always sees inputs on a familiar scale, which lets you train deeper networks with larger learning rates and far less fiddling.

Prerequisites: The Multilayer Perceptron, Stochastic Gradient Descent

Train a deep network and every layer is chasing a moving target. Layer 5's inputs are layer 4's outputs, and layer 4's weights are changing on every step of training. So the distribution of numbers layer 5 has to cope with — their scale, their spread — keeps shifting under it, even though layer 5's own weights haven't changed. Each layer is like a batter trying to hit a pitch whose speed and angle change every swing, not because the batter is doing anything wrong, but because the pitcher keeps adjusting. Training becomes slow and touchy: push the learning rate up to speed things along and the whole stack can blow up.

Batch normalization fixes the pitch, not the batter. Before each layer's output is passed on, it is rescaled so it always arrives with roughly zero mean and unit spread, batch by batch, during training.

The mechanics, one symbol at a time

Take a mini-batch of mm examples and look at one number flowing through the network — say, the pre-activation score zz of a single hidden unit, computed for each of the mm examples in the batch. Batch norm standardizes it:

z^i=ziμBσB2+ϵ,yi=γz^i+β\hat{z}_i = \frac{z_i - \mu_B}{\sqrt{\sigma_B^2 + \epsilon}}, \qquad y_i = \gamma \hat{z}_i + \beta

In words: μB\mu_B is the average of zz across the batch, σB2\sigma_B^2 is the batch's variance, and ϵ\epsilon is a tiny constant added purely so you never divide by zero. The first equation says "subtract the batch's average and divide by the batch's spread" — exactly the same standardizing you'd do to a column of data before regressing on it, just done fresh at every layer, every step. The second equation says the network is then allowed to undo that standardizing if it wants to, by learning its own scale γ\gamma and shift β\beta per unit. That matters: forcing every unit's output to have exactly unit variance forever would remove information the network might need, so batch norm gives the layer a dial to stretch or shift back and hands the network the freedom to use it or not.

Worked example 1: standardizing one unit across a batch

A hidden unit produces these pre-activation scores for a batch of four training examples: z=(2,4,4,6)z = (2, 4, 4, 6).

Mean: μB=(2+4+4+6)/4=4\mu_B = (2+4+4+6)/4 = 4.

Variance: σB2=(24)2+(44)2+(44)2+(64)24=4+0+0+44=2\sigma_B^2 = \frac{(2-4)^2+(4-4)^2+(4-4)^2+(6-4)^2}{4} = \frac{4+0+0+4}{4} = 2.

Standard deviation: 2+ϵ1.414\sqrt{2 + \epsilon} \approx 1.414 (take ϵ0\epsilon \approx 0 for cleanliness).

Standardized values: z^=(241.414,441.414,441.414,641.414)=(1.41,0,0,1.41)\hat{z} = (\frac{2-4}{1.414}, \frac{4-4}{1.414}, \frac{4-4}{1.414}, \frac{6-4}{1.414}) = (-1.41, 0, 0, 1.41).

If the layer has learned γ=2\gamma = 2, β=0.5\beta = 0.5: outputs are y=2z^+0.5=(2.32,0.5,0.5,3.32)y = 2\hat{z} + 0.5 = (-2.32, 0.5, 0.5, 3.32). Same relative shape, rescaled to whatever range the network has decided is useful downstream.

Worked example 2: why this helps training

Suppose two units feed the next layer, one with raw outputs typically around 10001000 and one typically around 0.010.01 (unnormalized, because upstream weights happened to grow that way). A single learning rate has to move both. Make it big enough to nudge the second unit meaningfully and it will send the first unit into wild swings; make it small enough to be safe for the first and the second barely learns. After batch norm, both units arrive at the next layer standardized to roughly the same scale — mean near 0, spread near 1 — so one learning rate works reasonably well for both. This is the direct mechanism, not a side effect: same-scale inputs are why the network tolerates larger, more uniform learning rates and trains faster.

Function explorer
-221.1
x = 1.00f(x) = 0.731

Use the explorer above to see how sensitive a sigmoid-style unit is to the scale of its input: a steep, badly-scaled input saturates the unit near 0 or 1, where the gradient is nearly flat and learning stalls. Batch norm's whole job is to keep inputs in the responsive middle of that curve.

before: wide, off-center after: centered, unit spread
Batch norm squeezes each layer's output distribution back to a predictable, centered shape every step, instead of letting it drift as training proceeds.

What this means in practice

At test time there is no "batch" — you may be scoring one example at a time. So batch norm keeps a running average of μB\mu_B and σB2\sigma_B^2 collected during training and uses those fixed numbers at inference, rather than recomputing them per prediction. This is also why batch norm interacts badly with very small batches or with sequences of correlated batches (a common trap in time series work): a batch of size 2 gives a wildly noisy estimate of the mean and variance, which injects noise into training the model. For sequence models and small-batch financial time series, alternatives like layer normalization (normalizing across features for one example, not across examples in a batch) are usually preferred for exactly this reason.

Batch normalization standardizes each layer's inputs using the current batch's own mean and variance, then lets the network learn a scale and shift to undo that if needed. The effect is a more stable, faster-trainable network, not a change to what the network can represent.

The classic confusion: assuming batch norm behaves identically in training and in deployment. It does not — training uses the live batch statistics, inference uses stored running averages, and a mismatch (say, a live batch of oddly-distributed data, or a batch size of 1 left in "training mode" by mistake) silently produces different predictions than were validated. Always confirm the model is switched to evaluation mode before scoring live data.

Related concepts

Practice in interviews

Further reading

  • Ioffe & Szegedy, Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
  • Goodfellow, Bengio & Courville, Deep Learning, ch. 8
ShareTwitterLinkedIn