# What Is Batch Normalisation?

> What batch normalisation does, why it speeds up training, what changes at test time and where it breaks, for your AI interview.

**In short:** Batch normalisation rescales each layer's inputs using the mean and spread of the current mini-batch, then lets the model learn a new scale and shift. It speeds up training, but its batch statistics get noisy with small batches.

AI and LLM interview question Q427, Classic ML and deep-learning basics. Intermediate, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-batch-normalisation

## Say it in 30 seconds

Batch normalisation is a layer that steadies the numbers flowing through a neural network. For each training step, it takes the average and spread of the batch, rescales the values to match, and then learns a small scale and shift. That lets the model train faster, with higher learning rates. At test time it uses running averages instead. Its weak spot is small batches, where the averages get noisy.

## Say it in 2 minutes

A neural network is a stack of layers, and each layer's inputs shift as the layers before it learn. The 2015 paper that introduced batch normalisation called this a problem, and its cure is a normalising step inside the model. For each mini-batch, the layer takes the mean and spread of the values, rescales them, and then learns its own scale and shift.

The practical result is faster training. The authors reported that it lets you use much higher learning rates and worry less about starting weights. On a state-of-the-art image model, the paper reports the same accuracy in 14 times fewer training steps. Later research argues the main effect is a smoother training problem, so the effect is clear and the explanation is still debated.

Two things matter in practice. At test time there is no useful batch to average, so the layer uses running averages collected during training. And the batch must be big enough. With small batches the statistics get noisy, and the GroupNorm paper reports that batch norm's error rises rapidly as the batch shrinks. With a single example in a fully connected layer, there is no spread to measure, so a per-example normaliser such as GroupNorm is one option to know.

## What is batch normalisation, in plain words?

A neural network is a stack of layers. Each layer's numbers come from the layer before, so when that layer learns, the numbers shift under the next one. The next layer keeps chasing a moving target.

Batch normalisation steadies those numbers. It works on a mini-batch, a small group of examples the model sees together in one training step. For each feature, it takes the group's average and spread, rescales every value to match, and then lets the model learn a new scale and shift on top.

Think of a bank's credit team scoring loan files. If one clerk writes incomes in thousands and another in full dollars, the scoring rules break. Converting each day's batch of files to one standard keeps the scorer steady. Picture a made-up card-fraud model where amounts run from $2 to $90,000. A hidden layer's numbers can differ wildly from one feature to the next during training, and batch normalisation keeps them in a range the next layer can use.

- **Mini-batch:** A small group of examples the model processes together in one training step.
- **Mean and variance:** The average of a set of numbers, and how far they spread from it. Variance is the spread squared.
- **Gamma and beta:** The scale and shift the layer learns, so it can undo the rescaling if that works better.
- **Running average:** A moving average of the batch statistics, kept during training for use at test time.

The 2015 paper called the problem internal covariate shift. Its authors wrote that "the distribution of each layer's inputs changes during training, as the parameters of the previous layers change". Covariate shift is a fancy name for a layer's inputs changing as training goes on.

## How does batch normalisation work, step by step?

1. **Find the batch's mean and spread:** For each feature, add up the values in the mini-batch and divide by the count to get the average. Then measure the spread around that average.
2. **Normalise each value:** Subtract the average and divide by the spread. A tiny number, called eps, is added to the variance before the square root, so the code never divides by zero.
3. **Scale and shift:** Multiply by gamma and add beta. Both are learned during training, so the layer can keep the normalised values or undo them.

*Diagram, What batch normalisation does inside one layer: A layer takes a whole mini-batch, rescales it with that batch's own mean and spread, and passes on the result. Gamma and beta are learned, so the model can undo the rescaling if that helps.*
1. The layer takes a whole mini-batch at once.
2. It subtracts the batch mean and divides by the batch spread.
3. It multiplies by a learned gamma, adds a learned beta, and passes the result on.

Here is the arithmetic in plain Python. The numbers are made up: four transactions in one batch, one feature from a fraud model's hidden layer.

```python
# A fraud model's hidden layer, one feature, four made-up transactions in a batch.
batch = [12.5, 48.0, 230.0, 1900.0]   # one feature: amounts in dollars
gamma, beta, eps = 1.0, 0.0, 1e-5   # learned scale and shift; eps avoids dividing by zero
momentum = 0.1                      # how fast running averages follow each batch

mean = sum(batch) / len(batch)
var = sum((x - mean) ** 2 for x in batch) / len(batch)
out = [gamma * (x - mean) / (var + eps) ** 0.5 + beta for x in batch]

for x, y in zip(batch, out):
    print(f"{x} -> {y:.3f}")
print("batch mean:", mean, "batch variance:", var)

running_mean = (1 - momentum) * 0.0 + momentum * mean   # kept for test time
print("running mean after one batch:", running_mean)
```

Output:

```text
12.5 -> -0.682
48.0 -> -0.636
230.0 -> -0.405
1900.0 -> 1.722
batch mean: 547.625 batch variance: 616446.921875
running mean after one batch: 54.7625
```

The batch averages about 548 dollars. The 12.5 and 1900 amounts come out at about -0.682 and 1.722, and the whole batch now averages zero with a spread of about one. Gamma and beta then set the final scale. PyTorch's documentation describes the same step: the mean and standard deviation are calculated per-dimension over the mini-batches.

