Machine learning with football · Chapter 2 · Linear regression

Gradient descent

How a computer finds the best line on its own: work out which way is downhill, take a small step, repeat.

Estimated time: 15 minutes · page 4 of 7

Downhill, one step at a time

You found a decent line by eye. A computer gets there with gradient descent, and it starts with no idea at all, w = 0 and b = 0. Then it repeats four things:

  1. Work out the loss of the current line.
  2. Work out which way the loss falls if w and b move a little. That is the gradient.
  3. Move w and b a small step that way. The step is the gradient times the learning rate.
  4. Go back to 1, until the loss stops falling.

For MSE the gradient has a simple form. With the error y′ − y of each team:

slope for w = average of 2 · (y′ − y) · x

slope for b = average of 2 · (y′ − y)

Both come out negative at the start, because every prediction is too low. Here are the first steps on Premier League, at a learning rate of 0.1:

Stepwbslope wslope bMSE
00.0000.000-0.604-2.7272.088
10.0600.273-0.544-2.1821.384
20.1150.491-0.490-1.7450.928
30.1640.665-0.441-1.3960.631
40.2080.805-0.397-1.1170.437
50.2480.917-0.357-0.8940.310

Step 1 is just the rule from above: new b = 0 − 0.1 × (-2.727) = 0.273. The same for w. Every step the loss drops, fast at first and then less and less.

Watch it converge

On the board, reset to 0 and press 10 steps a few times. The loss curve under the buttons drops steeply, then bends and goes flat. Once more steps no longer lower the loss, the model has converged. In practice the loss still wobbles a tiny bit, so you train until it has settled.

The loss surface

Every pair of w and b has a loss. Shade them all and you get the map below, dark where the loss is low. For a linear model it is always a single bowl, which is called convex. There is one lowest point and no false dips to get stuck in, so converging means you have found the best line there is. The orange path is your descent from the board, and the cross marks the bottom.

w = −1w = 2b = 0b = 3weight w →
The loss of every line on Premier League. The bottom of the bowl is still above 0, because no line goes through every team.

Check yourself

What does gradient descent do?
Goal difference per game averages about 0 across a league. So where should the bias b end up?
Training has converged at an MSE of 0.04. Why not 0?

Terms on this page: Convergence, Convex, Gradient, Gradient descent, Loss curve

Leagues