Ch 7 ▸ Backprop through a convolution ▸ Module 1/4
Loss and gradients.
🔒 This module is locked
It builds on Not every pixel counts. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.
To improve a network, you first need a way to say how wrong it is.
Loss
A loss is a single number that measures how far a network’s outputs are from the outputs you wanted, the targets. Zero means every output is exactly right; bigger means worse. Training is the search for weights that make the loss small.
This chapter uses the simplest loss, the squared error: subtract the target from the output and square the difference, . Squaring makes every error count as positive, so a miss above the target and a miss below it both add to the loss, and it makes big misses count much more than small ones. For a whole map of outputs, add up the squared error of each.
One weight
Take the smallest network there is: one input , one weight , and one output . Let the input be 2 and the target 6. The right weight is obviously 3, but suppose the network starts at :
Which way should move? Try nudging it. At , the output is 2.02 and the loss is . A nudge of 0.01 lowered the loss by 0.1596, about 16 times the nudge. So near , the loss changes by about −16 for every unit moves.
That rate is the gradient of the loss with respect to : how much the loss changes per unit change in the weight, for a very small change. It’s the slope of the loss, drawn as a curve against the weight. A negative gradient means increasing the weight lowers the loss; a positive gradient means increasing it raises the loss. The bigger the gradient, the steeper the slope.
Computing it without nudging
Nudging works, but a real network has millions of weights, and nudging each one separately would take millions of extra runs. Instead, the gradient can be computed directly, by splitting the path from weight to loss into steps and multiplying their rates:
- How fast the loss changes with the output. For the squared error that’s . Here, .
- How fast the output changes with the weight. Since , every unit of adds to the output. Here, 2.
Multiply them: , the same answer the nudge gave. This step-by-step multiplying is called the chain rule: when one quantity drives a second, and the second drives a third, the rate from first to third is the product of the rates along the way. So
Gradient descent
Knowing the slope, move the weight downhill: against the gradient. Gradient descent is the method that does this over and over:
The step size, also called the learning rate, is a small number you choose that sets how far each step goes. With a step size of 0.1, the first step is . Then , and the loss drops from 16 to .
y = 1 × 2 = 2
LOSS = (2 − 6)² = 16
GRADIENT = 2 × (2 − 6) × 2 = −16
NEXT w = 1 − 0.1 × (−16) = 2.6
Press Step a few times in figure 7.1. As gets close to 3, the slope flattens, the gradient shrinks, and so do the steps, so gradient descent slows down as it arrives instead of running past.
Now set the step size to 0.3 and reset. The first step jumps from to , past the bottom and higher up the other side: the loss goes from 16 to 31.36. The next step overshoots even further, and the loss keeps climbing: 61.47, then 120.47. For this curve, any step size above 0.25 makes every step land further out than the last. Too small a step size is slow; too large and training falls apart. Picking it well is one of the practical arts of training networks.
Backpropagation
A network has many layers, and the loss sits at the very end. The gradient of a weight deep inside has to account for every layer between that weight and the loss. Backpropagation is the method for computing all of a network’s gradients at once: start from the loss and work backward, one layer at a time, using the chain rule at each layer to turn the gradient of the layer’s output into gradients for its weights and its input. Every layer only needs to know how to do its own step. The next two modules work out that step for a convolution.
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.
1A one-weight network computes y = w × x. With x = 3, w = 2 and target 12, what is the squared-error loss?
2For the same network (x = 3, w = 2, target 12), what is the gradient of the loss with respect to w?
3Take one gradient descent step from w = 2 with step size 0.01, using that gradient. What is the new w?
4A weight’s gradient is negative. What does that tell you?
5What happens when the step size is too large?