295. Derive the BatchNorm training-mode backward pass for 2-D and 4-D inputs
In training mode BatchNorm normalises with the batch's own mean and variance, so every output depends on every input in the batch. A backward pass that treats the statistics as constants is wrong, and the error grows as the batch shrinks.
Write batchnorm_backward(x, gamma, dy, eps=1e-5) for the forward y = gamma * (x - mu) / sqrt(var + eps) + beta, where mu and var are the batch mean and population variance per channel.
- If x is (N, C), channels are columns and statistics are over axis 0.
- If x is (N, C, H, W), statistics for channel c are over axes (0, 2, 3).
gamma has shape (C,), dy has the shape of x. Return a dict with "dx" (shape of x), "dgamma" (C,) and "dbeta" (C,).
Check: the per-channel sum of dx is always 0, because shifting a whole channel by a constant does not change y.
def batchnorm_backward(x, gamma, dy, eps=1e-5)Example 1
Input: batchnorm_backward([[2.5, 1.8, 3.5], [0.8, 5.9, 4.2], [4.9, 2.5, 7.7], [2.3, 1.8, 4.6], [-0.2, 1.3, 4.4]], [-0.9, -2.2, -0.1], [[0.9, -0.1, 1.1], [-0.6, -1.7, -0.6], [0.0, -0.3, -0.3], [0.1, 0.8, -0.9], [-0.1, -0.5, 0.0]])
Output: {'dx': [[-0.415, 0.098, -0.0699], [0.2817, 0.1073, 0.039], [0.1689, 0.0029, -0.02], [-0.0091, -1.0916, 0.0552], [-0.0265, 0.8835, -0.0043]], 'dgamma': [0.8091, -3.2335, -1.1688], 'dbeta': [0.3, -1.8, -0.7]}
Constraints
gammahas shape(C,)- Python 3 with NumPy. Code runs in your browser.
- Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1
The batch mean and variance depend on every input, so dx has extra terms beyond dy * gamma / std, and those terms make each channel's dx sum to 0.
Hint 2
Pick the reduction axes from x.ndim, compute mu, var, inv_std and xhat per channel, set dxhat = dy * gamma, then dgamma is the sum of dy * xhat and dbeta is the sum of dy over those axes.
Hint 3
With m values per channel, dx = inv / m * (m * dxh - dxh.sum(axes, keepdims=True) - xh * (dxh * xh).sum(axes, keepdims=True)); reshape gamma to (1, -1) or (1, -1, 1, 1) so it broadcasts.
Common mistakes
- The buggy version keeps only the direct path dy * gamma *
inv_std.
Understand the idea
In training mode BatchNorm uses the batch's own mean and variance, so nudging one input moves the statistics and therefore every output in that channel. The gradient has three paths: the direct one, one through the mean and one through the variance. They combine into a short formula with two per-channel sums.
one channel, x = [1, 3], gamma = 1, dy = [1, 0]. Mean 2, variance 1, x_hat = [-1, 1]. sum(dy) = 1 and sum(dy * x_hat) = -1. dx = (1 / 2) * (2 * [1, 0] - 1 - x_hat * (-1)) = 0.5 * ([2, 0] - [1, 1] - [1, -1]) = [0, 0] (tiny values with eps). That makes sense: with two points the normalised output is always [-1, 1], whatever x is. dgamma = -1, dbeta = 1. Rule per channel with m values: dx = gamma * inv_std / m * (m * dy - sum(dy) - x_hat * sum(dy * x_hat)); dgamma = sum(dy * x_hat); dbeta = sum(dy). For 4-D input, the sums run over N, H and W. Check: dx sums to 0 per channel. Any framework's BatchNorm layer uses this backward.
Topics
Companies1
- 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.