291. BatchNorm with running statistics for inference
BatchNorm normalises each feature across the batch during training and keeps a running average of mean and variance for inference, when batches may be size 1.
Write batchnorm(x, gamma, beta, running_mean, running_var, training, momentum=0.1, eps=1e-5) for x of shape (n, d). Return (y, new_running_mean, new_running_var).
- training: use the batch mean and population variance to normalise; update running = (1 - momentum) * running + momentum * batch_stat (use the population variance for the update too).
- not training: normalise with the running stats and return them unchanged.
def batchnorm(x, gamma, beta, running_mean, running_var, training, momentum=0.1, eps=1e-5)Example 1
Input: batchnorm(np.array([[1.0, 10.0], [3.0, 20.0], [5.0, 30.0]]), np.ones(2), np.zeros(2), np.zeros(2), np.ones(2), True)[0]
Output: [[-1.224743, -1.224745], [0.0, 0.0], [1.224743, 1.224745]]
Example 2
Input: batchnorm(np.array([[1.0, 10.0], [3.0, 20.0], [5.0, 30.0]]), np.ones(2), np.zeros(2), np.zeros(2), np.ones(2), True)[1:]
Output: ([0.3, 2.0], [1.166667, 7.566667])
Example 3
Input: batchnorm([[2.0, 15.0]], np.ones(2), np.zeros(2), np.array([3.0, 20.0]), np.array([4.0, 100.0]), False)[0]
Output: [[-0.499999, -0.5]]
Constraints
xhas shape(n, d)- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
Hint 1
The only difference between training and inference is where the mean and variance come from, and only training updates the running stats.
Hint 2
If training, compute the batch mean and population variance over axis 0 and blend them into the running stats with momentum; otherwise use the running stats as is. Then normalise, scale by gamma and shift by beta.
Hint 3
Use x.var(axis=0), which already has ddof=0 (population variance), both for normalising and for the update. The update is (1 - momentum) * running + momentum * batch_stat, and y is (x - mu) / np.sqrt(var + eps) * gamma + beta.
Common mistakes
- The buggy version uses batch stats at inference too.
Understand the idea
BatchNorm normalises each feature using the mean and variance of the current batch. At inference you may get a single example, whose variance is zero, so during training BatchNorm also keeps running averages of mean and variance and uses those at test time. Like a shop tracking average daily sales instead of judging from one customer. Worked example, one feature, momentum 0.1, eps tiny: training batch [1, 3] has mean 2 and population variance 1, so y = [-1, 1]. Running mean goes from 0 to 0.9 x 0 + 0.1 x 2 = 0.2. Running var goes from 1 to 0.9 x 1 + 0.1 x 1 = 1.0. At inference, x = 1 gives (1 - 0.2) / sqrt(1.0) = 0.8, and the running stats are returned unchanged.
training uses batch stats, then running = (1 - momentum) running + momentum batch_stat; inference uses running stats; y = (x - mean) / sqrt(var + eps) * gamma + beta
CNNs such as ResNet depend on this.
Topics
Companies2
- Google a candidate reported this question Google AI/ML, RAG and agentic AI interview (AI Engineering Insider)
- NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan
From public interview reports and guides. Not affiliated with or endorsed by these companies.