TensorTau PathToAGI Training Deep Networks Level 07

Level 07 · Training Deep Networks · Chapter 099

Batch Normalization

Can a layer learn more reliably if we keep its intermediate activations on a controlled scale during training?

For one feature across a mini-batch, BatchNorm computes

μB=1B∑ixi,\mu_B=\frac1B\sum_i x_i,

σB2=1B∑i(xi−μB)2,\sigma_B^2=\frac1B\sum_i(x_i-\mu_B)^2,

x^i=xi−μBσB2+ϵ,\hat x_i=\frac{x_i-\mu_B}{\sqrt{\sigma_B^2+\epsilon}},

then restores learnable freedom:

yi=γx^i+β.y_i=\gamma\hat x_i+\beta.

1. Why gamma and beta?

Normalization should not permanently force every representation to mean 0 and variance 1. Learned γ\gamma and β\beta let the network choose an appropriate scale and offset.

2. Training and inference differ

During training, BatchNorm uses batch statistics. During inference, it typically uses running estimates accumulated during training. This is why model.train() and model.eval() matter.

3. Batch dependence

Very small or non-i.i.d. batches can make statistics noisy. BatchNorm also couples examples through shared batch statistics.

4. CNN shapes

For PyTorch image tensors [B,C,H,W], BatchNorm2d normally learns one gamma/beta per channel and aggregates statistics across batch and spatial positions.

What to Remember

BatchNorm normalizes using batch-dependent statistics, then applies learned scale/shift. It often stabilizes optimization but behaves differently at train and inference time.

Next → Layer Normalization: can we normalize each example independently so sequence models do not depend on other batch members?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

  1. Explain: Roles of μ, variance, γ and β.
  2. Calculate: Normalize a tiny batch by hand.
  3. Predict: Why does BatchNorm behave differently in train/eval modes?
  4. Break: Forget model.eval() at inference and observe instability.
  5. Build: Implement a simple 1-D BatchNorm forward pass.
  6. Investigate: Compare BatchNorm at large and tiny batch sizes.