JJoeven

Curriculum/Machine Learning

Loss Functions

A loss is a number that is small when the model is right. Training is making that number go down.

beginner21 min6 / 24

A loss (also called a cost) is a number that is small when the model is right and large when it is wrong. Training is “change parameters to make this number go down on the training set,” and hope it also goes down on validation.

If you cannot write the loss, you do not know what you are optimizing. Accuracy is a metric. You usually cannot take a slope through a hard yes/no, so we train on a smooth cousin. The cousin is the loss. The metric is what you report to humans. They are allowed to disagree. When they disagree, believe the metric for shipping and the loss for the training loop — then ask whether the loss is the wrong cousin.

A loss is a function: parameters plus a batch of examples in, one number out. Gradient descent (next) is how you walk that function downhill. This page is the map of which mountain you chose.

Mean squared error (regression)

For predictions p and targets t:

MSE = average of (p - t) squared

Squares punish large mistakes more than small ones. Predicting 10 when the answer is 0 is much worse than predicting 1. That is often what you want for numbers (latency, a quality score, tokens remaining). It is a bad idea for classifying tools: being “off by 2 tool ids” is meaningless. Tool 4 is not “twice” tool 2.

Mean absolute error (average of absolute gaps) punishes large mistakes more gently. Use it when a few wild outliers should not own the fit. Huber-style hybrids exist; you do not need the name to know the product question: should one crazy row dominate training?

MSE on chances for a classifier is also a mismatch. It does not treat “true class has chance 0.001” as the disaster that cross-entropy does. Use MSE for numbers that live on a line. Use cross-entropy for exclusive classes.

Cross-entropy (classification)

The model outputs logits (raw scores, one per class). Softmax turns them into chances that add up to 1. Then we punish a low chance on the correct class:

loss = -log(chance of the true class)

If the true class has chance 1, loss is 0. If it has chance 0.001, loss is large. During training the model is not asked to pick a class. It is asked to put mass on the right class. The hard pick (argmax) is for inference and for accuracy.

Softmax, in code: subtract the max logit (so exp does not explode), exponentiate, divide by the sum. Subtracting the max does not change the chances. It saves you from overflow. You will see this trick everywhere. It is numerics, not a new model.

Binary cross-entropy is the two-class version: one chance p from a sigmoid, loss is -log(p) if the label is 1 and -log(1-p) if the label is 0. Same idea: punish a confident wrong chance.

If missing a jailbreak is a hundred times worse than a false alarm, say so: class weights (multiply that example’s loss), or a metric you select on. Unweighted averages spend all their effort on the common class. The loss will happily ignore the rare disaster unless you make the disaster expensive.

MSE grows when the miss is large
-2.502.5051015prediction minus targetloss

Squares punish large mistakes more than small ones. Do not use this shape on tool ids.

MSE grows when the miss is large
Cross-entropy on the true class
0.05correct1.1unsure3.7wrong

Low chance on the right class is a large loss. Training pushes mass onto that class.

Cross-entropy on the true class
Live PythonOpen full playgroundpython
Output
Run to execute this in your browser. Nothing is sent to a server.

What printed: MSE of a perfect pair is 0.0. A bit off is a small positive number. Way off is much larger — squares amplify the 7-point miss. Then three classification rows, true class always index 0. Confident-correct puts almost all mass on class 0; loss is near 0. Unsure is roughly equal chances (~0.333 each); loss is about 1.1 (-log(1/3)). Confident-wrong puts mass on class 2; chance of the true class is tiny; loss is several nats — a disaster. That is the loss telling the optimizer what we care about.

The 1e-12 floor keeps log(0) from exploding if a chance underflows. It is a numeric seatbelt, not a modeling idea.

Loss vs metric

LossMetric
RoleDrive trainingReport quality
Smooth?Usually yesOften no (accuracy, pass rate)
Human meaningIndirectDirect (“did the agent finish?”)

You can overfit a loss and still fail a product metric. Agents should log both: a training-style score (valid JSON? citation present?) and a business score (ticket closed without reopen).

A loss that is “average token surprise” of a language model is not “did we call the right tool.” Fine-tuning on next-token text can drop loss while tool choice gets worse. If the product is a tool policy, the loss should see tools — or you accept that you are training a different job and you select checkpoints on the tool metric.

Surrogate is the honest word: cross-entropy is a smooth stand-in for “wrong class.” It is a good stand-in when chances are calibrated enough to rank. It is a bad stand-in when you need a costed cutoff on a rare class. Then you still train on cross-entropy, and you choose the cutoff on validation with the real costs (later lessons).

Designing a loss for traces

When you “train” a prompt on ten traces by eyeball, you still have a loss — it is just in your head and unstable. Write it down:

  • 1.0 if schema-invalid
  • 0.3 if no citation
  • 0.0 if gold match
  • extra if a forbidden tool ran

Average it. That number is more honest than “it feels better.” You can still not take a slope through a prompt. You can search: try neighbors, keep the winner. The ML habit is: define downhill, then walk.

If two errors should hurt equally, do not use MSE. If a miss on the rare class is fatal, do not optimize plain accuracy, and do not use unweighted cross-entropy without looking at recall.

Common mistakes

  • Training a router with MSE on integer tool ids.
  • Reporting loss as if it were pass rate.
  • Ignoring the rare class in an unweighted average.
  • Changing the loss until the demo looks good, without renaming the dataset version.
  • Using accuracy as the training objective (zero slope almost everywhere).

How agents use this

A judge that scores traces is a loss you can compute without GPUs. Put it on a frozen slice. Compare rules, prompts, and small models with the same number. When you later fit weights, pick a smooth cousin that points at the same idea: put mass on the right tool, punish forbidden tools, do not square a class index.

Loss is not morality. It is a contract with the optimizer. Write the contract in one sentence before you train: “downhill means higher chance on the senior-engineer tool, on train, without using test.” Then keep a metric that says whether that contract was the right product.

Tip:If two errors should hurt equally, do not use MSE. If a miss on the rare class is fatal, do not optimize plain accuracy.

Check your understanding

Why train a classifier with cross-entropy instead of accuracy?