How does a loss function work in machine learning?
For each training example the loss compares the prediction with the label and returns a number, zero for a perfect answer and larger as it gets worse. Averaged over a batch it is often called the cost. The optimizer computes how that average changes with every weight and steps downhill, the loop at the heart of deep learning. The loss must be differentiable for that to work, which is why models do not train on accuracy directly: a count of right answers does not change smoothly as weights move. The loss is what the model optimizes; the metric is what you care about, and they are rarely the same number.
What are the common loss functions and their formulas?
Here y is the true value, ŷ the prediction and p the predicted probability of the positive class.
| Loss | Formula | Task | What it punishes most |
|---|---|---|---|
| Mean squared error (L2) | mean of (y - ŷ)² | Regression | Large errors, quadratically |
| Mean absolute error (L1) | mean of abs(y - ŷ) | Regression | All errors linearly, so outliers count less |
| Binary cross-entropy | -[y log p + (1 - y) log(1 - p)] | Two-class classification | Confident wrong answers |
| Categorical cross-entropy | -log p(correct class) | Multi-class classification | Low probability on the right class |
| Hinge | max(0, 1 - y·f(x)) | Margin classifiers such as SVMs | Correct answers too close to the boundary |
Classification almost always trains on cross-entropy, because it rewards calibrated probabilities instead of only the final label.
What loss function do language models use?
Pretraining uses next-token cross-entropy: the negative log probability the model assigned to the token that actually came next, averaged over every token in the corpus. The GPT-3 paper tracks model quality through exactly this cross-entropy loss and reports that improvements in it led to consistent gains on downstream tasks (Brown et al., 2020, read 2 November 2026). Perplexity is the same quantity exponentiated. A pretrained LLM has been optimized for one thing: assigning high probability to what comes next. Instruction following arrives later, when post-training adds preference losses and rewards from a reward model.
Why does the choice of loss function matter?
Because the model learns exactly what the loss pays for. A plain cross-entropy treats a missed fraud and a false alarm alike; class weights or an asymmetric loss make the expensive error cost more during training, not only at the threshold. Optimized hard enough, a model also exploits gaps between the loss and the real goal, the training-time cousin of reward hacking. Watch the training and validation losses together: a training loss that keeps falling while validation loss rises is overfitting, and the run should stop there.