AI: Loss Functions & Objectives

AI & Machine Learning › Loss Functions & Objectives

Training a neural network is optimization, and a loss function is the thing being optimized. It is the single most consequential design choice after the architecture: it defines what “good” means, shapes the gradients that flow during backpropagation, and silently encodes assumptions about the noise, the label distribution, and the geometry of the problem. The same network trained under mean-squared error versus cross-entropy behaves completely differently. This page surveys the major families of loss functions — regression, classification, metric/contrastive, ranking, and self-supervised/generative objectives — with the probabilistic reasoning that motivates each one.

The Common Thread: Loss as Negative Log-Likelihood

Most “standard” losses are not arbitrary distance measures — they are negative log-likelihoods of a probabilistic model of the data. If you assume the targets are generated by some conditional distribution $p_\theta(y \mid x)$, then maximum likelihood estimation says: pick the parameters that make the observed data most probable. Minimizing the average negative log-likelihood,

\[\mathcal{L}(\theta) = -\frac{1}{N}\sum_{i=1}^{N} \log p_\theta(y_i \mid x_i),\]

recovers familiar losses as special cases of the assumed noise model:

Assumed $p_\theta(y \mid x)$ Resulting loss
Gaussian with fixed variance Mean squared error (MSE)
Laplace (double-exponential) Mean absolute error (MAE)
Categorical (softmax) Cross-entropy
Bernoulli Binary cross-entropy

This is the single most useful lens on the whole subject. When you choose a loss, you are choosing a noise model. MSE assumes your errors are Gaussian (symmetric, light-tailed); MAE assumes Laplace (heavy-tailed, robust to outliers). Understanding this connection tells you when a loss is appropriate, not just how to compute it.

A second organizing idea is the distinction between the surrogate loss you actually optimize and the task metric you care about. You usually cannot directly minimize 0–1 classification error or ranking-based metrics like NDCG — they are piecewise-constant and have zero gradient almost everywhere. So you minimize a smooth, convex (or at least differentiable) surrogate (cross-entropy, hinge, pairwise logistic) that upper-bounds or correlates with the true metric and provides usable gradients.

Regression Losses

Regression predicts a continuous target. The choice of loss is fundamentally a choice about how to treat residuals — especially large ones.

Mean Squared Error (L2 Loss)

\[\mathcal{L}_{\text{MSE}} = \frac{1}{N}\sum_{i=1}^{N}\left(y_i - \hat{y}_i\right)^2\]

MSE is the negative log-likelihood of a Gaussian noise model with constant variance. Its gradient with respect to a prediction is linear in the residual, $\partial \mathcal{L}/\partial \hat{y}_i = -\tfrac{2}{N}(y_i - \hat{y}_i)$, so large errors produce large gradients — the model is pushed hardest to fix its worst mistakes. The minimizer of expected MSE is the conditional mean $\mathbb{E}[y \mid x]$.

  • Why use it: smooth everywhere, convex, the natural choice when errors are roughly Gaussian and you want the mean.
  • Why avoid it: the quadratic penalty makes it extremely sensitive to outliers. A single mislabeled point with a huge residual can dominate the gradient and drag the fit toward it.

Mean Absolute Error (L1 Loss)

\[\mathcal{L}_{\text{MAE}} = \frac{1}{N}\sum_{i=1}^{N}\left|y_i - \hat{y}_i\right|\]

MAE is the negative log-likelihood of a Laplace noise model. Its gradient has constant magnitude (the sign of the residual), so a far-away outlier contributes no more to the gradient than a near miss — this is exactly why MAE is robust. The minimizer of expected MAE is the conditional median rather than the mean, which is the right target when the data has heavy tails or asymmetric outliers.

  • Why use it: robustness to outliers; you want the median, not the mean.
  • Why avoid it: non-differentiable at zero (sub-gradient needed), and the constant gradient gives no signal about how close you are, which can slow late-stage convergence.

Huber Loss (Smooth L1)

Huber loss interpolates: quadratic for small residuals (so it is smooth and gives a graded signal near the optimum) and linear for large ones (so it stays robust to outliers). With a threshold $\delta$ and residual $r = y - \hat{y}$:

\[\mathcal{L}_{\delta}(r) = \begin{cases} \tfrac{1}{2}\, r^2 & \text{if } |r| \le \delta, \\[4pt] \delta\left(|r| - \tfrac{1}{2}\delta\right) & \text{if } |r| > \delta. \end{cases}\]

