A convolution layer uses one small kernel at every position of the image, so when the error arrives from above, every output position has a claim on that same kernel. This lesson is about the bookkeeping: where each error goes, and which contributions have to add up. This lesson pairs with Lectures 11 and 12 of CMU's 11-785 (Fall 2026), linked under Watch & read. You build conv2d_backward and max_pool2d_backward in NumPy and check them against PyTorch's autograd.
Recall the forward pass. Output value out[n, f, i, j] is the sum, over input channels and kernel offsets, of window × kernel, plus the bias b[f]. The upstream gradient dout has the output's shape. Each output value touches only its own window, the kernel and one bias, so the derivatives are short:
- db[f] is the sum of dout[n, f, i, j] over n, i and j. Every position added the same bias once.
- dw[f, c, a, b] is the sum, over n, i and j, of dout[n, f, i, j] × the input value that kernel entry (a, b) multiplied at that position. One position gives the window value; the kernel is shared, so all positions are summed.
- dx: the derivative of an output with respect to an input value in its window is the kernel entry that multiplied it. Position (i, j) therefore adds dout[n, f, i, j] × kernel into its window of the input, for every filter f. Windows overlap, so a cell collects contributions from several positions, and they must be added with
+=.
Take a 3 × 3 input holding 1 to 9, the kernel [[1, 2], [3, 4]], stride 1, and dout equal to 1 everywhere. There are four positions. Watch dx fill up:
The same four positions give db = 4 and, because every position multiplies the kernel entry (0, 0) by a different input cell, dw[0, 0] = 1 + 2 + 4 + 5 = 12. Reproduce all three with loops:
import numpy as np
x = np.arange(1.0, 10.0).reshape(3, 3)
k = np.array([[1.0, 2.0], [3.0, 4.0]])
dx, dw, db = np.zeros((3, 3)), np.zeros((2, 2)), 0.0
for i in range(2):
for j in range(2):
dw += x[i:i + 2, j:j + 2] # error 1 times the window it saw
dx[i:i + 2, j:j + 2] += k # error 1 times the kernel
db += 1.0
print(dx.astype(int).tolist())
print(dw.astype(int).tolist(), db)Two details from the forward pass carry over unchanged. Position (i, j) reads the window that starts at (i × stride, j × stride), in the backward pass as well as in the forward one. And with padding, the windows reach into zeros that were added around the input. Accumulate into an array of the padded size, then cut the padding off at the end: dx has the shape of x. Gradient that landed on padding cells belongs to no input value and is discarded.
Seen as an operation, dx scatters the kernel, scaled by the errors. That is a transposed convolution, the layer that upsamples in later architectures. With stride s the scattered copies sit s cells apart, which is the same as putting s - 1 zeros between the entries of dout and then applying an ordinary full convolution with the kernel:
import numpy as np
k = np.array([1.0, 2.0, 3.0])
dout = np.array([10.0, 20.0, 30.0]) # three positions over a row of 7, stride 2
scatter = np.zeros(7)
for j in range(3):
scatter[2 * j:2 * j + 3] += dout[j] * k
spread = np.zeros(5)
spread[::2] = dout # [10, 0, 20, 0, 30]
print(scatter.tolist())
print(np.convolve(spread, k).tolist())Max pooling has no weights, so the only question is where each error goes. The output is the maximum of its window. Nudge the maximum and the output moves with it by the same amount; nudge any other cell by a little and nothing changes. So the whole error goes to the cell that held the maximum, and the rest of the window gets zero. This is not average pooling, which would share the error equally. If two cells tie, the derivative is not defined; the build asks for the first maximum in row-major order, and other libraries may choose differently, which is why the comparison with PyTorch uses inputs without ties. When the stride is smaller than the pool size, windows overlap, one cell can be the maximum of several of them, and its errors add.
These are the mistakes the tests are built to catch:
- Assigning into dx instead of adding. A first test with non-overlapping windows passes and the first overlap breaks it.
- Locating a window at (i, j) instead of (i × stride, j × stride) when computing dw. Stride 1 hides it.
- Cutting the padding off from the wrong offset, or not cutting it off. Padding 0 hides it.
- Averaging dw or db over positions or over the batch. Both are sums.
- Splitting a pooling error among the cells of a window, or sending it to every cell.
To check a gradient, measure it. Change one kernel entry by ±ε, recompute a scalar loss, and compare the slope with your dw. Take the loss to be the output weighted by a fixed dout, so that its gradient is exactly what conv2d_backward receives:
import numpy as np
x = np.arange(1.0, 10.0).reshape(3, 3)
dout = np.array([[1.0, -2.0], [0.5, 3.0]])
def loss(k):
out = np.array([[(x[i:i + 2, j:j + 2] * k).sum() for j in range(2)] for i in range(2)])
return (out * dout).sum()
k, eps = np.array([[1.0, 2.0], [3.0, 4.0]]), 1e-6
bumped = k.copy()
bumped[1, 0] += eps
down = k.copy()
down[1, 0] -= eps
print(round((loss(bumped) - loss(down)) / (2 * eps), 4))
print(sum(dout[i, j] * x[i + 1, j] for i in range(2) for j in range(2)))The hidden tests use a second, independent check: PyTorch's autograd is given the same forward pass and the same upstream gradient (out.backward(dout)), and dx, dw and db must agree to 1e-10 for strides 1 to 3, paddings 0 to 2, several channels, non-square inputs and kernels, and overlapping pooling windows. That shows your loops compute the same numbers as autograd for those cases. It does not cover ties in pooling, dilation, groups or a different stride per axis, and it says nothing about speed: real libraries compute the same result with much faster algorithms.