Learn ML

Chapter 16 of 25

Loss Functions

Teaching a network what 'wrong' means

18 min read

Motivation

A network can't improve at something it can't measure. Before any gradient can be computed, before backpropagation has anything to propagate, there has to be a single number that says "here's exactly how wrong you were." That number is the loss, and the function that produces it — the loss function — is one of the most consequential design decisions in the entire training pipeline. Pick the wrong one, and even a perfectly implemented network trains against the wrong goal.

This chapter catalogs the loss functions actually used in practice, and — because it was explicitly promised back in the Backpropagation chapter — carefully derives why two of the most common pairings (sigmoid with binary cross-entropy, and softmax with categorical cross-entropy) collapse the output error signal down to the remarkably clean y^y\hat{y} - y.

A Real-Life Scenario

Imagine grading two very different kinds of exams. On a numeric exam (predict a temperature, predict a stock price), "how wrong" is naturally a matter of degree — being off by 2 degrees is worse than being off by 0.5 degrees, and the penalty should scale smoothly with the size of the miss. That's the world of regression losses.

On a multiple-choice exam (is this email spam or not, is this a cat or a dog or a bird), "how wrong" is a different kind of question — you're not off by some numeric amount, you assigned a certain confidence to the correct answer, and the penalty should depend on how confident you were and whether you were right. That's the world of classification losses. Loss functions formalize both of these grading philosophies mathematically, and this chapter walks through the main ones on each side.

Building the Intuition

  • A loss function isn't arbitrary — most of the losses in this chapter can be derived from a Maximum Likelihood Estimation argument: assume a specific noise distribution for how the true labels are generated, and the "obviously correct" loss falls straight out of the math.
  • For regression, the size and shape of the error matters: squaring it punishes big mistakes heavily; taking the absolute value treats all mistakes proportionally; a hybrid can get the best of both.
  • For classification, the loss cares about confidence, not just correctness: being 51% sure of the right answer is barely rewarded over being 51% sure of the wrong one, while being 99% confidently wrong is punished severely.

The Math Behind It: Regression Losses

Mean Squared Error (MSE)

Mean Squared Error
LMSE=1Ni=1N(yiy^i)2,LMSEy^i=2N(yiy^i)L_{\text{MSE}} = \frac{1}{N}\sum_{i=1}^N (y_i - \hat y_i)^2, \qquad \frac{\partial L_{\text{MSE}}}{\partial \hat y_i} = -\frac{2}{N}(y_i - \hat y_i)

Where MSE comes from: if we assume the target is generated as y=fθ(x)+ϵy = f_\theta(\mathbf{x}) + \epsilon with Gaussian noise ϵN(0,σ2)\epsilon \sim \mathcal{N}(0,\sigma^2), then maximizing the likelihood of the observed data — equivalently, minimizing the negative log-likelihood — reduces, after dropping constants that don't affect the minimizer, to exactly i(yiy^i)2\sum_i (y_i-\hat y_i)^2. In other words: minimizing MSE is Maximum Likelihood Estimation under a Gaussian noise assumption.

Advantages: smooth, convex in y^\hat y, heavily penalizes large errors due to squaring, simple gradient. Disadvantages: very sensitive to outliers — one badly wrong prediction, squared, can dominate the entire loss. Use case: the default regression loss when errors are roughly Gaussian and outliers aren't a major concern.

Mean Absolute Error (MAE)

LMAE=1Ni=1Nyiy^i,LMAEy^i=1Nsign(yiy^i)L_{\text{MAE}} = \frac{1}{N}\sum_{i=1}^N |y_i - \hat y_i|, \qquad \frac{\partial L_{\text{MAE}}}{\partial \hat y_i} = -\frac{1}{N}\,\text{sign}(y_i-\hat y_i)

MAE corresponds to MLE under a Laplace noise assumption instead of Gaussian. Since errors aren't squared, large errors aren't disproportionately amplified — MAE is robust to outliers. The trade-off: its gradient has constant magnitude regardless of error size, which can make fine convergence near the minimum slower and less stable, and it's non-differentiable exactly at zero error.

Huber Loss