The “smooth L1” variant (Huber with $\delta = 1$) is the standard bounding-box regression loss in object detectors like Faster R-CNN. The threshold $\delta$ is a knob: small $\delta$ behaves like MAE (robust), large $\delta$ like MSE (sensitive).

Log-Cosh and Quantile Losses

  • Log-cosh, $\mathcal{L} = \sum \log!\left(\cosh(\hat{y}_i - y_i)\right)$, behaves like MSE for small residuals and MAE for large ones but is twice differentiable everywhere — a smoother alternative to Huber with no hard threshold.
  • Quantile (pinball) loss lets you predict a chosen quantile $\tau \in (0,1)$ instead of the mean/median, which is the foundation of probabilistic and prediction-interval forecasting:
\[\mathcal{L}_{\tau}(r) = \max\!\big(\tau\, r,\ (\tau - 1)\, r\big), \qquad r = y - \hat{y}.\]

Setting $\tau = 0.5$ recovers (a scaled) MAE; $\tau = 0.9$ penalizes under-prediction nine times as heavily as over-prediction, yielding a conservative upper bound.

LossOutlier sensitivitySmooth at 0?Estimates
MSE (L2)HighYesConditional mean
MAE (L1)LowNoConditional median
HuberTunable via δYesMean-like, robustified
QuantileAsymmetricNoChosen quantile τ

Classification Losses

Classification predicts a discrete label. The dominant objective is cross-entropy, but several refinements address class imbalance, overconfidence, and noisy labels.

Cross-Entropy (Log Loss)

For a $K$-class problem, the network outputs logits $z$ that a softmax turns into a probability distribution $\hat{p}_k = e^{z_k}/\sum_j e^{z_j}$. With a one-hot target $y$, the categorical cross-entropy is

\[\mathcal{L}_{\text{CE}} = -\sum_{k=1}^{K} y_k \log \hat{p}_k = -\log \hat{p}_{c},\]

where $c$ is the true class. It is exactly the negative log-likelihood of the categorical distribution. The binary special case (sigmoid output $\hat{p}$, label $y \in {0,1}$) is

\[\mathcal{L}_{\text{BCE}} = -\big[\, y \log \hat{p} + (1-y)\log(1-\hat{p})\,\big].\]

The reason cross-entropy dominates regression-style losses for classification is its gradient. For softmax + cross-entropy, the gradient with respect to the logits collapses to the strikingly simple

\[\frac{\partial \mathcal{L}_{\text{CE}}}{\partial z_k} = \hat{p}_k - y_k.\]

The gradient is just “predicted minus true.” It does not saturate when the model is badly wrong (unlike pairing softmax with MSE, which can flatten and stall), so learning stays fast precisely where it matters most. Minimizing cross-entropy is equivalent to minimizing the KL divergence $D_{\mathrm{KL}}(p_{\text{data}} \,|\, \hat{p})$ between the true and predicted label distributions.

Label Smoothing

Hard one-hot targets push the model toward infinite logits and overconfident, poorly calibrated predictions. Label smoothing replaces the target with a softened distribution: the true class gets $1 - \epsilon$ and the remaining mass $\epsilon$ is spread uniformly over the other $K-1$ classes,

\[y_k^{\text{LS}} = (1-\epsilon)\, y_k + \frac{\epsilon}{K}.\]

This caps the logit gap the model tries to achieve, improves calibration (predicted probabilities track true accuracy more closely), and acts as a regularizer that reduces overfitting. It is standard in modern image classifiers and transformers (typically $\epsilon \approx 0.1$). The trade-off: slightly worse log-likelihood on confident, clean examples and reduced usefulness of the raw probabilities for downstream confidence thresholding.

Focal Loss

In tasks with extreme class imbalance — dense object detection, where the vast majority of candidate boxes are easy background — the sheer number of easy negatives can swamp the gradient even when each is individually well-classified. Focal loss down-weights easy examples so training focuses on the hard ones. With $p_t$ the predicted probability of the true class:

\[\mathcal{L}_{\text{focal}} = -\alpha_t\,(1 - p_t)^{\gamma}\,\log p_t.\]

The modulating factor $(1 - p_t)^{\gamma}$ shrinks toward zero as $p_t \to 1$: a well-classified example ($p_t = 0.99$) with $\gamma = 2$ has its loss scaled by $(0.01)^2 = 10^{-4}$, while a hard example ($p_t = 0.3$) is barely attenuated. The focusing parameter $\gamma \ge 0$ controls the strength ($\gamma = 0$ recovers weighted cross-entropy), and $\alpha_t$ adds a per-class weight. Focal loss was the key ingredient that let RetinaNet match two-stage detectors with a single-stage design.

