January 2019 · Interactive Essay

Why Does Batch Normalization Work?

Reproducing Santurkar et al. live in the browser with TensorFlow.js — testing why BatchNorm accelerates training, why internal covariate shift isn’t the reason, and how it smooths the optimization landscape.

01introduction

Beyond the conventional story

Batch Normalization is a simple yet extremely effective technique that makes learning with neural networks faster and more stable. Despite its widespread adoption, the theoretical justification of BatchNorm has been vague and shaky. The belief propagating through the ML community is that BatchNorm improves optimization by reducing internal covariate shift (ICS). As we shall see, ICS has little to no effect on optimization.

This post examines the explanations of why BatchNorm works, mainly agreeing with the conclusions from How Does Batch Normalization Help Optimization? (No, It Is Not About Internal Covariate Shift) [1]. It joins the effort to make reproducibility and open source commonplace in ML by reproducing the results from [1] live in your browser using TensorFlow.js. To see the results you will train the models from scratch, made as easy as clicking a button. Parameter initialization is random, so you will observe slightly different trajectories every time you train. The original source code lives in this GitHub repo.

02mechanics

Batch Normalization, step by step

Batch Normalization is applied during training on hidden layers. It is similar to feature scaling applied to the input data, except we do not divide by the range. The idea is to track layer activations along each dimension and then subtract the mini-batch mean and divide by the standard deviation for each batch. The interactive visualization below walks through both steps.

interactive · 2d activation slice (m = 40)
Raw mini-batch activations
-218-164-109-550016324864dimension x₁dimension x₂
μ₁ -127.61
μ₂ 44.99
σ₁ 31.87
σ₂ 6.48
step 01

Subtract the mean

Batch mean:
Center:

Subtracting the batch mean shifts the cloud directly onto the origin (0, 0). Watch the axes and crosshair move.

step 02

Normalize the variance

Variance:
Scale:

Dividing by the standard deviation rescales every axis to unit variance ( prevents division by zero).

fig. 01Interactive 2D activation batch: click Step 1 to center the cloud around the origin, Step 2 to rescale both dimensions to unit variance, and then explore the learnable affine parameters γ and β.

One important detail is that Batch Normalization includes learnable parameters. After centering and normalizing, the layer applies an affine transformation:

Here and are learned parameters and is the output of the BatchNorm layer. If normalization is unhelpful for a given layer, the network can learn the identity mapping and undo it. BatchNorm also behaves differently at test time: rather than computing batch statistics on the fly, it uses the running mean and variance accumulated during training via an exponential moving average.

03empirical check

Does Batch Normalization work?

Before examining why BatchNorm works, let’s convince ourselves that it really works as well as advertised. We will train a convolutional neural network with and without BatchNorm at both a low and a high learning rate. The two architectures are shown below: on the left is a regular CNN, and on the right is the exact same network with BatchNorm inserted after each convolutional block.

Regular CNN
Baseline · 6 layers
Input28 × 28 × 1
Convolution 1 + ReLU5 × 5 · 8 filters
Max Pool 12 × 2 · stride 2 → 14 × 14 × 8
Convolution 2 + ReLU5 × 5 · 16 filters
Max Pool 22 × 2 · stride 2 → 7 × 7 × 16
Dense + Softmax784 → 10 classes
CNN + BatchNorm
Normalized · 8 layers
Input28 × 28 × 1
Convolution 1 + ReLU5 × 5 · 8 filters
Max Pool 12 × 2 · stride 2 → 14 × 14 × 8
BatchNorm 1normalize 8 channels (γ, β)
Convolution 2 + ReLU5 × 5 · 16 filters
Max Pool 22 × 2 · stride 2 → 7 × 7 × 16
BatchNorm 2normalize 16 channels (γ, β)
Dense + Softmax784 → 10 classes
fig. 02Model architectures trained in the live browser experiments below: a 2-block baseline CNN (left) vs. the identical network with channel-wise BatchNorm after each max-pool layer (right).

We train a digit classifier on the classic MNIST dataset. Click Train in browser below to run all four trainings live on your device (MNIST downloads once and stays cached for the remaining experiments).

experiment 01 · mnist live training
Regular CNN vs. CNN + BatchNorm across learning rates

Trains 4 fresh networks from scratch on MNIST (LR 0.1 and LR 0.5) and streams held-out test loss & accuracy every 2 steps.

Test Set Loss
lower is better
awaiting run
Click Train to stream live measurements
CNN (LR 0.1)
CNN + BN (LR 0.1)
CNN (LR 0.5)
CNN + BN (LR 0.5)
Test Set Accuracy
higher is better
awaiting run
Click Train to stream live measurements
CNN (LR 0.1)
CNN + BN (LR 0.1)
CNN (LR 0.5)
CNN + BN (LR 0.5)
fig. 03Evaluation of Regular CNN vs. CNN + BatchNorm on held-out MNIST test digits during SGD training at low (0.1) and high (0.5) learning rates.

Once training completes, you should see BatchNorm outperforming the baseline at both learning rates. It is common for the standard CNN without BatchNorm to stall or diverge at the higher learning rate, whereas with BatchNorm it simply trains faster.

04the hypothesis

Internal covariate shift and the error surface

According to the original BatchNorm paper [3], the technique works because it remedies Internal Covariate Shift (ICS) — the change in the distribution of hidden-layer inputs as earlier weights update during training. Concretely, the mean shifts, the variance changes, and the distribution shape drifts. Intuitively this sounds compelling: if the first layer’s activations start roughly Gaussian around one center and then drift and skew as training proceeds, the second layer must continuously adapt. Yet as Santurkar et al. [1] demonstrate, ICS has little to do with optimization performance, and BatchNorm does not even reliably reduce it.

