Batch Normalization › Worked Example: Batch Norm By Hand

Michael BrenndoerferApril 19, 20254 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 71Batch Normalization › Worked Example: Batch Norm By Hand
Reading section 12 of 17 · Worked Example: Batch Norm By Hand · 4 minSection 12 of 17 · 4 min

Book contents

Section 12 of 17 · 4 min read

Batch Normalization › Worked Example: Batch Norm By HandOne section · Sections in this chapter12 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's trace through a single batch normalization step with a concrete mini-batch. Suppose we have a batch of 4 samples with activations for one feature dimension:

x1=2.0,x2=4.0,x3=3.0,x4=1.0x_1 = 2.0, \quad x_2 = 4.0, \quad x_3 = 3.0, \quad x_4 = 1.0

Step 1: Compute the batch mean.

μB=2.0+4.0+3.0+1.04=10.04=2.5\mu_{\mathcal{B}} = \frac{2.0 + 4.0 + 3.0 + 1.0}{4} = \frac{10.0}{4} = 2.5

Step 2: Compute the batch variance.

σB2=(2.0−2.5)2+(4.0−2.5)2+(3.0−2.5)2+(1.0−2.5)24\sigma^2_{\mathcal{B}} = \frac{(2.0-2.5)^2 + (4.0-2.5)^2 + (3.0-2.5)^2 + (1.0-2.5)^2}{4} =0.25+2.25+0.25+2.254=5.04=1.25= \frac{0.25 + 2.25 + 0.25 + 2.25}{4} = \frac{5.0}{4} = 1.25

Step 3: Normalize. Using ϵ=10−5\epsilon = 10^{-5}:

x^1=2.0−2.51.25+10−5≈−0.51.118≈−0.447\hat{x}_1 = \frac{2.0 - 2.5}{\sqrt{1.25 + 10^{-5}}} \approx \frac{-0.5}{1.118} \approx -0.447 x^2≈1.342,x^3≈0.447,x^4≈−1.342\hat{x}_2 \approx 1.342, \quad \hat{x}_3 \approx 0.447, \quad \hat{x}_4 \approx -1.342

Notice that the normalized values sum to zero and have unit variance: (−0.447)2+(1.342)2+(0.447)2+(−1.342)2=0.2+1.8+0.2+1.8=4.0(-0.447)^2 + (1.342)^2 + (0.447)^2 + (-1.342)^2 = 0.2 + 1.8 + 0.2 + 1.8 = 4.0, and 4.0/4=1.04.0 / 4 = 1.0. The normalization has precisely achieved its goal.

Step 4: Scale and shift. With initial parameters γ=1.0\gamma = 1.0 and β=0.0\beta = 0.0 (the default initialization):

yi=γx^i+β=1.0⋅x^i+0.0=x^iy_i = \gamma \hat{x}_i + \beta = 1.0 \cdot \hat{x}_i + 0.0 = \hat{x}_i

Initially, the transformation is pure normalization. As training proceeds, γ\gamma and β\beta will shift away from these initial values to match the learned optimal distribution. If the downstream layer works best when inputs are scaled by 2 and shifted by 0.5, gradient descent will move γ→2.0\gamma \to 2.0 and β→0.5\beta \to 0.5 over the course of training.

What happens in the next training step? If a different batch is drawn with slightly different values, say mean 2.7 instead of 2.5, the normalized values will be different. Sample x1=2.0x_1 = 2.0 will be normalized to 2.0−2.7σ2≈−0.625\frac{2.0 - 2.7}{\sqrt{\sigma^2}} \approx -0.625 instead of −0.447-0.447. This is the stochastic noise of batch normalization. Over many batches, the network learns representations that are robust to this variability.

The figure below shows the effect of batch normalization on activation distributions.

Out[7]:
Visualization
Histogram of raw activations centered near 4.0.
Activation distribution before batch normalization. The feature has mean 4.0 and standard deviation 2.0, representing an arbitrary, uncontrolled distribution typical of deep network activations.
Histogram of normalized activations centered near 0.
After the normalize step: activations are recentered at zero with unit variance. All features share a common scale, regardless of their raw magnitude, which stabilizes gradient flow.
Histogram of scaled and shifted activations centered near 0.5.
After applying learned scale (gamma=1.5) and shift (beta=0.5): the network recovers a useful distribution. The gamma and beta parameters restore representational capacity that pure normalization removed.