Machine learning with football · Chapter 3 · Logistic regression

Loss and regularization

Training works as in chapter 2, with two changes: a loss made for probabilities, and a brake on the weights.

Estimated time: 15 minutes · page 3 of 4

Why not squared loss?

A straight line changes at the same rate everywhere, which is why squared loss suited chapter 2. The sigmoid doesn't. Around 50% a small step in z moves the probability a lot, and out at the edges it barely moves at all. σ(5) is 0.993, σ(7) is 0.999 and σ(10) is 0.99995. Squared loss sees almost no difference between those, even though a model that says 99.995% is far more sure of itself than one that says 99.3%.

Log loss fixes that by taking the logarithm of the probability the model gave to what really happened:

Log loss = average of −[ y · ln y′ + (1 − y) · ln(1 − y′) ]

Only one half counts for each match. Say the model gives the home side 80%. If they win, the match costs −ln 0.8 = 0.22. If they don't, it costs −ln 0.2 = 1.61. Say 99% and lose, and it costs 4.6. Being sure and wrong is expensive, which is exactly what you want from a model that prices matches.

The board shows both losses for your curve. With w = 0 and b = 0 every match gets 50%, and the log loss is ln 2 = 0.693, the score of a coin flip, and any curve that knows some football scores lower.

Check yourself

The model gives the home side 99% and they lose. What does that match add to the log loss?
Why does logistic regression train with log loss instead of squared loss?

Training, and putting on the brakes

The rest is chapter 2 again. Reset the board and press 10 steps a few times. Gradient descent moves w and b downhill on the log loss until the curve runs through the squares and the loss curve goes flat. There is no exact formula this time, so Show best curve is the result of 2,000 such steps.

The second change is regularization, and with many features it matters a lot. A model with dozens of features can often find a combination that separates its training matches perfectly. Then the cheapest way to lower the log loss is to push those weights toward infinity, so every prediction becomes 0% or 100%. The training loss heads to 0 while the model gets worse at matches it hasn't seen. That's overfitting, the subject of chapter 7.

Two common brakes:

  • L2 regularization adds λ · w² to the loss, so a large weight has to earn its keep. λ, the regularization rate, is a hyperparameter you choose.
  • Early stopping ends training after a set number of steps, while the loss is still falling.

One feature can't overfit much, but you can still watch the brake work. Set λ to 1 and press 100 steps. The curve flattens, because every bit of w now costs extra. At 0.01 it hardly changes.

Check yourself

You set λ to 1 on the board and train. What happens to the curve?
What is early stopping?

Terms on this page: Early stopping, L2 regularization, Log loss, Overfitting, Regularization, Regularization rate (λ), Squared loss

Leagues