Loss Functions
Defining 'wrong' precisely
The loss function quantifies wrongness and steers every gradient. Classification uses cross-entropy; regression uses MSE/MAE; generation uses next-token cross-entropy. Choosing right is choosing what 'better' means.
What you will learn
- Match loss functions to task types
- Explain why cross-entropy underpins LLMs
- Diagnose loss pathologies that waste training
What is this?
A loss function scores how wrong each prediction is — the model gets smaller values when closer to right. The gradient of the loss is literally the training direction. Classification usually uses cross-entropy; regression typically MSE or MAE; and language models train on next-token cross-entropy, which is why 'LLM' and 'estimating text probability' are the same idea.
Why do you need it?
- The loss defines what the model considers good — wrong loss, wrong model.
- Cross-entropy works perfectly with softmax logits and labels.
- MSE punishes outliers quadratically; MAE is robust. The choice changes behavior.
- Every generation quality effort starts from the next-token cross-entropy objective.
Mental model
A referee with a strict scorebook
The loss function
The referee decides exactly how many points an off-target move loses. The fun discipline of backprop just reads those points and runs downhill — the whole competition is shaped by the scoring rubric.
How it works
- Regression: MSE = mean (y−ŷ)², sensitive to outliers; MAE = mean |y−ŷ|, robust.
- Classification: cross-entropy = −Σ y log(p), perfect fit with softmax probabilities.
- LLM pre-training: predicts each next token with cross-entropy over the vocabulary.
- Hinge/ranking losses favor correct ordering over magnitude.
- Weighted losses reshape priorities toward rare or expensive mistakes.
Task → head → loss.
In production
- Fraud classifiers weight the rare-class loss so misses cost more than flags.
- Object detection masks use GIoU-family losses to optimize geometry, not pixels.
- LLM fine-tuning matters most through data/objective structure — same CE loss, better target distribution.
Best practices
- Match the loss to the decision, not to vibes: robust vs single-error dominated.
- Weight class importance inside the loss explicitly.
- Before fancy losses, sanity-check that your CE sees the right label distribution.
Common mistakes
- Using MSE on a classification and waiting for 'convergence'.
- Ignoring NaN/Inf when logits are ill-posed (e.g. tiny negatives in logs).
- Believing 'custom loss' fixes data problems.
Common issues
NaN cross-entropy for a softmax headLogits inflated or labels out of range
Stabilize (log-softmax), clamp, and verify label index bounds.
MSE dominated by a few pointsHeavy-tailed targets
Use Huber/MAE or winsorize extreme targets.
Model optimizes loss but product metric doesn't moveLoss proxy diverges from the business metric
Add an auxiliary loss or re-frame the objective around the metric.
Debugging tips
- Loss plateaus but big for softmax — usually logit scale or label mismatch, not 'still learning'.
- MSE sensitive to one huge error — plot residuals to find the outlier.
- If CE is NaN, check label classes exist in the vocabulary and logits aren't all −Inf.
Interview checkpoint
Quick quiz
Which loss naturally pairs with softmax classification?
MAE is more robust to outliers than MSE.