Batch Normalization in Deep Networks

Watch the training flow! Unscaled data causes first-layer weights to explode (Red), while subsequent deep layers vanish (Blue). Toggle Batch Normalization to dynamically rescale intermediate outputs, locking the deep gradients into stability (Green).

35
80k

Gradient Health

Global Status
STABLE
Waiting to start...

Average Weight Magnitude

W1 (Input → H1) 1.000
W3 (H2 → H3) 1.000

Color Legend

Vanish (~0) Stable (~1) Explode (>2)
Drag to Pan | Scroll to Zoom

Batch Normalization in Deep Networks: Complete Guide

Read this after trying the interactive once, then run it again with the suggested experiments.

Quick Context

Batch Normalization (BatchNorm) was introduced by Ioffe & Szegedy in 2015 and is now a standard building block in virtually every deep network. It normalizes the inputs to each layer so that training is faster and more stable. This page lets you see what happens with and without it.

1) The Problem BatchNorm Solves

When you feed raw features with different scales (e.g., Age 18–80 vs. Salary 20k–200k) into a network, the first-layer weights that receive the large feature grow disproportionately. During backpropagation those large weights amplify gradients in early layers (exploding), while deeper layers receive progressively smaller updates (vanishing). The result: unstable training or very slow convergence.

Normalizing inputs at the front door helps the first layer, but internal covariate shift — the distribution of each hidden layer's inputs keeps changing as weights update — still destabilizes deeper layers. BatchNorm fixes this by re-normalizing activations inside the network at every layer.

2) How Batch Normalization Works

For a mini-batch of activations z at a given layer:

μ_B  = (1/m) Σ z_i         # batch mean
σ²_B = (1/m) Σ (z_i − μ_B)²  # batch variance
ẑ_i  = (z_i − μ_B) / √(σ²_B + ε) # normalize
y_i  = γ · ẑ_i + β         # scale & shift (learnable)
  • ε is a tiny constant (e.g., 1e-5) to avoid division by zero.
  • γ and β are learnable parameters that let the network recover any scale/shift it needs — BatchNorm doesn't permanently force zero-mean unit-variance; it just gives the optimizer a much friendlier starting point.
  • At inference time, the running mean and variance computed during training are used instead of batch statistics.

3) Why This Is Worth Learning

  • Faster convergence — you can often use much higher learning rates.
  • Regularization effect — the noise from batch statistics acts as mild regularization, sometimes reducing the need for Dropout.
  • Enables deeper networks — gradients stay in a healthy range across 50, 100, or even 1000+ layers.
  • Interview staple — expect questions on BatchNorm in almost every ML/DL interview.

4) Guided Experiments with This Interactive

  1. Baseline — Set Age ≈ 35, Salary ≈ 80k. Leave both toggles OFF. Click Train Network. Watch W1 (first layer) turn red and W3 (deep layer) turn blue. The status should say "Unstable".
  2. Input normalization only — Turn ON "Normalize Raw Inputs", leave Batch Norm OFF. Train again. W1 calms down, but deep layers may still drift.
  3. Batch Normalization — Turn ON "Enable Batch Norm" (keep input normalization ON too). Train again. All bars should stay green and status should read "Stable".
  4. Extreme inputs — Set Salary to 200k with only Batch Norm ON (no input normalization). Observe how BatchNorm alone handles large feature scales internally.
  5. Reset & reproduce — Click Reset and repeat experiment 3 to confirm the result is consistent.

5) Where BatchNorm Is Placed

There are two common conventions:

  • Before activation: Linear → BatchNorm → ReLU (the original paper's approach).
  • After activation: Linear → ReLU → BatchNorm (some practitioners prefer this).

In practice both work well; consistency within your architecture matters more than the placement choice.

6) Common Mistakes

  • Forgetting model.eval() — at inference, if you don't switch to eval mode, PyTorch/TensorFlow will use live batch stats instead of running averages, producing inconsistent predictions.
  • Batch size too small — with batch size 1 or 2, the batch mean/variance estimate is too noisy. Use Group Normalization or Layer Normalization instead.
  • Using BatchNorm with Dropout carelessly — both introduce stochasticity; their interaction can hurt. Test whether you need both.
  • Applying BatchNorm to the output layer — normalizing the final prediction layer often harms performance.

7) BatchNorm vs. Other Normalizations

TechniqueNormalizes OverBest For
Batch NormBatch dimensionCNNs with large batches
Layer NormFeature dimensionTransformers, RNNs
Group NormChannel groupsSmall-batch CNNs
Instance NormSingle sample, single channelStyle transfer

8) Key Takeaways

  • BatchNorm normalizes hidden-layer activations using batch statistics during training and running statistics during inference.
  • It fights internal covariate shift, enabling higher learning rates and deeper architectures.
  • The learnable parameters γ and β let the network undo the normalization if needed.
  • Always switch to eval mode at inference and be mindful of batch size.
Cheat sheet

Batch Normalization in Deep Networks

Batch Normalization (BatchNorm) was introduced by Ioffe & Szegedy in 2015 and is now a standard building block in virtually every deep network. It normalizes the inputs to each layer so that training is faster and more stable. This page lets you see what happens with and without it.

DEEP LEARNING · vizlearn.in/deep_learning/batch_normalization.html