RECEPTIVE FIELD

Ch 7 ▸ Backprop through a convolution ▸ Module 3/4

  1. 1Loss and gradients
  2. 2Gradients for the weights
  3. 3Gradients for the input
  4. 4Learning a kernel

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 hh. Every output whose window covers that pixel changes by hh times the weight that sat on it. So each of those outputs contributes (GG × 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:

With G=[200−2]G = \begin{bmatrix} 2 & 0 \\ 0 & -2 \end{bmatrix} from the last module:

(2×0)+(0×(−2))+(0×0)+((−2)×(−1))=2(2 \times 0) + (0 \times (-2)) + (0 \times 0) + ((-2) \times (-1)) = 2

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 2×(−1)=−22 \times (-1) = -2.

FIG 7.3 ▸ GRADIENTS FOR THE INPUT LOSS = 2

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

Each copy of the input shows the 3×3 kernel at one position: the outlined squares are the nine pixels it covers, with the weight on each written small underneath. The solid square is the picked pixel. Red cells are above zero, blue below.

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 GG 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 2×2 \times the kernel onto the top-left 3×3 window, the bottom-right output stamps −2×-2 \times the kernel onto the bottom-right window, and the other two stamp zeros. The full input gradient is the sum of the stamps:

[−2020−424−2−242−4020−2]\begin{bmatrix} -2 & 0 & 2 & 0 \\ -4 & 2 & 4 & -2 \\ -2 & 4 & 2 & -4 \\ 0 & 2 & 0 & -2 \end{bmatrix}

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 GG 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 GG is 0; the last stamp, −2 × the kernel, lands on the bottom-right window, and where it overlaps the first, the two add.

FIG 7.4 ▸ BACKWARD: SPREAD READY

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.

Direction
The same four kernel positions, in the same order, both ways. Forward, the outlined 3×3 window is gathered into one output. Backward, that output's gradient is spread back over the same window, scaled by the kernel, and where windows overlap the stamps add up. Red is above zero, blue below.

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 GG 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.

  1. 1In the module’s 4×4 example, what is the gradient for input pixel (0, 2), in the top row, third from the left?

  2. 2On a large image, how many outputs of a 3×3 convolution at stride 1 read a pixel far from the edges?

  3. 3In a 4×4 image with a 3×3 kernel, how many outputs read pixel (0, 1)?

  4. 4Why does backpropagation need the gradient for each input pixel, not just for each weight?

  5. 5In the 4×4 example, the backward step for the input turns the 2×2 map G into what?

0 of 5 answered correctly.

🔒 Next module: Learning a kernel