Loss: measuring mistakes
About this lesson
A model learns by making its mistakes smaller, so first it needs one number that says how wrong it is: the loss. Follow four deliveries through a tiny model, watch plain errors cancel out to a misleading 0, square them, and average them into the mean squared error. Then use the loss to compare models, see training as a loop that pushes the loss down, and meet log loss, the loss classifiers use.
Models make mistakes
A model makes guesses, and some are wrong. To improve it, we first need one number that says how wrong it is overall. That number is the loss, also called the cost. Smaller loss means smaller mistakes.
model makes a guess for each example loss one number: how wrong the guesses are, overall loss big -> the guesses are far off loss small -> the guesses are close loss 0 -> every guess is exactly right
Read the lesson step by step
Models make mistakes
A model makes guesses, and some are wrong. To improve it, we first need one number that says how wrong it is overall. That number is the loss, also called the cost. Smaller loss means smaller mistakes.
model makes a guess for each example loss one number: how wrong the guesses are, overall loss big -> the guesses are far off loss small -> the guesses are close loss 0 -> every guess is exactly rightFour real deliveries
Our task: guess how many minutes a delivery takes from its distance. We have four past deliveries with their real times: 1 km took 3, 2 km took 3, 3 km took 8 and 4 km took 6 minutes. The real times are the labels.
training data: distance -> real time (the label) 1 km 3 min 2 km 3 min 3 km 8 min 4 km 6 min the model never sees the real time when it guesses; we use the real time to check the guessThe model guesses
Our model is one rule with one adjustable number, w: guess = w × km. Start with w = 2. Each distance goes through the model: the guesses are 2, 4, 6, 8 minutes, one per delivery.
w = 2 guess = w * x 1 km -> 2 * 1 = 2 min 2 km -> 2 * 2 = 4 min 3 km -> 2 * 3 = 6 min 4 km -> 2 * 4 = 8 minEach error
The error is real time minus guess. 3 − 2 = +1, 3 − 4 = −1, 8 − 6 = +2, 6 − 8 = −2. A plus means the guess was too low; a minus, too high.
err = y - guess (real - guess) 3 - 2 = +1 3 - 4 = -1 8 - 6 = +2 6 - 8 = -2 + guess too low - guess too highPlain errors cancel out
Why not just average the errors? +1 − 1 + 2 − 2 = 0, so the average error is 0, as if the model were perfect. But every guess was wrong: misses that are too high cancel misses that are too low.
sum of errors: +1 - 1 + 2 - 2 = 0 average error: 0 / 4 = 0 looks perfect, but all four guesses were wrong so the plain average is a bad lossSquare every error
The fix: square each error, which means multiply it by itself. (+1)² = 1, (−1)² = 1, (+2)² = 4, (−2)² = 4. Squares are never negative, so nothing cancels, and a miss twice as big costs four times as much.
total += err ** 2 (** is "to the power of") (+1)² = 1 (-1)² = 1 (+2)² = 4 (-2)² = 4 a miss of 2 costs 4, a miss of 1 costs 1Average them: the loss
Add the squares, 1 + 1 + 4 + 4 = 10, and divide by the 4 deliveries: 2.5. This is the mean squared error (MSE), our loss: one number for the whole data set, in minutes squared.
loss = total / len(data) total = 1 + 1 + 4 + 4 = 10 loss = 10 / 4 = 2.5 MSE = mean squared error the program prints 2.5Compare models by loss
Now try other values of w and measure each one. With w = 1 the loss is 8.5; with w = 3 it is 11.5; with w = 2 it is 2.5. Each bar shows one loss. Lower is better, so w = 2 wins.
same data, three models, three losses w = 1 guesses 1, 2, 3, 4 loss 8.5 w = 2 guesses 2, 4, 6, 8 loss 2.5 w = 3 guesses 3, 6, 9, 12 loss 11.5 the lowest bar is the best model of the threeTraining lowers the loss
Training is a loop: guess, measure the loss, nudge w to lower it, repeat. From w = 1, nudges to 1.5 and then 1.9 bring the loss down: 8.5, then 3.625, then 2.425. For this data, no w does better than 1.9.
the training loop: 1 guess 2 measure the loss 3 nudge w 4 repeat w = 1 loss 8.5 w = 1.5 loss 3.625 w = 1.9 loss 2.425 best possible here: w = 1.9, loss 2.425Log loss for classifiers
Classifiers, which pick a label, usually use log loss: −log(p), where p is the chance the model gave the right answer. p = 0.9 costs 0.11; p = 0.1 costs 2.30. Being sure and wrong costs the most.
log loss = -log(p) (natural log) p = chance given to the right answer p = 0.9 -> -log(0.9) = 0.11 p = 0.1 -> -log(0.1) = 2.30 p = 1 -> -log(1) = 0.00 (sure and right) it is also called cross-entropy loss
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What is a loss function for?
The loss (also called the cost) measures how wrong the model's guesses are overall. Training changes the model to make that number smaller.
02A model's errors are +3 and −3. What is the average of the plain errors, and why is it a bad loss?
+3 − 3 = 0, so the average looks perfect. Squaring first gives 9 and 9, which cannot cancel.
03The errors are +1, −1, +2 and −2. What is the mean squared error?
Squares: 1, 1, 4, 4. Their sum is 10, and 10 divided by the 4 examples is 2.5.
04Model A has a loss of 8.5 on some data and model B has a loss of 2.5 on the same data. Which fits the data better?
Lower loss means smaller mistakes on that data. Training is the search for the model numbers that make the loss as small as possible.
05A classifier gives the right answer a probability of 0.1. What does log loss say about that?
Log loss is −log(p) with the natural log. p = 0.9 costs about 0.11; p = 0.1 costs about 2.30. Being confident and wrong costs the most.
Glossary
- Loss (cost)
- One number that measures how wrong a model's predictions are on some data. Training tries to make it smaller.
- Error
- For one example: the real value minus the model's prediction. Positive means the guess was too low.
- Squared error
- An error multiplied by itself. It is never negative, and big misses count much more than small ones.
- Mean squared error (MSE)
- The average of the squared errors over all the examples. A common loss when the model predicts a number.
- Parameter
- A number inside the model that training can change, such as w in guess = w × km.
- Training loop
- Guess, measure the loss, nudge the parameters to lower it, and repeat.
- Log loss (cross-entropy)
- The usual loss for classifiers: −log(p), where p is the probability the model gave the right answer.
Notes and sources
- The mean squared error here is computed exactly as written: add the squared errors, divide by how many there are. Some textbooks (such as Stanford CS229) put ½ in front instead; that changes the number, not which model is best.
- The four deliveries are made up, small numbers chosen so the arithmetic is easy to follow. Real models learn from thousands of examples and many numbers, not one.
- Squared error is not the only choice. Mean absolute error (average of the error sizes, ignoring signs) also stops cancelling, but punishes big misses less.
- Log loss uses the natural logarithm (Python math.log). It is the same as cross-entropy loss for a label with two choices.
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma): least squares cost function, logistic regression
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), chapter 5: mean squared error and maximum likelihood
- Google Machine Learning Crash Course, Linear regression: Loss (MSE, MAE, outliers)
- Stanford CS231n, Linear classification notes: loss functions and the softmax cross-entropy loss
- scikit-learn User Guide, Log loss
- Python Library Reference: math.log (natural logarithm)
Quiz Unlocks after the last step.