Deep Learning

The Only Loss Function Cheat Sheet You Need

Suresh Madhra·May 15, 2026·7 min read

The loss is the specification

Your model does exactly what the loss function asks. Every complaint about a model — it ignores rare cases, it is over-confident, it is wrecked by outliers — is usually a complaint about the loss. This is a working reference: what each loss optimises, when it is the right call, and what it does badly.

Regression losses

Mean squared error

Optimises the conditional mean. Smooth gradients everywhere, which optimisers love. Squaring means one outlier can dominate the entire batch — use it when large errors genuinely are disproportionately costly.

Mean absolute error

Optimises the conditional median, so it is robust to outliers. The gradient is constant in magnitude, which slows fine convergence near the optimum — decay the learning rate.

Huber

Quadratic near zero, linear in the tails: MSE's smooth convergence with MAE's robustness. δ is the outlier threshold, and it is worth tuning.

Quantile (pinball)

Asymmetric by design: train separate heads at τ = 0.1, 0.5, 0.9 and you get a prediction interval instead of a point estimate. The right default when under- and over-prediction have different costs.

regression_losses.py

Classification losses

Binary cross-entropy

The default for two-class problems. It is a proper scoring rule, so it produces calibrated probabilities — and it punishes confident mistakes brutally, which is exactly what you want. Always compute it from logits for numerical stability.

Categorical cross-entropy

The multi-class generalisation, paired with a softmax. Add label smoothing (ε ≈ 0.1) when the model becomes over-confident or labels are noisy; it trades a little accuracy for much better calibration.

Focal loss

Down-weights examples the model already gets right, so a flood of easy negatives cannot drown the rare positives. Built for dense object detection; excellent for extreme class imbalance generally. γ = 2 is the usual starting point.

classification_losses.py

Representation losses

  • Contrastive: pull matched pairs together, push mismatched pairs apart beyond a margin. Simple, but sensitive to how you mine negatives.
  • Triplet: anchor, positive, negative with a margin. Works well with semi-hard negative mining; collapses without it.
  • InfoNCE / NT-Xent: treat the batch as a classification over candidates. The workhorse behind modern embedding and multimodal models — larger batches directly mean more negatives and better representations.
  • Temperature is the hidden hyperparameter in all of them. Too low and training is unstable; too high and everything looks similar.

Choosing under pressure

  • Need calibrated probabilities? Cross-entropy, and do not replace it with a metric surrogate.
  • Severe imbalance? Focal loss or class weights — and change the metric too, not just the loss.
  • Outliers in the target? Huber, or MAE if they are common.
  • Need intervals? Quantile loss with multiple heads.
  • Business cost asymmetry? Encode it directly in the loss weights; do not fix it with a threshold afterwards and hope.
  • Optimising a non-differentiable metric (F1, AUC)? Train on a smooth surrogate and select the threshold on validation data.