Loss functions
A loss function turns a model's mistakes into one number to minimise. Learn MSE, MAE, Huber, quantile, cross-entropy, focal, hinge, KL, triplet and CRPS losses.
Overview
A loss function tells a model how badly it is doing. It takes a prediction and the correct answer and returns a number: zero or small when the prediction is good, larger when it is bad. Training means changing the model's parameters so that the average loss over the training data goes down, usually with gradient descent. The loss is the only thing the training algorithm "sees", so it defines what the model learns to care about.
An everyday picture is a darts score where you choose the rules. If every centimetre of miss costs the same, a player learns to aim at the middle of their spread. If a big miss costs far more than a small one, the player becomes cautious and avoids wild throws. If only the outer ring counts as failure, the player stops caring about fine accuracy near the centre. Different loss functions are different scoring rules, and the model adapts to whichever one you pick.
This page covers the main losses for regression (MSE, MAE, Huber, log-cosh, quantile), classification (binary and categorical cross-entropy, focal, hinge), distributions (KL divergence, CRPS) and embeddings (contrastive and triplet). It explains what each one rewards, how to choose, and how losses differ from the evaluation metrics you report.
What it is
A loss is a function ℓ(prediction, target) that returns a non-negative number. The average over a dataset is often called the cost or the objective. Two properties matter in practice: it should reflect what you care about, and it should be differentiable (or nearly so) so gradients can be computed.
| Family | Typical losses | Prediction type |
|---|---|---|
| Regression, point | MSE, MAE, Huber, log-cosh | A single number |
| Regression, quantile | Pinball (quantile) loss | A chosen quantile, such as the 90th percentile |
| Regression, probabilistic | Gaussian negative log-likelihood, CRPS | A full predictive distribution |
| Binary classification | Binary cross-entropy, focal, hinge | A probability or a score |
| Multi-class classification | Categorical cross-entropy, label-smoothed cross-entropy | A probability vector |
| Distribution matching | KL divergence | One distribution compared with another |
| Metric learning | Contrastive, triplet, InfoNCE | Embeddings where distance means similarity |
Many losses are negative log-likelihoods in disguise. MSE corresponds to assuming Gaussian noise, MAE to Laplace noise, and cross-entropy to a categorical (or Bernoulli) model of the labels. This link to probability and information theory explains why they work as well as they do.
Where it came from
Squared error goes back to the method of least squares, published by Adrien-Marie Legendre in 1805 and developed by Carl Friedrich Gauss. Absolute error has an even older history in astronomy and survey work. In 1964 Peter Huber published "Robust Estimation of a Location Parameter" in the Annals of Mathematical Statistics, introducing the loss that is quadratic for small errors and linear for large ones, so a few outliers cannot dominate.
Roger Koenker and Gilbert Bassett introduced regression quantiles in Econometrica in 1978, which uses the asymmetric pinball loss. Solomon Kullback and Richard Leibler defined the divergence that carries their names in 1951, in "On Information and Sufficiency". The continuous ranked probability score, used for probabilistic forecasts, was formalised by James Matheson and Robert Winkler in 1976, and Tilmann Gneiting and Adrian Raftery's 2007 paper on strictly proper scoring rules made it a reference point for evaluating and training probabilistic models.
On the classification side, the hinge loss is the loss behind support vector machines, whose soft-margin form was published by Corinna Cortes and Vladimir Vapnik in 1995. Cross-entropy became the standard for neural classifiers with the softmax output. In 2006 Raia Hadsell, Sumit Chopra and Yann LeCun described a contrastive loss for learning embeddings. In 2015 Florian Schroff, Dmitry Kalenichenko and James Philbin used a triplet loss in FaceNet. In 2017 Tsung-Yi Lin and colleagues introduced focal loss for object detection, where background examples vastly outnumber objects.
How it works
The formulas
MSE = (1/n) Σ (y − ŷ)²
MAE = (1/n) Σ |y − ŷ|
Huber_δ = ½ (y − ŷ)² if |y − ŷ| ≤ δ
δ · (|y − ŷ| − ½ δ) otherwise
log-cosh = Σ log(cosh(ŷ − y))
Pinball_τ = τ · (y − ŷ) if y ≥ ŷ
(1 − τ) · (ŷ − y) otherwise
BCE = −[ y · log p + (1 − y) · log(1 − p) ]
CE = −Σ_k y_k · log p_k
Focal = −α · (1 − p_t)^γ · log p_t
Hinge = max(0, 1 − y · s) with y in {−1, +1}
KL(P‖Q) = Σ_x P(x) · log( P(x) / Q(x) )
Triplet = max(0, d(a, p) − d(a, n) + m)
CRPS(F, y) = ∫ ( F(z) − 1{z ≥ y} )² dz
Here y is the target and ŷ the prediction, n is the number of examples, δ (delta) is the Huber threshold, τ (tau) is the target quantile between 0 and 1, p is the predicted probability of the positive class, p_k the predicted probability of class k and y_k is 1 for the true class and 0 otherwise, p_t is the probability given to the true class, α and γ are the focal-loss weight and focusing parameter, s is a raw classifier score, P and Q are two distributions, a, p and n in the triplet loss are anchor, positive and negative embeddings with distance d and margin m, and F is the predicted cumulative distribution function with 1{z ≥ y} the step function at the observed value.
Regression losses
Mean squared error squares each error, so an error of 10 costs 100 times an error of 1. It pushes the model towards the conditional mean of the target. It is smooth and easy to optimise but sensitive to outliers. Mean absolute error treats all errors linearly and pushes towards the conditional median. It is robust to outliers but has a constant gradient, so it does not slow down near the optimum, and its gradient is undefined at zero.
Huber loss blends them: quadratic inside ±δ, linear outside. You get MSE's smooth behaviour near the target and MAE's robustness to large errors. PyTorch's nn.HuberLoss uses delta=1.0 by default; choose δ near the size of error you consider "normal". Log-cosh behaves like half the squared error for small errors and like absolute error minus a constant for large ones, and it is twice differentiable everywhere, which suits methods that use second derivatives.
Quantile (pinball) loss penalises under- and over-prediction asymmetrically. With τ = 0.9, under-predicting costs nine times as much as over-predicting, so the model learns the 90th percentile. Training several quantiles gives prediction intervals, and libraries such as LightGBM and XGBoost offer it as an objective.
Probabilistic regression
Sometimes you want a whole distribution, not one number. One approach is to predict a mean and a variance and minimise the Gaussian negative log-likelihood. Another is the continuous ranked probability score (CRPS), which compares the predicted cumulative distribution with a step at the observed value. CRPS rewards forecasts that are both sharp and well calibrated, has the same units as the target, and reduces to absolute error when the forecast is a single point. It is widely used in weather and demand forecasting and for evaluating probabilistic tabular models.
Classification losses
Binary cross-entropy (log loss) scores a predicted probability against a 0/1 label. Confidently wrong predictions are punished very hard: predicting 0.01 for a true positive costs about 4.6, while predicting 0.5 costs about 0.69. Categorical cross-entropy extends this to many classes with a softmax output. In PyTorch, nn.CrossEntropyLoss takes raw logits and applies log-softmax internally, which is more numerically stable than applying softmax yourself; it also has a label_smoothing argument that softens one-hot targets.
Focal loss multiplies cross-entropy by (1 − p_t)^γ. Easy, well-classified examples (high p_t) contribute almost nothing, so training focuses on hard ones. The original paper found γ = 2 with α = 0.25 worked best for its detector. It helps with extreme class imbalance.
Hinge loss is zero once an example is on the correct side of the margin by at least 1, and grows linearly otherwise. It produces a maximum-margin classifier, as in support vector machines, but it does not produce calibrated probabilities.
Distribution and embedding losses
KL divergence measures how much information is lost when a distribution Q is used to approximate P. It is not symmetric. It appears in variational autoencoders, in knowledge distillation (matching a student's softened outputs to a teacher's), and as a penalty in fine-tuning methods such as RLHF that keep a model close to a reference. Minimising cross-entropy against fixed targets is equivalent to minimising KL divergence, since they differ only by the entropy of the targets.
Contrastive loss pulls embeddings of similar pairs together and pushes dissimilar pairs at least a margin apart. Triplet loss works on an anchor, a positive and a negative, and asks the anchor to be closer to the positive than to the negative by a margin. Modern contrastive learning often uses InfoNCE, which treats one positive and many negatives as a classification problem.
How to choose
- Start from the decision the prediction supports. If over- and under-prediction have different costs, use a quantile or custom weighted loss.
- Match the target type: real values, probabilities, ranks or distributions.
- Consider the noise. Heavy-tailed noise or outliers argue for Huber or MAE; clean Gaussian-like noise suits MSE.
- For classification you will threshold later, use cross-entropy to get calibrated probabilities, and handle imbalance with class weights or focal loss.
- Check that the loss and your evaluation metric agree in spirit. Training on MSE and reporting MAE is fine if you understand that MSE targets the mean, not the median.
Key concepts and terms
| Term | Meaning |
|---|---|
| Loss | Error on one example |
| Cost or objective | Average loss over a dataset, sometimes plus regularisation terms |
| Logit | A raw, unnormalised score before sigmoid or softmax |
| Calibration | Whether predicted probabilities match observed frequencies |
| Robust loss | A loss that limits the influence of outliers, such as Huber or MAE |
| Proper scoring rule | A score minimised in expectation by reporting the true distribution |
| Margin | The required gap between correct and incorrect scores or distances |
| Class weighting | Scaling the loss of rare classes up to counter imbalance |
| Label smoothing | Replacing hard 0/1 targets with slightly softened values |
| Negative log-likelihood | Minus the log probability the model gives to the observed data |
| Surrogate loss | A smooth loss optimised in place of a hard metric such as accuracy |
A worked example
An illustrative online shop forecasts next-day demand for one product. Four days of actual sales are 10, 12, 11 and 40 units; the last was a one-off promotion. A model predicts 11 every day. Errors are −1, 1, 0 and 29.
- MSE: (1 + 1 + 0 + 841) ÷ 4 = 210.75. The promotion day contributes 99.8 per cent of the loss, so a model trained on MSE will be dragged upward to chase such spikes.
- MAE: (1 + 1 + 0 + 29) ÷ 4 = 7.75. The spike matters, but in proportion.
- Huber with δ = 5: the three small errors cost 0.5, 0.5 and 0; the spike costs 5 × (29 − 2.5) = 132.5. Average 33.4. The spike is penalised linearly, so it cannot dominate.
- Pinball with τ = 0.9: running out of stock is costly, so the shop wants the 90th percentile. Under-predictions (actual above forecast) cost 0.9 per unit and over-predictions 0.1. Day 1 over-predicts by 1: cost 0.1. Day 2 under-predicts by 1: 0.9. Day 3: 0. Day 4: 0.9 × 29 = 26.1. Average 6.78. A model trained this way learns to forecast higher than the median.
Now a classifier for whether a customer will return. For a true returner, the model says 0.9: binary cross-entropy is −log 0.9 ≈ 0.105. If it says 0.2: −log 0.2 ≈ 1.609, about 15 times worse. With focal loss (γ = 2, α ignored for simplicity), the first case is scaled by (0.1)² = 0.01 to about 0.001, while the second is scaled by (0.8)² = 0.64 to about 1.03. The easy example almost disappears from training; the hard one keeps most of its weight.
Strengths and limitations
Strengths:
- A well-chosen loss encodes business costs directly into training.
- Likelihood-based losses (MSE, cross-entropy) have clear statistical meaning and give calibrated outputs when the model is well specified.
- Robust losses such as Huber protect training from outliers without removing data.
- Quantile and probabilistic losses produce uncertainty estimates, not only point forecasts.
- Margin and contrastive losses learn useful embedding spaces without explicit labels per class.
Limitations:
- The metric you care about (accuracy, F1, profit) is often not differentiable, so you train on a surrogate.
- Some losses add hyperparameters (δ, τ, γ, margins) that need tuning.
- MSE and cross-entropy are sensitive to label noise and outliers.
- Hinge and focal losses do not by themselves produce calibrated probabilities.
- KL divergence is asymmetric and can be infinite when Q gives zero probability where P does not.
How it compares with alternatives
| Loss | Best for | Outlier sensitivity | Output meaning |
|---|---|---|---|
| MSE (linear regression) | Clean regression targets | High | Conditional mean |
| MAE | Noisy targets, median forecasts | Low | Conditional median |
| Huber | Mostly clean data with some outliers | Medium, tunable | Between mean and median |
| Cross-entropy (logistic regression) | Probabilistic classification | High for confident errors | Calibrated probabilities |
| Hinge (SVM) | Margin-based classification | Medium | Scores, not probabilities |
Practical guidance and best practices
- Write down the real cost of different errors before picking a loss; the loss should approximate that cost.
- Pass logits, not probabilities, to
nn.CrossEntropyLossand usenn.BCEWithLogitsLossfor binary tasks, for numerical stability. - For imbalanced classes, try class weights first, then focal loss, and always check calibration afterwards.
- Use Huber when you suspect outliers but do not want to delete rows; set δ near the scale of typical errors.
- For prediction intervals, train separate quantiles (for example 0.1, 0.5, 0.9) or a model that outputs a distribution scored by CRPS.
- Standardise regression targets with large ranges so the loss scale does not destabilise training.
- Report evaluation metrics that match the business question, alongside the training loss, on held-out data from cross-validation.
- When combining several losses, weight them so their gradients have comparable size, and log each term separately.
Common mistakes
- Applying softmax before
nn.CrossEntropyLoss, which applies log-softmax again and weakens gradients. - Using MSE on probabilities for classification, which learns slowly when predictions are confidently wrong.
- Optimising accuracy directly by thresholding inside the loss, which has zero gradient almost everywhere.
- Using MAE and expecting mean predictions, or MSE and expecting medians.
- Ignoring class imbalance, so the model learns to predict the majority class.
- Mixing up KL(P‖Q) and KL(Q‖P), which behave differently.
Frequently asked questions
What is the difference between a loss function and a metric?
A loss is what training minimises; it must be differentiable or nearly so. A metric is what you report to judge the model, such as accuracy, AUC or MAPE, and it need not be differentiable. They are often related but not identical. See evaluation metrics.
Why is cross-entropy preferred over MSE for classification?
With a sigmoid or softmax output, cross-entropy produces gradients proportional to the error in probability, so learning stays fast even when the model is badly wrong. MSE on sigmoid outputs has gradients that shrink when the output saturates, slowing learning. Cross-entropy is also the negative log-likelihood of the labels, so it gives probabilistic meaning to the outputs.
When should I use Huber loss?
Use it when most errors are moderate but a few are very large and you do not trust them fully, for example sensor glitches or one-off sales spikes. It keeps the smooth behaviour of MSE near the target and limits the pull of outliers.
How does quantile loss create prediction intervals?
Train one model (or one output) per quantile, such as τ = 0.05 and τ = 0.95. Each learns the corresponding percentile of the target given the inputs. The pair forms a 90 per cent interval, which you should check on held-out data for actual coverage.
Is focal loss always better for imbalanced data?
No. It helps when there are huge numbers of easy negatives, as in dense object detection. For moderate imbalance, class weights or resampling are simpler and often equally good. Focal loss also changes probability calibration, so recalibrate if you need reliable probabilities.
Why does KL divergence appear in so many deep learning methods?
Because it measures how far one distribution is from another in a principled way. Variational autoencoders use it to keep the latent distribution close to a prior. Distillation uses it to match a teacher. Alignment methods use it to stop a fine-tuned model drifting too far from its starting point.
Glossary
- Loss function: a formula that scores the error of one prediction.
- Cross-entropy: the negative log probability a model assigns to the true class.
- Huber loss: a loss that is quadratic for small errors and linear for large ones.
- Pinball loss: the asymmetric loss used to estimate quantiles.
- Focal loss: cross-entropy that down-weights easy, well-classified examples.
- Hinge loss: a margin-based loss used by support vector machines.
- KL divergence: an asymmetric measure of difference between two distributions.
- Triplet loss: a loss on anchor, positive and negative embeddings with a margin.
- CRPS: a proper score comparing a predicted distribution with an observed value.
- Logit: a raw score before conversion to a probability.
Summary
A loss function turns a model's errors into one number, and training minimises that number, so the loss decides what the model learns. MSE targets the mean and punishes large errors, MAE targets the median and resists outliers, and Huber and log-cosh sit between them. Quantile loss estimates percentiles, while Gaussian likelihood and CRPS train full predictive distributions. For classification, cross-entropy gives calibrated probabilities, focal loss concentrates on hard examples and hinge loss maximises margins. KL divergence compares distributions, and contrastive and triplet losses shape embedding spaces. Choose the loss from the real cost of errors, the target type and the noise, then judge the result with metrics that reflect the actual decision.
Related
It belongs to Training fundamentals in my stack, under Machine learning. Others in the same group: