Lensa ML
Lensa ML

How Batch Normalization Works

Step 1 of 6

The Problem

Why shifting data kills learning

Data flowing through layers:
InputHiddenOutput
Where output data lands on the activation function:
flat zoneflat zonesteep = good gradientspre-activation valuegrad
How much data shifts (drag me!)0.0
Data drift
0.0
Avg gradient
0.248
Gradient health
Strong

Data flows from Input to Output. The dots land on the steep middle part of the sigmoid curve (green dots). Gradients are strong here, so the network learns quickly!

Step 1 of 6: The Problem

Why shifting data kills learning

Data flowing through layers:
InputHiddenOutput
Where output data lands on the activation function:
flat zoneflat zonesteep = good gradientspre-activation valuegrad
How much data shifts (drag me!)0.0
Data drift
0.0
Avg gradient
0.248
Gradient health
Strong

Data flows from Input to Output. The dots land on the steep middle part of the sigmoid curve (green dots). Gradients are strong here, so the network learns quickly!

Step 2 of 6: Where BN Lives

Placed between linear layer and activation

The BN pipeline: x → Wx+b → BN → activation → output
x1.5Wx+b1.8BNoffσ(·)0.858out0.858
Where the value lands on the sigmoid:
grad=0.122
Weight scale1.0
Pre-BN
1.8
Post-BN
1.8
Gradient
0.122

Without BN, small weights keep values in the sigmoid's useful range. But real networks need large weights to learn complex patterns.

Step 3 of 6: BN in Action

Watch the calculation inside a network

A batch of 4 inputs flowing through one layer:
InputWx+bBNσ(·)Outx₁0.81.7-0.50.380.38x₂1.52.80.40.590.59x₃2.33.91.40.800.80x₄0.20.8-1.30.220.22μ=2.3 σ=1.2
Before and after BN (same values on a number line):
Before BN (Wx+b)mean=2.31.72.83.90.8After BN (normalized)0-0.510.381.40-1.28
Weight (W)1.5
Bias (b)0.5
Batch mean
2.30
Batch std
1.18
Norm range
-1.3 to 1.4

Four inputs go through Wx+b, producing scattered pre-activation values (top line). BN looks at the whole batch, computes the mean and std, then normalizes each value: (x - mean) / std. Result: always centered at zero (bottom line).

Step 4 of 6: Scale & Shift

Learnable γ and β restore power

Raw (reference)Normalized (x̂)y = γx̂ + β
γ (scale)1.00
β (shift)0.00
γ
1.00
β
0.00
Output range
-1.5 to 1.5

γ=1, β=0: the output equals the normalized values. This is the starting point before the network learns the best scale and shift.

Step 5 of 6: Train vs Inference

Batch stats vs running averages

Batch means (dots) vs running average (line) over training:
Training batchesBatch meanRunning avg
Training
uses batch stats
mean=2.1
normalized=0.27
Inference
uses running avg
mean=0.2
normalized=1.65
Batch index0
Momentum0.10
Batch mean
2.1
Running mean
0.21
Difference
1.38

During training, BN uses the current batch's mean (orange dot). It also keeps a running average (blue line) that smoothly tracks over time. Notice how training and inference produce different outputs for the same input value.

Step 6 of 6: The Payoff

Faster training, higher learning rates

EpochLoss020406080With BNWithout
Epoch0
Learning rate0.010
LR
0.010
Loss (BN)
2.65
Loss (no BN)
2.90

At this conservative learning rate, both models learn slowly. Press Play to watch them train. BN has a slight edge even here.