## What changes at test time?

At test time the model may score one transaction, so there is no batch to average. PyTorch's documentation says that during training the layer keeps running estimates of the mean and variance, and uses them for normalisation during evaluation. In PyTorch you switch this with model.eval().

- **Training: Use this batch.** Mean and spread come from the mini-batch in front of the layer, and the running averages are updated.
- **Scoring: Use the running averages.** The layer uses the averages from training, so one transaction can be scored on its own.

> **Tip:** Asked what changes at test time? Say the layer switches from batch statistics to running averages. Forgetting this switch is an easy bug to make.

## Why does it help training, and is the reason settled?

The 2015 paper reported faster training. It said the method lets you use much higher learning rates, the step size the model takes each time it updates its weights, and that it acts as a regulariser, a trick that stops the model memorising its training data. In some cases it removes the need for Dropout, a training method that randomly switches off neurons. On a state-of-the-art image model, it reported the same accuracy in 14 times fewer training steps.

> "Batch Normalization allows us to use much higher learning rates and be less careful about initialization." (Ioffe and Szegedy, 2015)
>
> "it makes the optimization landscape significantly smoother" (Santurkar and colleagues, 2018)

The explanation is less settled. The 2015 idea was that batch normalisation fixes shifting inputs. A 2018 study found that keeping inputs stable has little to do with its success. Its finding was that the main effect is a smoother training problem, so the gradients, the signals that tell each weight how to change, become more predictive.

> **Tip:** Asked why it works? Give the practical effect first, then say the cause is still argued over. Calling the shifting-inputs idea settled fact is a trap.

## When does batch normalisation break down?

Batch normalisation needs a batch with enough examples to estimate the average and spread. With a small batch, the estimates swing and accuracy drops. In a fully connected layer, a batch of one has no spread to measure at all. GroupNorm is a related method that works out its average and spread inside each single example, so batch size does not affect it.

> "BN's error increases rapidly when the batch size becomes smaller" (Wu and He, GroupNorm, 2018)

*Diagram, When the batch is too small: A small batch gives noisy averages. GroupNorm computes its statistics inside each example instead. Its computation does not depend on batch size, so its accuracy stays stable across a wide range of batch sizes.*
1. Two examples give a poor estimate of the batch's average and spread.
2. GroupNorm works out its statistics inside each example, so its accuracy stays steady across a wide range of batch sizes.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: normalise each layer's inputs per mini-batch, then learn a scale and shift.
- [ ] The problem it targets, and that its real cause is still debated.
- [ ] Training uses batch statistics; test time uses running averages.
- [ ] Why small batches hurt, and a per-example alternative such as GroupNorm.
- [ ] The payoff: faster training and higher learning rates, with a mild regularising effect.
- [ ] Its limits, said plainly.

**Common traps**

- Trap: "It removes the need to tune the learning rate." Better: It lets you use much higher learning rates. You still tune them.
- Trap: "Batch norm works the same at test time." Better: At test time it uses running averages from training, not the current batch.
- Trap: "Just use batch norm with a batch of one." Better: With one example in a fully connected layer there is no spread to measure. Use a per-example method.

## Can you spot the bug?

**Spot the bug:** A fraud scorer runs once per transaction in a live service. Which snippet is a real bug?

A. model.eval()  # then score each transaction on its own
B. model.train()  # then score each transaction on its own
C. model.eval()  # then score a batch of 200 at once
D. model.eval()  # called once, when the service starts

**Answer:** B. In training mode the layer normalises with the mean and spread of the current batch, not the saved running averages. One transaction gives no useful spread, so the output is meaningless. Train mode also keeps updating the running averages from live traffic. Score in eval mode, which uses the running averages.

**Quick check:** A team trains a vision model with batches of 2 on a small GPU, and the training error jumps around. What is the most sensible first fix?

A. Raise the learning rate tenfold
B. Switch to GroupNorm, which works inside each example
C. Remove gamma and beta from the layer
D. Turn off the running averages during training

**Answer:** B. Two examples give a poor estimate of the batch's average and spread. GroupNorm computes its statistics inside each example, and its accuracy stays stable across a wide range of batch sizes. The other fixes leave the noisy statistics in place.

## Can you answer these from memory?

**Flash cards**

- **What is batch normalisation?** A layer that rescales inputs with the batch's mean and variance, then learns a scale and shift.
- **What are gamma and beta?** The learned scale and shift. The layer can undo the normalising if that works better.
- **What does it use at test time?** Running averages of the mean and variance, collected during training.
- **Why did the 2015 paper say it helps?** It allows much higher learning rates and less careful initialisation.
- **What is its main weakness?** Noisy statistics with small batches. Per-example norms like GroupNorm avoid it.
- **Is the reason settled?** No. Later work argues the main effect is a smoother training problem.

## Likely follow-up questions

- Q052 What is layer norm; pre-norm vs post-norm? https://designervenkat.online/interview/ai/what-is-layer-norm-pre-norm-vs-post-norm
- Q417 What does the learning rate do? https://designervenkat.online/interview/ai/what-does-the-learning-rate-do
- Q426 Vanishing and exploding gradients? https://designervenkat.online/interview/ai/vanishing-and-exploding-gradients
- Q438 Why does weight initialisation matter? https://designervenkat.online/interview/ai/why-does-weight-initialisation-matter