Even before BatchNorm became mainstream, Geoffrey Hinton showed how shifting and scaling inputs reshapes the error surface. As he explained in his Neural Networks Course (Lecture 6.2 — A bag of tricks for mini-batch gradient descent), centering data around zero and normalizing scale gives each dimension similar curvature, turning an elongated, high-curvature elliptical ravine into a far more spherical basin.

Visualization of deep network loss surfaces with and without Batch Normalization from D. Jiwoong et al.
fig. 04Visualization of deep network loss surfaces without (top) and with (bottom) Batch Normalization across optimizers. Source: D. Jiwoong et al. [2].

Empirical work by D. Jiwoong et al. [2] (figure above) observed clean, unimodal transitions with Batch Normalization, whereas without BatchNorm they reported wide, bumpy surfaces that are not necessarily unimodal. They also found that without BatchNorm, optimization depends heavily on weight initialization — BatchNorm makes the network much less sensitive to its starting state.

05fundamental analysis

Lipschitzness and a smoother optimization landscape

The authors of [1] provide both empirical evidence and theoretical proof that the primary mechanism is geometric rather than distributional:

BatchNorm helps optimization by making the loss landscape significantly smoother — improving the Lipschitzness of both the loss and its gradients — rather than by reducing internal covariate shift.

Formally, a function is -Lipschitz if , and Lipschitzness of its gradients is captured by -smoothness: is -smooth if for inputs . They further show that BatchNorm is not unique in this regard: their , , and activation normalizations yield similar smoothing benefits even when they introduce higher ICS. Fundamental research like this matters not only for understanding, but for cultivating an ML culture built on first principles.

Let’s test the ICS claim directly. We will train three models at the same learning rate (): a Regular CNN, a CNN + BatchNorm, and a CNN + BatchNorm + Noise model where random noise from a non-zero-mean, non-unit-variance distribution is injected immediately after each BatchNorm layer at every step. That noise creates severe, chaotic covariate shift — yet the model still outperforms the regular CNN.

experiment 02 · internal covariate shift test
Injecting chaotic non-zero-mean, non-unit-variance noise after BatchNorm

Trains Regular CNN, CNN + BatchNorm, and CNN + BatchNorm + Noise (LR 0.15), tracking train loss/accuracy alongside Layer 2 activation moment shifts.

Train Set Loss
lower is better
awaiting run
Click Train to stream live measurements
Regular CNN
CNN + BatchNorm
CNN + BN + Noise
Train Set Accuracy
higher is better
awaiting run
Click Train to stream live measurements
Regular CNN
CNN + BatchNorm
CNN + BN + Noise
Layer 2 Mean Shift |Δμ|
step-to-step ics
awaiting run
Click Train to stream live measurements
Regular CNN
CNN + BatchNorm
CNN + BN + Noise
Layer 2 Variance Shift |Δσ²|
step-to-step ics
awaiting run
Click Train to stream live measurements
Regular CNN
CNN + BatchNorm
CNN + BN + Noise
fig. 05Training loss and accuracy (top) alongside step-to-step shifts in Layer 2 activation mean and variance (bottom). Deliberately injecting chaotic ICS into BatchNorm barely hurts convergence.

After training, you should see BatchNorm with or without noise outperforming the regular CNN. Meanwhile, in the bottom two charts, the activation moments (mean and variance) of the noisy BatchNorm model fluctuate wildly from step to step. Even when we intentionally inject massive ICS, the network trains better — directly supporting the authors’ conclusion.

Finally, let’s train Regular CNN vs. CNN + BatchNorm one last time to measure landscape smoothness directly. The left chart tracks how sharply the loss jumps between consecutive steps, and the right chart tracks local “effective” -smoothness measured while stepping in the direction of the gradient. Because deep networks are non-linear, global -smoothness does not hold everywhere, so we probe local effective -smoothness around each iterate. Lower, steadier values indicate a smoother, more predictable gradient field.

experiment 03 · optimization landscape smoothness
Step-to-step loss variation & local effective β-smoothness

Measures how smoothly the loss and activation gradients evolve along the gradient direction across a ∈ [0.05, 0.35]. Lower and steadier curves indicate a smoother landscape.

Loss Change |ΔL|
step-to-step jitter
awaiting run
Click Train to stream live measurements
Regular CNN
CNN + BatchNorm
Local Effective β-Smoothness
gradient predictiveness
awaiting run
Click Train to stream live measurements
Regular CNN
CNN + BatchNorm
fig. 06Step-to-step loss variation (left) and local effective β-smoothness along the Layer 1 activation gradient direction (right).

The local effective -smoothness above is computed by choosing a range of step sizes and, at every training step , evaluating:

where is the gradient of the loss with respect to , the output activations of the first convolution / BatchNorm block.

06references

Papers cited

  1. [1]Shibani Santurkar, Dimitris Tsipras, Andrew Ilyas, and Aleksander Madry. How Does Batch Normalization Help Optimization? (No, It Is Not About Internal Covariate Shift). arXiv:1805.11604, June 2018.
  2. [2]Daniel Jiwoong Im, Michael Tao, and Kristin Branson. An Empirical Analysis of Deep Network Loss Surfaces. arXiv:1612.04010, December 2016.
  3. [3]Sergey Ioffe and Christian Szegedy. Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. arXiv:1502.03167, March 2015.