Hinge Loss

The hinge loss is the objective behind support vector machines and is occasionally used in deep nets. For a binary label $y \in {-1, +1}$ and raw score $s$:

\[\mathcal{L}_{\text{hinge}} = \max\!\big(0,\ 1 - y\,s\big).\]

It is zero once a point is correctly classified with margin at least 1, and grows linearly inside the margin. Unlike cross-entropy it does not keep pushing already-correct, confident points — it cares only about the margin, which yields sparse support and a max-margin decision boundary. It does not produce calibrated probabilities, which is one reason cross-entropy is preferred when probability estimates matter.

Metric Learning: Contrastive, Triplet, and InfoNCE

Sometimes the goal is not to predict a label but to learn an embedding space in which semantically similar items are close and dissimilar items are far apart. This underpins face recognition, image retrieval, and modern self-supervised pretraining. These losses operate on relationships between embeddings rather than on absolute predictions.

Contrastive (Pairwise) Loss

Given a pair of embeddings $(z_i, z_j)$ with a binary label $Y$ (1 = similar, 0 = dissimilar) and Euclidean distance $D = \lVert z_i - z_j \rVert_2$:

\[\mathcal{L}_{\text{contrastive}} = Y\, D^2 + (1 - Y)\,\max\!\big(0,\ m - D\big)^2.\]

Similar pairs are pulled together (penalty grows with distance); dissimilar pairs are pushed apart but only until they exceed a margin $m$, after which they contribute no gradient. The margin prevents the model from wasting capacity separating points that are already far enough.

Triplet Loss

Triplet loss conditions on a reference. For an anchor $a$, a positive $p$ (same class) and a negative $n$ (different class), it demands the anchor be closer to the positive than to the negative by at least a margin $m$:

\[\mathcal{L}_{\text{triplet}} = \max\!\big(0,\ \lVert z_a - z_p \rVert^2 - \lVert z_a - z_n \rVert^2 + m\big).\]

Because it compares relative distances, triplet loss is more flexible than absolute pairwise contrastive loss and was central to FaceNet’s face embeddings. Its practical difficulty is triplet mining: most randomly sampled triplets already satisfy the margin and give zero gradient, so effective training requires actively selecting hard or semi-hard negatives that violate it.

InfoNCE and the Softmax Over Negatives

The dominant modern formulation generalizes the triplet idea to many negatives at once and frames metric learning as classification: identify the one positive among a batch of candidates. InfoNCE (Noise-Contrastive Estimation, the loss in SimCLR, MoCo, and CLIP) is a cross-entropy over similarities scaled by a temperature $\tau$. For an anchor with positive $z^{+}$ and a set of negatives ${z^{-}_k}$, using cosine similarity $\mathrm{sim}(\cdot,\cdot)$:

\[\mathcal{L}_{\text{InfoNCE}} = -\log \frac{\exp\!\big(\mathrm{sim}(z, z^{+})/\tau\big)}{\exp\!\big(\mathrm{sim}(z, z^{+})/\tau\big) + \sum_{k}\exp\!\big(\mathrm{sim}(z, z^{-}_k)/\tau\big)}.\]

Minimizing it maximizes a lower bound on the mutual information between the two views. Two design choices dominate its behavior: more negatives give a tighter bound and a richer signal (hence MoCo’s momentum queue and CLIP’s large batches), and the temperature $\tau$ sharpens or softens the similarity distribution — small $\tau$ emphasizes the hardest negatives. CLIP applies this symmetrically across an image–text batch to align the two modalities in a shared space.

Ranking Losses

In search, recommendation, and information retrieval the goal is to order items, not to score them in isolation. Pointwise regression of relevance scores ignores that only the relative order matters, so ranking losses operate on pairs or whole lists.

Pairwise Ranking (RankNet)

Given two items with scores $s_i, s_j$ where $i$ should rank above $j$, RankNet models the probability that $i$ beats $j$ with a logistic function and minimizes its binary cross-entropy:

\[\mathcal{L}_{\text{pairwise}} = \log\!\Big(1 + \exp\!\big(-(s_i - s_j)\big)\Big).\]

This depends only on the score difference, so it learns ordering directly. The closely related margin ranking loss, $\max(0,\ -(s_i - s_j) + m)$, enforces a fixed gap instead.

