Ch 7 ▸ Backprop through a convolution ▸ Module 2/4
Gradients for the weights.
🔒 This module is locked
It builds on Loss and gradients. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.
Backpropagation reaches a convolution layer with one number for each of its outputs: the gradient of the loss with respect to that output. Call that map of gradients . The layer’s job is to turn into a gradient for each of its nine weights.
The example
Take chapter 2’s 4×4 example: the image, the edge kernel, and the 2×2 output map it made.
Suppose the targets are 27, 16, 28 and 17. Two outputs are right, one is 1 too high, and one is 1 too low. The squared error is , and each output’s gradient is :
In a real network, would arrive from the layers above. Here it comes straight from the loss.
One weight, every position
Take the kernel’s middle weight. Because of weight sharing, it was used at all four positions, and each time it sat on a different pixel: 5 for the top-left output, 9 for the top-right, 4 for the bottom-left and 8 for the bottom-right.
Nudge that weight up by a small amount . Each output changes by times the pixel the weight sat on, because that’s what the weight multiplied. And by the chain rule, each output’s change moves the loss by its gradient times that change. So each output contributes (pixel × ) to the weight’s gradient, and the four contributions add up:
The gradient is −6, so increasing the middle weight would lower the loss. That makes sense: the bottom-right output is too low, and raising this weight raises it by 8 for every unit, more than it raises the too-high top-left output, by 5.
All nine
Every weight works the same way: add up (pixel × ) over all positions, using the pixel that weight sat on at each one.
THE KERNEL · 3×3, 9 WEIGHTS
PICK A WEIGHT
GRADIENT FOR EACH WEIGHT · 3×3
The kernel fits on the 4×4 input in four positions, so the input is drawn four times below, once per position. The same nine weights are used at every position. Pick a weight, and each position shows the pixel it sat on and what that adds to its gradient.
TOP-LEFT POSITION
y = 28 · TARGET 27 · G = 2
ADDS 5 × 2 = 10
TOP-RIGHT POSITION
y = 16 · TARGET 16 · G = 0
ADDS 9 × 0 = 0
BOTTOM-LEFT POSITION
y = 28 · TARGET 28 · G = 0
ADDS 4 × 0 = 0
BOTTOM-RIGHT POSITION
y = 16 · TARGET 17 · G = −2
ADDS 8 × (−2) = −16
▸ GRADIENT FOR THE PICKED WEIGHT = (5 × 2) + (9 × 0) + (4 × 0) + (8 × (−2)) = −6
A 3×3 kernel fits on the 4×4 image in four positions, so figure 7.2 draws the image four times, once per position, with the kernel outlined on it and each weight written under the pixel it sits on. Pick any weight. In each copy, the pixel that weight sat on is filled in, and underneath is what that position adds to the weight’s gradient. The line at the bottom adds up the four. The full gradient, one number per weight, is
It has the same shape as the kernel, 3×3, because there’s one gradient for each weight.
Why sharing matters
Every weight of a convolution is used at every position, so its gradient collects a contribution from every position. On the 32×32 duck that’s 900 contributions per weight. Each position says how the weight should change to fix that position’s output, and the gradient adds up all those votes. That’s why a learned kernel ends up finding a pattern that’s useful everywhere in the image, not at one place: it was trained by all of them at once.
Knowledge check ▸ 4 questions
Before you move on.
Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.
1A weight was used at four positions. It sat on pixels 3, 1, 2 and 4, and those outputs’ gradients G were 1, 0, −1 and 2. What is the weight’s gradient?
2In the module’s 4×4 example, what is the gradient for the kernel’s top-right weight?
3A 3×3 kernel runs over a 32×32 image with no padding. How many contributions does each weight’s gradient add up?
4Why does a convolution weight’s gradient add up contributions from every position?