Training Methods Intermediate

Loss Function

A single number for how far off the answer was

Key points
  • A loss function is a rule that boils down how wrong an answer was into a single number.
  • Training is the work of shrinking that number. Every change made to the model traces back to it.
  • What gets penalized, and how hard, decides the outcome. Change the penalty rules and you get a different model.
  • The rules differ depending on whether the answer is a number or a label, because the two kinds of mistakes are measured differently.
  • A low loss doesn't guarantee a good result. There's always a gap between what you want to measure and what you can actually measure.
Contents

1The analogy

A bakery installing a new oven doesn't get good bread on the first tray. So the baker lines today's loaf up next to a sample loaf and fills out a scorecard: how much darker the crust is than the sample, how underbaked the center is, how short the rise fell. Each item gets a penalty, and the penalties get added up. That single number is today's grade. A 12 is better than yesterday's 18. The next tray gets adjusted to bring that number down.

What matters most here is how the scorecard was written. Load all the penalty onto a dark crust and you'll end up with tray after tray of undercooked centers, because the oven learns to play it safe on the one thing that gets punished hardest. Write the scorecard poorly and every tray after this one inherits the mistake. A loss function is that scorecard.

2In detail

Folding every mismatch into one number

Once the model produces an answer, it gets compared against the correct one. The gap can show up in different ways — too high, too low, off on one detail or another. A loss function takes all of that and folds it into a single number using a fixed rule.

There's a reason it has to be one number. Comparing two trays to see which is better only works if there's one comparable value. If the crust looks better on one tray and the center looks better on the other, there's no way to decide what to fix next.

Averaging the penalty across an entire batch of loaves, rather than judging one at a time, follows the same logic. The whole tray needs a single grade before the next tray can be planned.

The penalty rules decide the outcome

The same loaf scores differently depending on how the scorecard is written. Some scorecards add up the mismatch directly. Others square it first, then add. Squaring makes small mismatches barely register while big ones jump out. One badly burnt loaf can swamp the other nine.

So choosing the scorecard isn't a matter of taste — it's a policy decision. If a large mismatch has to be avoided at almost any cost, squaring is the right call. If you don't want a handful of odd loaves to swing the grade, adding the raw mismatch is safer.

Back at the bakery, the scorecard changes depending on what customers hate most. In a neighborhood that gets furious over burnt crust, pile the penalty onto burning, and the oven learns to run a little cooler.

Measuring and fixing are different jobs

A loss function's job ends at measuring. It reports a 12 — it doesn't say which way to turn the dial. Deciding which direction to move, and by how much, belongs to the next step.

That puts one condition on the scorecard: a small change should produce a small change in score. If nudging the temperature by one degree makes the score jump abruptly, there's no way to tell which way to turn it. That's why loss functions are written as smooth rules. Counting the number of correct answers looks great to a person, but a small tweak rarely changes that count, so it's useless for training.

The target shapes the rule

If the answer is a number, the mismatch is measured directly — how many millimeters short the rise fell, say. That's the kind of rule used for predicting numeric values.

If the answer is a label, it works differently. Instead of right-or-wrong, it looks at how confident the model was. It weighs how much belief was placed on the correct option, and the biggest penalty goes to a confident wrong answer. Being wrong while unsure hurts less than being wrong while certain. That pushes the model to sharpen its confidence, not just get the label right.

A low score isn't the same as a good result

A scorecard is a human-made stand-in. What you actually want is bread customers call delicious, but all you can measure is crust color and rise. So it's possible to score well and still taste mediocre.

Models run into the same trap. Loss drops visibly, yet the model acts strange in practice. Sometimes it only lowered its penalty on the practice loaves and falls apart on anything new; sometimes it ignores a quality the scorecard never captured. That's why the loss used for training and the metrics people actually care about get tracked separately, side by side.

3More precisely

A loss function is a function that takes a model's output and a target value and returns a single real number. Training becomes the problem of minimizing that number with respect to the parameters, and the direction that lowers it comes from the gradient. That's why loss functions are usually built to be differentiable.

The bakery comparison breaks down in a few places. A human grades the bakery scorecard by eye, but loss gets computed automatically for every single example, with hundreds of thousands of them averaged at once. A loaf gets graded once it's fully baked, but during training, the direction to move is calculated for intermediate values too, not just the finished output. And real training often bolts extra terms onto the scorecard to keep values from growing too large — terms that have nothing to do with matching the target but still shape the outcome heavily. So the loss used in training is better read as the distance from the target plus a handful of added policies, all stacked into one number.

There's also no single scorecard that fits every kitchen. A recipe site, a professional bakery, and a home cook weighing the same tray would each reach for a different formula, because each cares about a different kind of mistake. Picking the loss function is where that judgment call gets made, long before any training starts.

4Try it yourself

5Common misconceptions

  • It's easy to think loss counts how many answers were wrong, but actually it measures how far off each one was — get the same number wrong and the score still differs depending on how badly.

  • It's easy to think a low loss means a good model, but actually that drop often only happened on the practice data, so it needs to be checked separately against material the model has never seen.

  • It's easy to think there's one fixed loss function, but actually it's a design choice, picked by a person based on what matters most for the job.

7One-line summary

In shortA loss function is a scorecard that folds how far an answer was from correct into a single number, and how that scorecard is written decides what the model ends up good at.

Spotted an error or have a better analogy? Suggest an edit · Last updated2026-09-02