Every loss is a negative log-likelihood under some noise model, and choosing a loss is choosing that model whether or not anyone says so.
A loss is the sentence 'here is what wrong means', written so a derivative exists.
How this chapter is built
M3Load-bearing
The content is mathematics. Understanding is demonstrated by computation, not recall.
basics11/11what the words mean
concept2/2what to picture
theory4/4why it works, and when it does not
mathematics15/15derive it, then compute it
practice9/9build it, break it, read the papers
Five strands, not one. Mathematics is the spine; the other four are the body. A chapter cannot pay its way out of teaching with problems, nor out of problems with teaching.
Training needs one scalar to descend. Which scalar is not a detail: it fixes the implied noise model, it fixes what the optimal prediction even is, and — as this chapter's counterexample shows — it decides whether any gradient at all reaches a model at the point where it is most confidently wrong.
Fig. 4 — A loss surface drawn as contours, with a deep basin and a shallower one. Descent finds the nearer minimum, which is not always the better one.
What this chapter covers
Loss and risk
Mean squared error
Mean absolute error
Huber loss
Binary cross-entropy
Categorical cross-entropy
Logits and log-sum-exp
Class weighting
Label smoothing
Apparatus
The mathematics this chapter leans on, held in Book 0 so it can be assumed here without being taught here. Not a gate — follow a link when a step stops making sense.
A loss ℓ : 𝒴 × 𝒴 → ℝ₊ assigns one non-negative number to a single prediction–target pair, smaller meaning better. It must be differentiable in its first argument almost everywhere, because that argument is what the optimiser moves. Nothing in the data selects it.
ℒThe loss
Definition 2Reduction
The reduction is the function turning per-example losses into the one scalar that is differentiated — almost always the arithmetic mean. The choice is invisible in the formula and decides how much each example contributes to the gradient.
Definition 3Mean squared error
MSE = (1/n) Σ (ŷᵢ − yᵢ)². Its derivative with respect to a residual is proportional to that residual, so a residual twice as large pulls twice as hard and one ten times as large pulls ten times as hard.
Definition 4Mean absolute error
MAE = (1/n) Σ |ŷᵢ − yᵢ|. Its derivative with respect to a residual is ±1 regardless of magnitude, so every example pulls equally hard whatever its error. It is not differentiable at zero.
Definition 5Huber loss
Huber_δ(r) = ½r² for |r| ≤ δ and δ(|r| − ½δ) otherwise. It is quadratic near zero and linear beyond δ, so it is differentiable everywhere and its derivative is bounded by δ.
Definition 6Logit
A logit is an unnormalised real-valued score, before any function maps it to a probability. Logits live on all of ℝ; probabilities live in (0,1). Every numerically sound loss in this chapter is computed from logits, never from probabilities.
Definition 7Softmax
softmax(z)ᵢ = e^{zᵢ} / Σⱼ e^{zⱼ}, mapping a logit vector to the probability simplex. It is invariant to adding a constant to every logit, which is why it can be computed after subtracting the maximum without changing the answer.
For a target distribution p and a predicted q, H(p, q) = −Σ p(x) log q(x). With p one-hot it collapses to −log q(c) for the true class c, so all but one term of the sum vanish.
ℋEntropy, in nats unless bits are named
Definition 9Label smoothing
Label smoothing replaces the one-hot target by (1 − ε)·onehot + ε/K applied uniformly. It caps the reward for confidence: the loss is then minimised at a finite logit gap rather than at an infinite one.
Formal results
I.4.T1
Losses as negative log-likelihoods
Minimising MSE is exactly maximising the likelihood of a Gaussian observation model with fixed variance; minimising cross-entropy is exactly maximising the likelihood of a categorical model. In both cases the loss is −log p(y | x, θ) up to an additive constant and a positive scale, neither of which changes the minimiser.
What this does not promise
It does not promise the implied noise model is true of your data. Choosing MSE asserts symmetric, constant-variance, unbounded Gaussian errors — an assertion that is false for counts, for bounded quantities, and for anything heavy-tailed. The correspondence is a fact about the algebra, not evidence about the world, and it is most dangerous when nobody realises an assertion was made.
I.4.T2
Bayes-optimal predictors
Under squared loss the risk-minimising prediction at x is the conditional mean 𝔼[Y | X = x]; under absolute loss it is a conditional median; under cross-entropy it is the true conditional distribution p(· | x). Each is derived by differentiating the expected loss and setting it to zero.
What this does not promise
These describe the minimiser over all measurable functions, not over the hypothesis class the model actually searches. A network trained on squared loss approaches the conditional mean only insofar as the mean lies in its class and the optimiser finds it — two conditions I.1.T1 and I.3.T2 already warned are separate. They also assume the training distribution equals the deployment one.
Assumptions
Each is paired with the problem that shows what its removal costs. An assumption nobody tests is a disclaimer.
The implied noise model of the chosen loss matches the data — Gaussian for MSE, categorical for cross-entropy.I.4.X07
The reduction is a plain mean, so every example contributes equally to the gradient.I.4.X08
Squared loss through a sigmoid stops sending gradient exactly where the model is most wrong. At logit z = −10 with target 1, MSE's gradient is 4.54e-5 while cross-entropy's is 0.99996 — a ratio of 22,029. At z = −20 the ratio is 4.85e+8. The model is as wrong as it can be and MSE reports almost nothing.
Naive binary cross-entropy raises an exception on ordinary logits. Computing p = sigmoid(z) then log(p) at z = −800 overflows exp(800) to infinity, giving p = 0 and log(0); at z = +800 with target 0, sigmoid rounds to exactly 1.0 and log(1 − 1) fails. The stable identity returns 800.000000 in both cases.