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:
Work out the loss of the current line.
Work out which way the loss falls if w and b move a little. That is the gradient.
Move w and b a small step that way. The step is the gradient times the learning rate.
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:
Step
w
b
slope w
slope b
MSE
0
0.000
0.000
-0.604
-2.727
2.088
1
0.060
0.273
-0.544
-2.182
1.384
2
0.115
0.491
-0.490
-1.745
0.928
3
0.164
0.665
-0.441
-1.396
0.631
4
0.208
0.805
-0.397
-1.117
0.437
5
0.248
0.917
-0.357
-0.894
0.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.
The loss of every line on Premier League. The bottom of the bowl is still above 0, because no line goes through every team.