Listwise and Metric-Aware Losses

Pairwise losses treat every misordered pair equally, but real ranking metrics (NDCG, MAP) weight errors near the top of the list far more heavily. LambdaRank/LambdaMART address this by scaling each pair’s gradient by the change in the target metric that swapping that pair would cause, effectively optimizing the non-differentiable metric through a weighted pairwise surrogate. Listwise losses such as ListNet instead define a probability distribution over entire permutations and minimize cross-entropy against the ideal ordering. The recurring theme: the surrogate must encode where in the list an error occurs, because top-ranked errors hurt users most.

Self-Supervised and Generative Objectives

Generative models learn a distribution rather than a label, and their objectives are correspondingly richer. Each major generative family is defined as much by its loss as by its architecture. (For the full architectural treatment, see Generative Models; here we focus on the objectives themselves.)

Maximum Likelihood and the Evidence Lower Bound (ELBO)

The cleanest generative objective is maximum likelihood: make the training data probable under the model, $\max_\theta \sum_i \log p_\theta(x_i)$. Autoregressive models (GPT-style LLMs, PixelCNN, WaveNet) optimize this directly by factoring the joint into a product of conditionals and applying cross-entropy to next-token prediction.

For latent-variable models like VAEs, the marginal likelihood $p_\theta(x) = \int p_\theta(x \mid z)\,p(z)\,dz$ is intractable. The variational approach introduces an approximate posterior $q_\phi(z \mid x)$ and optimizes a tractable lower bound, the ELBO:

\[\mathcal{L}_{\text{ELBO}} = \mathbb{E}_{q_\phi(z \mid x)}\!\big[\log p_\theta(x \mid z)\big] - D_{\mathrm{KL}}\!\big(q_\phi(z \mid x)\ \|\ p(z)\big).\]

The two terms have clear roles: the first is a reconstruction term (decode the latent back to the input), and the second is a regularizer pulling the approximate posterior toward the prior $p(z)$, usually a standard Gaussian. The gap between the ELBO and the true log-likelihood is exactly $D_{\mathrm{KL}}(q_\phi \,|\, p_\theta(z \mid x))$ — tighten the approximate posterior and the bound tightens. A weight $\beta$ on the KL term (the $\beta$-VAE) trades reconstruction fidelity against latent disentanglement.

Adversarial (GAN) Objectives

GANs replace an explicit likelihood with a learned discriminator that supplies the loss. Generator $G$ and discriminator $D$ play a minimax game:

\[\min_G \max_D\ \mathbb{E}_{x \sim p_{\text{data}}}\big[\log D(x)\big] + \mathbb{E}_{z \sim p_z}\big[\log\!\big(1 - D(G(z))\big)\big].\]

At the optimal discriminator this objective minimizes the Jensen–Shannon divergence between the real and generated distributions. The appeal is that no tractable likelihood is needed and samples are sharp; the cost is notorious training instability. When real and fake distributions barely overlap, the JS divergence saturates and gradients to the generator vanish. The Wasserstein GAN replaces JS with the earth-mover (Wasserstein-1) distance, which provides smooth gradients even for disjoint supports:

\[\mathcal{L}_{\text{WGAN}} = \mathbb{E}_{x \sim p_{\text{data}}}\big[D(x)\big] - \mathbb{E}_{z \sim p_z}\big[D(G(z))\big],\]

with $D$ constrained to be 1-Lipschitz (enforced by weight clipping or, better, a gradient penalty). This single change to the objective — not the architecture — was the main reason GAN training became far more stable.

Diffusion and Score-Matching Objectives

Diffusion models define a fixed forward process that gradually corrupts data into Gaussian noise, and learn to reverse it. Remarkably, the full variational bound on the data likelihood simplifies to a plain regression loss: predict the noise that was added. With $x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1 - \bar\alpha_t}\,\varepsilon$ and a network $\varepsilon_\theta$,

\[\mathcal{L}_{\text{simple}} = \mathbb{E}_{t,\,x_0,\,\varepsilon}\!\left[\big\lVert \varepsilon - \varepsilon_\theta(x_t, t)\big\rVert^2\right].\]

It is mean squared error in disguise — which is exactly why diffusion training is so stable compared to the adversarial game GANs play. The equivalent score-matching view trains the network to estimate the gradient of the log-density (the score) $\nabla_x \log p_t(x)$ via denoising; predicting the noise and predicting the score are the same objective up to a scaling by the noise level. This unifying objective is what made diffusion models the dominant approach to high-fidelity image generation.

