Measuring 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.
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.
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.
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.
Building the loss
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.
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.
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.
Using the 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.
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.
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.
What happens
;; step 1/10 · 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
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. 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.