AAmit’s LibraryLEARN · PRACTISE · REFLECT

From slope to a model’s first update

Question: A program predicts 3 for an example whose correct answer is 4. Should the program increase its slope, decrease it, or leave it alone?

Before you start: You only need multiplication, subtraction and the idea that a graph plots inputs against outputs.

Draw a line, then measure the gap

Imagine a program estimating how many tiles it can process in each time interval. Our synthetic record says that it processed 4 tiles in 2 seconds. We try a model with no fixed starting offset: predicted tiles = rate × seconds. A trial rate of 1.5 tiles per second predicts 3 tiles at 2 seconds. Draw a line through (0, 0) and (2, 3), then mark the actual point (2, 4). The vertical gap is 1 tile.

This lesson defines the residual as observed minus predicted, so the residual is 1. Some code uses predicted minus observed and calls that the error; here, the signed error is −1. Before comparing results, check which convention a formula uses.

Turn the error into a loss

A model can overpredict one example and underpredict another. If you just add signed errors, they can cancel. Squared loss treats either direction as a mistake: L = (prediction − actual)². At a rate of 1.5, the loss is 1 tile². Near that rate, compare two possibilities:

How a small change in rate changes the loss
Rate (tiles per second)Prediction at 2 secondsSquared loss (tile²)
1.42.81.44
1.53.01.00
1.63.20.64

Increasing the rate a little from 1.5 lowered the loss in this example. That is a different question from asking about the slope of the prediction line. The line describes how predicted tiles change as time changes; the loss curve describes how error changes as the rate parameter changes for the same example.

Take a gradient step

Writing the rate as w, the loss is L(w) = (2w − 4)². If you already know derivatives, the chain rule gives L′(w) = 2(2w − 4) × 2. At w = 1.5 this is −4. If you are new to derivatives, read that negative number as a local direction: a small increase in w is expected to lower the loss. With an illustrative learning rate of 0.1, the update is:

new rate = 1.5 − 0.1 × (−4) = 1.9

The new prediction is 3.8 tiles and the loss is 0.04 tile². That is an improvement on this record. To judge a trained model, you need additional examples and an evaluation set not used to fit it.

Try it

  1. Start at rate 2.5 instead. What are the signed residual, the derivative, and the new rate after a step of size 0.1?
  2. Now return to 1.5 and make a step of size 0.5. Did you improve the loss? What does this show?
Check your answers

Answers: (1) The prediction is 5, the residual is −1, the derivative is +4 and the updated rate is 2.1. (2) The rate becomes 3.5, the prediction becomes 7 and the loss rises from 1 to 9. A correct local direction does not make an arbitrarily large step safe.

Glossary: A parameter is a model value you can fit; a residual is the observed value minus its prediction; a loss scores how far the prediction misses; a gradient gives the local slope with respect to one or more parameters; a learning rate controls the size of the update.

Back to the mathematics outline