Masked and Contrastive Pretraining

Self-supervised pretraining manufactures a supervised objective from unlabeled data:

  • Masked language/image modeling (BERT, MAE) hides part of the input and trains the model to reconstruct it — cross-entropy over masked tokens, or MSE over masked image patches. The “label” is the input itself.
  • Contrastive pretraining (SimCLR, MoCo, CLIP) uses the InfoNCE loss above to pull augmented views (or paired modalities) together and push everything else apart.

Both turn the structure of the data into a free training signal, which is what makes large-scale pretraining possible without human annotation.

Practical Considerations

A few cross-cutting concerns determine whether a theoretically sound loss actually trains well.

  • Numerical stability. Never compute softmax then take its log separately — combine them. Frameworks provide fused softmax_cross_entropy and sigmoid + binary-cross-entropy (“BCE-with-logits”) ops that apply the log-sum-exp trick to avoid overflow and the loss of precision from $\log(0)$.
  • Class imbalance. Beyond focal loss, simple per-class weighting (inverse frequency) or resampling rebalances the gradient contribution of rare classes. For segmentation, region-overlap objectives like Dice loss sidestep imbalance by measuring overlap directly rather than per-pixel accuracy.
  • Reduction. Whether you sum or average the per-example loss interacts with the learning rate and batch size; mean reduction keeps the gradient scale roughly batch-size-independent.
  • Auxiliary and composite losses. Real systems often sum several objectives — a reconstruction term, a regularizer, a perceptual loss, an adversarial term — each with a weight. Those weights are hyperparameters, and getting their relative scale right is frequently more important than the exact form of any single term.
  • Regularization is part of the objective. Weight decay ($L_2$ on parameters) and other penalties are added to the data loss, so the true objective being minimized is data loss plus regularization. Keeping that in mind avoids surprises when the training loss and validation behavior diverge.
# PyTorch: pick the loss to match the task and noise model.
import torch
import torch.nn as nn
import torch.nn.functional as F

# Regression: Huber is robust to outliers, smooth near zero.
huber = nn.HuberLoss(delta=1.0)

# Classification: fused, numerically stable; supports label smoothing.
ce = nn.CrossEntropyLoss(label_smoothing=0.1)

# Binary with logits (no separate sigmoid): stable via log-sum-exp.
bce = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([4.0]))  # handle imbalance

# Metric learning: InfoNCE as cross-entropy over similarities.
def info_nce(query, keys, temperature=0.07):
    # query: (B, D); keys: (B, D) where keys[i] is the positive for query[i]
    q = F.normalize(query, dim=1)
    k = F.normalize(keys, dim=1)
    logits = q @ k.t() / temperature           # (B, B) similarity matrix
    targets = torch.arange(len(q), device=q.device)  # positive on the diagonal
    return F.cross_entropy(logits, targets)

Choosing a Loss: A Quick Guide

TaskDefault lossReach for instead when…
RegressionMSEOutliers present → Huber/MAE; need intervals → quantile
Multi-class classificationCross-entropyOverconfidence → label smoothing; imbalance → focal/weighting
Binary / multi-labelBCE-with-logitsExtreme negative imbalance → focal
Embeddings / retrievalInfoNCEFew negatives available → triplet/contrastive
Ranking / recommendationPairwise logisticTop-of-list matters → LambdaRank/listwise
Latent-variable generationELBOSharper samples → adversarial; stable high-fidelity → diffusion MSE

Key Takeaways

  • The loss is a noise model. MSE assumes Gaussian errors and estimates the mean; MAE assumes Laplace errors and estimates the median. Choosing a loss is choosing what you believe about your data.
  • Cross-entropy wins for classification because softmax + cross-entropy gives the clean, non-saturating gradient $\hat{p} - y$, keeping learning fast exactly where the model is wrong.
  • You optimize a surrogate, not the metric. 0–1 error, NDCG, and IoU have no usable gradient, so you minimize a smooth proxy (cross-entropy, pairwise logistic, Dice) that correlates with the metric you actually care about.
  • Metric and ranking losses act on relationships, not absolute predictions — triplet and InfoNCE pull positives together and push negatives apart; ranking losses depend only on score differences.
  • Generative objectives reverse or bound a likelihood. VAEs maximize the ELBO, GANs minimize a divergence via a learned discriminator, and diffusion models reduce to a stable noise-prediction MSE — the objective, not just the architecture, defines each family.

See Also