LHuber(e)={12e2eδδ(e12δ)e>δ,e=yy^L_{\text{Huber}}(e) = \begin{cases}\frac{1}{2}e^2 & |e|\leq\delta \\ \delta\big(|e|-\frac{1}{2}\delta\big) & |e|>\delta\end{cases}, \qquad e = y-\hat y

Huber loss combines the best of both worlds: it behaves like MSE (smooth, quadratic) for small errors, and like MAE (robust, linear) for large errors, avoiding both MSE's outlier sensitivity and MAE's kink at zero. It's common in reinforcement learning value-function training, and any regression setting needing a balance between smoothness and robustness.

The Math Behind It: Classification Losses

Binary Cross-Entropy (BCE)

Binary Cross-Entropy
LBCE=1Ni=1N[yilogy^i+(1yi)log(1y^i)]L_{\text{BCE}} = -\frac{1}{N}\sum_{i=1}^N \big[y_i\log\hat y_i + (1-y_i)\log(1-\hat y_i)\big]

Derived from: the Bernoulli Maximum Likelihood argument — if each label is a coin flip with probability y^i\hat y_i of landing on class 1, this is exactly the negative log-likelihood of the observed labels. Requires: y^i(0,1)\hat y_i \in (0,1), typically produced by a sigmoid output activation.

Deriving the Sigmoid + BCE Simplification

This is the derivation the Backpropagation chapter promised: proving that δ[L]=y^y\delta^{[L]} = \hat y - y when a sigmoid output is paired with BCE loss.

For a single example, L=[ylogy^+(1y)log(1y^)]L = -\big[y\log\hat y + (1-y)\log(1-\hat y)\big] where y^=σ(z)\hat y = \sigma(z). First, differentiate the loss with respect to the prediction itself:

Ly^=yy^+1y1y^=y(1y^)+(1y)y^y^(1y^)=y^yy^(1y^)\frac{\partial L}{\partial \hat y} = -\frac{y}{\hat y} + \frac{1-y}{1-\hat y} = \frac{-y(1-\hat y) + (1-y)\hat y}{\hat y(1-\hat y)} = \frac{\hat y - y}{\hat y(1-\hat y)}

Then apply the chain rule through the sigmoid, using σ(z)=y^(1y^)\sigma'(z) = \hat y(1-\hat y) (derived in the Activation Functions chapter):

δ[L]=Lz=Ly^y^z=y^yy^(1y^)y^(1y^)=y^y\delta^{[L]} = \frac{\partial L}{\partial z} = \frac{\partial L}{\partial \hat y}\cdot\frac{\partial \hat y}{\partial z} = \frac{\hat y-y}{\hat y(1-\hat y)}\cdot \hat y(1-\hat y) = \hat y - y
Why this cancellation is such a big deal

The y^(1y^)\hat y(1-\hat y) terms in the numerator and denominator cancel exactly, leaving the remarkably simple result δ[L]=y^y\delta^{[L]} = \hat y - y. This is precisely why the sigmoid + BCE pairing is used so widely: it produces a clean, numerically stable gradient with no risk of dividing by a near-zero number — which would happen if you computed L/y^\partial L/\partial\hat y and y^/z\partial\hat y/\partial z separately and multiplied them naively for a confidently correct or confidently wrong prediction.

Use case: binary classification, always paired with a sigmoid output activation.

Categorical Cross-Entropy (CCE)

Categorical Cross-Entropy
LCCE=1Ni=1Nk=1Kyi,klogy^i,kL_{\text{CCE}} = -\frac{1}{N}\sum_{i=1}^N\sum_{k=1}^K y_{i,k}\log\hat y_{i,k}

Where: yi\mathbf{y}_i is a one-hot vector (all zeros except a single 1 at the true class index), and y^i\hat{\mathbf{y}}_i is the softmax output. Derived from: the Categorical distribution MLE — the direct multi-class generalization of the BCE derivation above. Because yi\mathbf{y}_i is one-hot, the double sum collapses to a single term: Li=logy^i,cL_i = -\log\hat y_{i,c}, where cc is the true class — the loss is simply the negative log-probability the model assigned to the correct class.

By an entirely analogous derivation to the BCE case above — using the softmax Jacobian in place of the sigmoid derivative — the softmax + CCE pairing produces the identical clean simplification δ[L]=y^y\boldsymbol{\delta}^{[L]} = \hat{\mathbf{y}} - \mathbf{y}. This is exactly why softmax and categorical cross-entropy are always used together in practice: the same elegant cancellation that made sigmoid + BCE so clean reappears here in vector form.

Sparse categorical cross-entropy is not a different loss

Sparse categorical cross-entropy computes exactly the same mathematical loss, Li=logy^i,cL_i = -\log\hat y_{i,c} — the only difference is that the true label is given as a plain integer class index (e.g. c=2c=2) instead of a one-hot vector (e.g. (0,0,1,0,0)(0,0,1,0,0)). It's purely an engineering convenience that saves memory and skips an explicit one-hot-encoding step; the resulting gradient is identical.

Hinge Loss

LHinge=1Ni=1Nmax(0, 1yiy^i),yi{1,+1}L_{\text{Hinge}} = \frac{1}{N}\sum_{i=1}^N \max\big(0,\ 1 - y_i\hat y_i\big), \qquad y_i \in \{-1,+1\}

Hinge loss is zero whenever the prediction has the correct sign and a margin of at least 1 from the decision boundary (yiy^i1y_i\hat y_i \geq 1); it grows linearly once the prediction crosses into incorrect or under-confident territory. This encourages not just correct classification but a confident margin around the boundary. It's historically the loss function of Support Vector Machines, and is occasionally used with MLPs for margin-based binary classification, though cross-entropy is far more common in practice.

Binary Cross-Entropy vs Mean Squared Error Penalty CurvesIllustration coming soon
For a true label y = 1, BCE's penalty rises sharply as the predicted probability approaches 0, while MSE's penalty rises much more gently — this is why cross-entropy produces sharper, more corrective gradients for confidently wrong predictions.

Worked Numerical Example: BCE vs. MSE

Suppose y^=0.8\hat y = 0.8 and the true label is y=1y=1:

LBCE=log(0.8)0.2231,LMSE=(10.8)2=0.04L_{\text{BCE}} = -\log(0.8) \approx 0.2231, \qquad L_{\text{MSE}} = (1-0.8)^2 = 0.04

Now suppose the model is badly wrong, y^=0.01\hat y = 0.01 for true label y=1y=1:

LBCE=log(0.01)4.605,LMSE=(10.01)20.980L_{\text{BCE}} = -\log(0.01) \approx 4.605, \qquad L_{\text{MSE}} = (1-0.01)^2 \approx 0.980

Notice how BCE penalizes the confidently-wrong prediction far more heavily (4.605 vs. 0.223 — a roughly 20-fold increase) than MSE does (0.980 vs. 0.04 — a 25-fold increase, but on a much smaller and less discriminative absolute scale). This is exactly why cross-entropy is generally preferred for classification: it produces much sharper gradients precisely for confidently wrong predictions, driving faster correction.

Implementing It

import numpy as np
 
def mse(y, y_hat):
    return np.mean((y - y_hat)**2)
 
def mae(y, y_hat):
    return np.mean(np.abs(y - y_hat))
 
def huber(y, y_hat, delta=1.0):
    e = y - y_hat
    is_small = np.abs(e) <= delta
    small_loss = 0.5 * e**2
    large_loss = delta * (np.abs(e) - 0.5*delta)
    return np.mean(np.where(is_small, small_loss, large_loss))
 
def binary_cross_entropy(y, y_hat, eps=1e-12):
    y_hat = np.clip(y_hat, eps, 1-eps)   # avoid log(0)
    return -np.mean(y*np.log(y_hat) + (1-y)*np.log(1-y_hat))
 
def categorical_cross_entropy(y_onehot, y_hat, eps=1e-12):
    y_hat = np.clip(y_hat, eps, 1.0)
    return -np.mean(np.sum(y_onehot * np.log(y_hat), axis=-1))
 
y = np.array([1.0, 0.0, 1.0])
y_hat = np.array([0.9, 0.2, 0.6])
print("MSE :", mse(y, y_hat))
print("MAE :", mae(y, y_hat))
print("BCE :", binary_cross_entropy(y, y_hat))

The eps clipping inside binary_cross_entropy and categorical_cross_entropy isn't decorative — it directly prevents the failure mode explained below.

Always clip probabilities before taking a log

Since cross-entropy involves log(y^)\log(\hat y), an unclipped prediction of exactly 0.0 or 1.0 — which can occur due to floating-point rounding — produces -\infty or NaN, silently corrupting training. Always clip predicted probabilities away from the exact endpoints, e.g. to [1012,11012][10^{-12}, 1-10^{-12}], before computing any cross-entropy loss in code.

Comparison Table

Loss functions at a glance
LossTaskNotes
MSERegressionSensitive to outliers; MLE under Gaussian noise
MAERegressionRobust to outliers; constant-magnitude gradient
HuberRegressionCombines MSE (near 0) and MAE (far from 0) behavior
Binary Cross-EntropyBinary classificationPaired with sigmoid; MLE under Bernoulli
Categorical Cross-EntropyMulti-class classificationPaired with softmax; MLE under Categorical
Sparse Categorical CEMulti-class classificationSame as CCE, integer labels instead of one-hot
HingeBinary classification (margin-based)Historically used with SVMs

Key Takeaways

Key Takeaways
  • Loss functions aren't arbitrary: MSE follows from MLE under Gaussian noise; cross-entropy losses follow from MLE under Bernoulli/Categorical distributions.
  • MAE and Huber loss trade some smoothness for robustness to outliers in regression.
  • Sigmoid + BCE and Softmax + CCE both produce the elegant gradient simplification y^y\hat y - y, proven explicitly above.
  • Sparse categorical cross-entropy is mathematically identical to categorical cross-entropy; it only changes the label encoding.
  • Hinge loss encourages margin-based separation, historically associated with SVMs.

Common Mistakes

Using MSE for classification

MSE's gradient doesn't distinguish sharply between a confidently wrong prediction and a mildly wrong one the way cross-entropy does, and it isn't the MLE-correct loss under a Bernoulli or Categorical label distribution. Use cross-entropy losses for classification tasks.

Forgetting that sparse CCE and CCE are the same loss

It's easy to assume sparse categorical cross-entropy is a fundamentally different, cheaper approximation. It isn't — it computes the identical value and gradient; only the label format differs.

Interview Corner

Quick Check
1. What does the sigmoid + binary cross-entropy derivation prove about δ^[L]?
2. Why is MSE generally avoided for classification tasks in favor of cross-entropy?

Where This Shows Up in Practice

Every training loop you'll ever write starts by picking a loss function, and that choice ripples through the entire rest of the pipeline: it determines what output activation is valid (sigmoid pairs with BCE, softmax pairs with CCE, linear pairs with MSE/MAE/Huber), and it determines exactly what gradient backpropagation starts with at the output layer. The clean y^y\hat y - y simplification derived in this chapter isn't just a mathematical curiosity — it's the literal first line of code in the backward pass of nearly every classification network in production today.