“Wrong” is not enough
Our model is supposed to give 6 for input 2, but it says 2. Wrong. If it said 5, that would be wrong too, but much closer! A plain “right or wrong” throws that information away.
So we need an error function, also called a loss function. It takes the prediction and the target and outputs a number. The smaller, the better. Zero means: perfect.
A little preview: when people training a language model stare at a curve with bated breath, it is almost always exactly this number.
First compute the difference
The obvious idea: prediction minus target. 2 − 6 = −4. The minus means: too low. With prediction 10 it would be 10 − 6 = 4, so too high.
The problem: with several examples, errors cancel out. Once −4, once +4, sum zero, and a model that is badly off twice would look flawless. Oops.
Square the difference
The fix: multiply the difference by itself. (−4) × (−4) = 16, and 4 × 4 = 16. The sign is gone, and errors can no longer cheat each other away.
| Prediction | Target | Difference | Squared error |
|---|---|---|---|
| 2 | 6 | −4 | 16 |
| 4 | 6 | −2 | 4 |
| 5 | 6 | −1 | 1 |
| 6 | 6 | 0 | 0 |
| 7 | 6 | 1 | 1 |
| 10 | 6 | 4 | 16 |
Side effect: big errors get punished especially hard. Twice as far off means an error four times as large.
The formula describes exactly this table
The prediction is called (“y-hat”), the target and the error number , for loss.
Read the formula: subtract the target from the prediction, multiply the result by itself, and call the whole thing L.
The hat only marks “this was computed by the model”. The raised 2 means “squared”. With 2 and 6 you get (2−6)²=16, exactly the first row of the table.
Several examples together
Take our model “input times weight” with weight 2 and the examples 1→3, 2→6 and 3→9. It says 2, 4 and 6. The differences are −1, −2 and −3, squared 1, 4 and 9.
We take the average of those: (1+4+9)/3=14/3≈4.667. That is the mean squared error, MSE for short.
Why the average instead of the sum? So the number doesn't grow just because you have more examples. The catch: a single wild outlier can get lost in the average. That is why we will also look at concrete examples later.
Measuring the error is not learning yet
Now we know: the error is 16. But which dial should we turn? In which direction? How far? The 16 alone doesn't tell us.
With a single dial we could just try things out. With nine dials that gets tedious, with billions impossible. In the next chapter you will learn a clever trick that reveals the right direction for every dial. The loss is the goal; the trick is the way there.
Why not just the distance?
You could also simply take the distance, without squaring. For 2 instead of 6 that would be 4 instead of 16. That works too! Different error measures rate big and small errors differently and therefore lead to somewhat different learning behavior.
So there is no one error for everything. For numbers, the square is convenient. For a language model, on the other hand, we want to rate how well it predicted the next word. There will be a separate error measure for that later, and there too we start with number examples.
What a small error proves, and what it doesn't
A small error means: on these examples the model fits well. It does not automatically mean it works on new examples too. And if the data contains mistakes, the model will dutifully learn them along with everything else.
That is why the data gets split. The model learns from the training data. Validation data is held back and used in between to check whether the model can handle the unknown too, like a practice exam with questions you haven't seen before.
In the experiment
Move only the prediction and watch the difference and its square. Then set two predictions that are equally far to the left and to the right of the target. The error is the same, even though the needed correction points in opposite directions. Size and direction are simply two different things.
Key points
- The loss is a number that measures how far off the model is. Smaller is better.
- Squared error means: difference times itself. Nothing can cancel out, and big errors count extra.
- A small loss on the training data does not yet guarantee success on new data.