Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Gradient descent and the learning rate

After this lesson: I can explain how a network learns from its errors.

60 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

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

WordWhat it meansExample
Lossone number for how wrong the network is
Gradientthe slope of the loss at the current weight-10
Learning ratehow big each step is0.1
Gradient descentrepeating small steps downhill on the loss
Backpropagationfinding 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

WeightErrorsSquared errorsLoss
0-2, -44, 1610
1-1, -21, 42.5
20, 00, 00
31, 21, 42.5

The cells students fill in are left blank in this preview.

Work out a loss in Python

Code: 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

WeightErrors2 x error x inputGradientMove the weight
0-2, -4-4, -16-10up, a lot
1-1, -2-2, -8-5up
20, 00, 00stay
31, 22, 85down

The cells students fill in are left blank in this preview.

Check the gradient at a weight of 1

Command
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

StepWeight beforeGradientChangeWeight after
10-10+11
21-5+0.51.5
31.5-2.5+0.251.75
41.75-1.25+0.1251.875

The cells students fill in are left blank in this preview.

Worked example: four steps at three rates

RateStep 1Step 2Step 3Step 4What happens
0.010.10.1950.28530.371Far too slow
0.111.51.751.875Steady, nearing 2
0.55-2.58.75-8.125Overshoots and grows

The cells students fill in are left blank in this preview.

Gradient descent on one weight

Code: python
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

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

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.