BatchNorm and LayerNorm
⚖️ What is batch normalization? Batch normalization standardizes each feature of a layer's input using the mean and variance of the current batch, then rescales it with a learned gain and shift. During training it uses batch statistics; at inference it uses running averages collected during training, so a prediction never depends on the other examples in the batch.
Waymo asked a research scientist candidate, in a screen with no LeetCode-style question, to hand-write batch normalization in Python (the question).
The Prompt
The forward pass is four lines. What the interviewer checks is the part people forget: the difference between training and inference, the running statistics, and, if time allows, the backward pass.
"Implement batch normalization for a fully connected layer: input
(N, D). Support training and evaluation modes."
Clarifying Questions
You: Normalize each of the D features over the batch axis, with a learned gamma and beta of shape
(D,)?
Interviewer: Yes.
You: How should the running statistics update? I'd follow PyTorch: an exponential average with
momentum = 0.1, and the unbiased batch variance for the running variance.
Interviewer: Fine. Use eps = 1e-5.
You: Should I write the backward pass by hand, or can I rely on autograd?
Interviewer: By hand, after the forward works.
You: What happens with a batch of one in training mode? The variance is zero.
Interviewer: Mention it; you don't need to support it.
What You'll Build
BatchNorm1d(D, momentum, eps)withforward(x, training) -> (N, D), keepingrunning_meanandrunning_varof shape(D,).backward(dout) -> dx, settingdgammaanddbeta, all with the shapes of what they differentiate.layer_norm(x, gamma, beta) -> same shape: normalize over the last axis instead.rms_norm(x, gain) -> same shape: scale by the root mean square, no centering.