Two layers in almost every deep network do not compute the same function in training and in evaluation. Batch normalization measures the batch it is given, and dropout throws activations away at random. If you do not know which mode a layer is in, you cannot tell what it will output. This lesson pairs with Lecture 8 of CMU's 11-785 (Fall 2026), linked under Watch & read. You build both layers in NumPy and compare batch normalization with PyTorch's BatchNorm1d.
In training, batch normalization takes a batch x of shape (N, F) and treats each feature on its own. For feature j it computes the batch mean μⱼ and the variance σⱼ² over the N examples, then returns γⱼ · (xⱼ - μⱼ) / √(σⱼ² + ε) + βⱼ. The ε keeps the division finite when the variance is 0. γ and β are learned: they let the layer undo the normalization if that helps. The consequence is easy to miss: the output for one example now depends on the other examples in its batch.
Take one feature with the three values 1, 1 and 4. The mean is 2 and the squared deviations are 1, 1 and 4, so the variance is 6 / 3 = 2. With γ = 1, β = 0 and ε = 0 the outputs are -1/√2, -1/√2 and 2/√2.
import numpy as np
x = np.array([[1.0], [1.0], [4.0]]) # one feature, three examples
mean, var = x.mean(axis=0), x.var(axis=0) # NumPy divides by N by default
print(((x - mean) / np.sqrt(var)).ravel().round(4).tolist())
print(round(float(0.9 * 1.0 + 0.1 * var[0] * 3 / 2), 4), round(float(0.9 * 1.0 + 0.1 * var[0]), 4))At evaluation there may be a single example, and an example's answer should not depend on who else was in the batch. So the layer keeps running averages during training and reads them later. Each training step updates them as running = (1 - momentum) · running + momentum · batch value, with the default momentum 0.1. Starting from a running mean of 0, the batch mean 2 moves it to 0.2.
The running variance carries a detail the build follows because PyTorch does. The batch is normalized with the biased variance, which divides by N. The running variance is updated with the unbiased one, the batch variance times N / (N - 1). In the example that is 2 · 3 / 2 = 3, so a running variance of 1 becomes 0.9 + 0.1 · 3 = 1.2. The biased value would give 1.1. Training needs at least two examples: a batch of one has no spread, and PyTorch raises an error for it.
Forgetting to switch modes is one of the most common bugs in practice, and the numbers show why. Suppose training left a running mean of 10 and a running variance of 4, and the value 12 is normalized three ways.
import numpy as np
def normalize(x, mean, var, eps=1e-5):
return (x - mean) / np.sqrt(var + eps)
for batch in ([9, 10, 13, 12], [12, 14, 15, 16]):
b = np.array(batch, dtype=float)
print("training mode, batch", batch, "->", round(normalize(12.0, b.mean(), b.var()), 4))
print("evaluation mode ->", round(normalize(12.0, 10.0, 4.0), 4))The same input becomes 0.6325, -1.5213 or 1.0. In training mode it depends on the companions. In evaluation it is fixed.
Dropout works on the activations themselves. In training, each entry is zeroed with probability p, and the survivors are divided by 1 - p. This is called inverted dropout. An entry keeps its value with probability 1 - p, so the expected output is (1 - p) · x / (1 - p) + p · 0 = x. Because the rescaling happens in training, evaluation is the identity: it returns the input unchanged.
import numpy as np
rng = np.random.default_rng(0)
x, p = np.ones(20000), 0.25
keep = rng.random(x.shape) >= p
print(round(float((x * keep).mean()), 2), round(float((x * keep / (1 - p)).mean()), 2))The first number is the mean without the rescale, near 1 - p. The second is with it, near 1. Both layers pay for what they give. Batch normalization couples the examples of a batch. Dropout adds noise to every gradient. Both are used as regularizers, and neither is free.
The common mistakes follow from the above. Mixing up the two variances: normalizing with the unbiased one, or updating the running variance with the biased one. Dropping the division by 1 - p, so training activations are smaller than evaluation ones. Computing batch statistics at evaluation, which works until the batch has one example. Writing into the running arrays you were passed instead of returning new ones.
The hidden tests run three training batches and then evaluation through torch.nn.BatchNorm1d in float64, and compare outputs and both running statistics to 1e-10. Dropout has no oracle that shares a random stream, so its tests check properties under a seed: the zero fraction, the scaled survivors and the mean. That shows your layers match PyTorch's convention and obey the dropout contract. It does not show that either layer helps a network train, which is an empirical question about a model and a dataset.