RECEPTIVE FIELD

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

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

Learning a kernel.

🔒 This module is locked

It builds on Gradients for the input. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.

Now all the pieces are in place. Training a convolution is a loop of four steps:

  1. Forward. Slide the kernel over the input and compute the output map.
  2. Loss. Compare the output map with the target, and compute each output’s gradient GG.
  3. Backward. Turn GG into a gradient for each of the nine weights, as in the Gradients for the weights module.
  4. Step. Move every weight against its gradient: weight − step size × gradient.

Then repeat.

Learning the edge kernel

Figure 7.5 runs that loop on the 32×32 duck. The target is the edge kernel’s output from chapter 2, and the kernel being trained starts with small, arbitrary weights. Can gradient descent find the edge kernel, knowing only the duck and the map it should make?

The loss here is the mean squared error: the squared error added over all 900 outputs and then divided by 900, so it’s the average squared miss per output. Dividing keeps the gradients the same size however big the map is, so the same step size works on any image.

FIG 7.5 ▸ LEARN A KERNEL STEP 0 · LOSS –

TARGET

CURRENT OUTPUT

KERNEL · SMALL: TARGET

GRADIENT

▸ LOSS IS THE MEAN SQUARED ERROR OVER ALL 900 OUTPUTS. ITS AXIS IS LOGARITHMIC: EACH LINE IS 100 TIMES SMALLER.

Step size
Speed
The input is the duck, and the target is what the edge kernel makes from it. The kernel starts with small, arbitrary weights. Each step computes the output, compares it with the target, finds every weight's gradient, and moves each weight against its gradient. Red is above zero, blue below.

Press Train. The loss starts at 0.35. With a step size of 0.2 it falls below 0.001 after 319 steps, and after 806 steps every weight is within 0.05 of the edge kernel’s. Left running, the kernel becomes the edge kernel, to as many decimal places as the figure shows. The output map, which starts out nearly blank, takes on the red and blue edges of the target.

Watch the gradient grid while it trains. Early on, it’s large and the weights move fast. As the output gets closer to the target, every GG gets smaller, so every weight gradient does too, and the weights settle instead of overshooting, just like the single weight in figure 7.1.

Try the other step sizes. At 0.1, everything takes twice as many steps: 638 to get the loss under 0.001, and 1,613 to bring every weight within 0.05. At 0.05, it’s twice as slow again. At 0.3, the loss grows instead of shrinking, and after a few dozen steps it’s in the billions. For this image the cutoff is just under 0.25; above it, each step overshoots further than the last.

Three layers

One kernel learning to copy another kernel is the simplest case. Here’s a harder job: mark the duck. Given the gray duck, output 1 on every duck pixel, beak and eye included, and 0 on the sky and water. That’s 289 duck pixels out of 1,024.

A single 3×3 kernel can’t do this, for two reasons. First, the answer has to switch from 0 to 1 at some brightness: the duck’s body is 1.00, the sky 0.56. A kernel only multiplies and adds, so its output rises smoothly with brightness instead of switching. Second, the duck’s eye is black, darker than anything else in the image, yet it’s part of the duck. Getting it right means seeing the white feathers around it, which takes more than a 3×3 view.

So figure 7.6 stacks three convolution layers. Layer 1 reads the duck and makes 4 maps; layer 2 reads those 4 and makes 4 more; layer 3 reads those and makes the 1 output map. Each layer uses same padding from chapter 3, so every map stays 32×32, and as chapter 6 showed, three stacked 3×3 layers give each output a 7×7 receptive field. Counting with chapter 4’s formula, the layers have (3×3×1×4)+4=40(3 \times 3 \times 1 \times 4) + 4 = 40, (3×3×4×4)+4=148(3 \times 3 \times 4 \times 4) + 4 = 148 and (3×3×4×1)+1=37(3 \times 3 \times 4 \times 1) + 1 = 37 parameters: 225 in all, every one of them learned.

ReLU

There’s one more piece between the layers. Stacking convolutions alone doesn’t help: a convolution of a convolution only multiplies and adds, so three of them in a row compute nothing that one bigger kernel couldn’t. To get the switch from 0 to 1 the job needs, something has to bend.

ReLU, short for rectified linear unit, is the simplest thing that bends: it keeps every positive number as it is and replaces every negative number with 0. Applied to a map, it’s applied to each value separately. Layers 1 and 2 put their outputs through a ReLU before the next layer reads them. That small cut is enough: with it, stacked layers can build up sharp yes-or-no answers out of smooth sums. Almost every CNN puts a ReLU, or something like it, after its convolutions.

Backpropagation goes through a ReLU easily. Where the ReLU’s input was positive, it passed the value through unchanged, so it passes the gradient back unchanged. Where the input was negative, the output was stuck at 0, and nudging the input changed nothing, so the gradient there is 0.

FIG 7.6 ▸ THREE LAYERS: FIND THE DUCK STEP 0 · LOSS –

INPUT

LAYER 1 · 4 MAPS

LAYER 2 · 4 MAPS

LAYER 3 · OUTPUT

TARGET

Step size
Start
Speed
Three 3×3 convolution layers with same padding: 1 channel in, 4, 4, then 1 out, with ReLU after layers 1 and 2. Each step runs the duck forward through all three, compares the output with the target, sends the gradients back through all three, and moves all 225 weights and biases. Hidden maps are drawn black at 0 and white at their own largest value. Start picks one of three fixed sets of random starting weights.

Press Train. The network starts with random weights. Its output is below 0.5 everywhere, so it calls every pixel “not duck”, which is right for the 735 pixels of sky and water and wrong for all 289 duck pixels. The loss starts at 0.43. As it trains, the duck appears in the output. After 500 steps it gets 1,004 pixels right and its loss is 0.028; after 3,000 steps, 1,008, or 98%. Each step is the same four parts as before, only longer: forward through three layers, the loss, gradients back through three layers and two ReLUs, and a step for all 225 parameters.

Watch the hidden maps while it trains. No one told them what to show; each one becomes whatever helps layer 3 get the answer right, and it doesn’t have to be anything a person would put a name to. Try the other starting points. The output ends up about as good each time, but the hidden maps come out different. There are many sets of weights that do the job, and where training ends up depends on where it started.

Now try start 2 with a step size of 0.2. The network never gets past 735. A few large early steps pushed every value in layer 2’s maps below zero, so the ReLU turns all four maps to 0. Then the gradient that reaches layer 2 is cut to 0 everywhere, layers 1 and 2 stop learning, and layer 3 is left reading blank maps. This is called a dead ReLU, and it’s a real hazard in training: too large a step doesn’t always blow up, as it did in figure 7.5. Sometimes it switches part of the network off for good.

How real training differs

These figures are small, honest versions of what happens inside a CNN, with three differences.

The core is the same: forward, loss, backward, step. Every weight in every kernel of every layer gets its own gradient, and moves a little downhill.

Knowledge check ▸ 7 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 what order does one training step go?

  2. 2A map has four outputs, with errors (y − t) of 1, −1, 2 and 0. What is the mean squared error?

  3. 3In figure 7.5, what happens with step size 0.3?

  4. 4A ReLU receives the values 2, −3 and 0.5. What is the sum of its three outputs?

  5. 5During backpropagation, what happens to the gradient at a spot where a ReLU’s input was negative?

  6. 6In figure 7.6 with start 2 and step size 0.2, why does the network never learn the duck?

  7. 7How is training a real CNN different from figure 7.5?

0 of 7 answered correctly.

🔒 Chapter 8: Upsampling and transposed convolution