How a network learns from its mistakes
Gradient descent is how a neural network learns from its errors. Each small step makes the network a little less wrong, and thousands of steps are what training means.
The network measures how wrong it is, works out which way to change each weight, and steps that way. You can follow every step with one weight and two days of bike hires.
By the end of this lesson
Note
I can explain how a network learns from its errors.
Key words
| Word | What it means | Example |
|---|---|---|
| Loss | one number for how wrong the network is | |
| Gradient | the slope of the loss at the current weight | -10 |
| Learning rate | how big each step is | 0.1 |
| Gradient descent | repeating small steps downhill on the loss | |
| Backpropagation | finding every gradient by working backwards |
The cells students fill in are left blank in this preview.
The loss measures how wrong a network is
Our toy model has one weight. Hires in hundreds equal the weight times the temperature in tens of degrees. Two days are known: 10 degrees gave 2 hundred hires, and 20 degrees gave 4.
For each day, the error is the guess minus the truth. Square it, then take the mean. That mean is the loss. Squaring stops errors cancelling out, and big errors count for more.
Worked example: the loss for four weights
| Weight | Errors | Squared errors | Loss |
|---|---|---|---|
| 0 | -2, -4 | 4, 16 | 10 |
| 1 | -1, -2 | 1, 4 | 2.5 |
| 2 | 0, 0 | 0, 0 | 0 |
| 3 | 1, 2 | 1, 4 | 2.5 |
The cells students fill in are left blank in this preview.
Work out a loss in Python
# One weight, two days: temperature in tens of degrees and hires in hundreds.
xs = [1, 2]
ys = [2, 4]
w = 0
total = 0
for i in range(2):
error = w * xs[i] - ys[i]
total = total + error * error
print("loss:", total / 2)
# Change w to 1.5 and press Run. Is the loss lower?
The gradient says which way is downhill
Think of the loss as a valley, with the weight as your position. The gradient is the slope where you stand. Its sign says which way the loss rises. Its size says how steep it is.
For this model, work out 2 times the error times the input for each day, then take the mean. At a weight of 0 the gradient is -10: the loss falls steeply as the weight goes up.
Worked example: the gradient at four weights
| Weight | Errors | 2 x error x input | Gradient | Move the weight |
|---|---|---|---|---|
| 0 | -2, -4 | -4, -16 | -10 | up, a lot |
| 1 | -1, -2 | -2, -8 | -5 | up |
| 2 | 0, 0 | 0, 0 | 0 | stay |
| 3 | 1, 2 | 2, 8 | 5 | down |
The cells students fill in are left blank in this preview.
Check the gradient at a weight of 1
docker exec jupyter python3 -c "xs = [1, 2]; ys = [2, 4]; w = 1; print(sum(2 * (w * x - y) * x for x, y in zip(xs, ys)) / 2)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A step moves the weight against the gradient
One step makes the new weight the old weight minus the learning rate times the gradient. The minus sign sends the weight downhill.
A small learning rate takes tiny steps and is slow. A huge one jumps over the lowest point and can make the loss worse. Repeating the step is gradient descent.
Worked example: four steps at rate 0.1
| Step | Weight before | Gradient | Change | Weight after |
|---|---|---|---|---|
| 1 | 0 | -10 | +1 | 1 |
| 2 | 1 | -5 | +0.5 | 1.5 |
| 3 | 1.5 | -2.5 | +0.25 | 1.75 |
| 4 | 1.75 | -1.25 | +0.125 | 1.875 |
The cells students fill in are left blank in this preview.
Worked example: four steps at three rates
| Rate | Step 1 | Step 2 | Step 3 | Step 4 | What happens |
|---|---|---|---|---|---|
| 0.01 | 0.1 | 0.195 | 0.2853 | 0.371 | Far too slow |
| 0.1 | 1 | 1.5 | 1.75 | 1.875 | Steady, nearing 2 |
| 0.5 | 5 | -2.5 | 8.75 | -8.125 | Overshoots and grows |
The cells students fill in are left blank in this preview.
Gradient descent on one weight
xs = [1, 2]
ys = [2, 4]
rate = 0.1
w = 0
for step in range(4):
gradient = 0
for i in range(2):
error = w * xs[i] - ys[i]
gradient = gradient + 2 * error * xs[i]
gradient = gradient / 2
w = w - rate * gradient
print("step", step + 1, "weight", round(w, 4))
# Change the rate to 0.5 and press Run.
# Then try 0.2. What happens at the first step?
One step from a weight of 1.5
docker exec jupyter python3 -c "w = 1.5; g = sum(2 * (w * x - y) * x for x, y in zip([1, 2], [2, 4])) / 2; print(g, round(w - 0.1 * g, 4))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Backpropagation finds every gradient
A real network has thousands of weights, and each needs its own gradient. Backpropagation finds them all in one sweep.
The forward pass gives the loss. Then the sweep goes backwards from the output layer. Each layer passes back how much it added to the error. One pass forward and one back give every gradient.
A common mistake
Warning
Many people think a bigger learning rate always trains faster. Up to a point it does. Past that point each step overshoots the lowest loss, and the weight bounces further away, as the row for 0.5 shows. Start small, and raise the rate only while the loss keeps falling.
Going further
In the descent code, set the rate to 0.25 and run ten steps instead of four. Does the weight settle at 2?
Then use days where the best weight is 3, with ys = [3, 6]. Work out the first gradient by hand before you run it.