Ch 7 ▸ Backprop through a convolution ▸ Module 3/4
Gradients for the input.
🔒 This module is locked
It builds on Gradients for the weights. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.
The weight gradients are what training uses to update this layer. But backpropagation has to keep going, back to the layers below. This layer’s input is the output of the layer before it, so that layer needs a gradient for each of its outputs: the gradient of the loss with respect to each of this layer’s input pixels.
One pixel
Nudge one input pixel up by a small amount . Every output whose window covers that pixel changes by times the weight that sat on it. So each of those outputs contributes ( × weight) to the pixel’s gradient, and the contributions add up.
Take pixel (1, 1) of the 4×4 example, the 5. All four kernel positions cover it, each through a different weight:
- The top-left output saw it through the kernel’s middle weight, 0.
- The top-right output saw it through the middle-left weight, −2.
- The bottom-left output saw it through the top-middle weight, 0.
- The bottom-right output saw it through the top-left weight, −1.
With from the last module:
A corner pixel is covered by only one position. Pixel (0, 0) was read only by the top-left output, through the top-left weight, so its gradient is .
THE KERNEL · 3×3, 9 WEIGHTS
GRADIENT FOR EACH PIXEL · 4×4
The kernel fits on the 4×4 input in four positions, so the input is drawn four times below, once per position. Pick a pixel in any copy, and each position shows whether its kernel covered that pixel, through which weight, and what that adds to its gradient.
TOP-LEFT POSITION
y = 28 · TARGET 27 · G = 2
ADDS 2 × 0 = 0
TOP-RIGHT POSITION
y = 16 · TARGET 16 · G = 0
ADDS 0 × (−2) = 0
BOTTOM-LEFT POSITION
y = 28 · TARGET 28 · G = 0
ADDS 0 × 0 = 0
BOTTOM-RIGHT POSITION
y = 16 · TARGET 17 · G = −2
ADDS (−2) × (−1) = 2
▸ GRADIENT FOR PIXEL (1, 1) = (2 × 0) + (0 × (−2)) + (0 × 0) + (−2 × (−1)) = 2
Figure 7.3 draws the image once per kernel position, like figure 7.2. Pick a pixel in any copy. Each position whose kernel covers it shows the weight that sat on it and what that adds to the pixel’s gradient; positions that don’t cover it add nothing. In a 4×4 image the four corners are read once, the other edge pixels twice, and the middle four by every output. On a bigger image, a pixel far from the edges is read by nine outputs, one for each tap of the kernel, so its gradient adds nine pairs.
Spreading gradients back
There’s another way to see the same computation. Each output read a 3×3 window of the input. Going backward, it spreads its gradient back over that same window, multiplied by the kernel: it adds times the kernel’s weights onto the nine pixels it read. Where windows overlap, the spread-out gradients add up.
In the example, the top-left output stamps the kernel onto the top-left 3×3 window, the bottom-right output stamps the kernel onto the bottom-right window, and the other two stamp zeros. The full input gradient is the sum of the stamps:
Figure 7.4 runs both directions on the same example, one kernel position at a time, in the same order. Forward, each position gathers its 3×3 window into one output. Backward, each output’s is multiplied by the whole kernel, and that 3×3 stamp is added onto the window the output read. Step through the backward direction and watch the input gradient build up: the first stamp, 2 × the kernel, fills the top-left window; the two middle stamps add nothing, because their is 0; the last stamp, −2 × the kernel, lands on the bottom-right window, and where it overlaps the first, the two add.
INPUT GRADIENT · 4×4
STAMP = G × KERNEL · 3×3
1 NUMBER × 9 WEIGHTS → 9 NUMBERS
OUTPUT GRADIENT G · 2×2
FROM THE LOSS: 2 × (y − t)
▸ PRESS STEP OR PLAY.
So the backward step turns a 2×2 map into a 4×4 map. It runs the convolution in reverse: instead of gathering a window into one number, each number is spread out into a window. Chapter 8 uses exactly this operation, the transposed convolution, as a layer in its own right, to make maps bigger.
Chapter 2 mentioned that a mathematician’s convolution flips the kernel. That flip shows up here. Read the stamps from a pixel’s point of view: the bottom-right output saw pixel (1, 1) through the top-left weight. As you go from output to pixel in one direction, you go through the kernel in the other. Written as a formula, the input gradient slides the kernel, flipped left to right and top to bottom, over padded with two rings of zeros. Sliding a flipped kernel is exactly what a mathematician’s convolution does.
Knowledge check ▸ 5 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.
1In the module’s 4×4 example, what is the gradient for input pixel (0, 2), in the top row, third from the left?
2On a large image, how many outputs of a 3×3 convolution at stride 1 read a pixel far from the edges?
3In a 4×4 image with a 3×3 kernel, how many outputs read pixel (0, 1)?
4Why does backpropagation need the gradient for each input pixel, not just for each weight?
5In the 4×4 example, the backward step for the input turns the 2×2 map G into what?