0.7 — Cross-entropy as a loss, and why minimising it is MLE
Goal: Show that −Σᵢ log p(yᵢ|xᵢ; θ) is the cross-entropy between the empirical distribution and the model, so MLE and cross-entropy minimisation are the same objective.
1. The starting point: i.i.d. data and a parametric model
We have a training set assumed to be i.i.d. draws from some unknown joint . We specify a parametric conditional model and want to fit to the data.
(I write instead of for the model to keep the truth/model distinction explicit — same convention as §0.6.)[1]
2. The likelihood
Under the i.i.d. assumption, the joint probability the model assigns to the labels given the inputs factorises:
This is the conditional likelihood of the labels under the model. As a function of with the data fixed, it is denoted .
3. Maximum likelihood estimation (MLE)
Maximum likelihood picks that maximises . Products are awful to differentiate, so take the log (which is monotone and so doesn't change the argmax):
Flip the sign to turn maximisation into minimisation (the convention in optimisation):
This is the negative log-likelihood (NLL). Divide by to get the per-sample version (gradients are then unitless in batch size):
4. The empirical distribution
Define the empirical distribution as the discrete measure that puts mass on each training point:
Expectation under is "average over the training set", which we already noted in §0.4:
5. NLL is cross-entropy
Apply this to :
That last line is the cross-entropy between and the conditional model , written with the joint factored as :
i.e. the input-averaged cross-entropy between the empirical conditional and the model conditional .
Putting it together:
MLE on a discriminative model is exactly cross-entropy minimisation on the empirical distribution. The two names refer to the same gradient.
6. Why the model's "missing" entropy term doesn't matter for gradients
Recall from §0.6 that . So minimising cross-entropy over is identical to minimising , since doesn't depend on and drops out of the gradient. Three names for the same optimisation:
- MLE (statistics).
- Cross-entropy minimisation (information theory).
- Forward KL minimisation against the empirical distribution (Bayesian ML).
7. The two cases you'll see daily
Binary classification with and for some predicted probability:
These two cases unify into one expression — the binary cross-entropy loss:
Multi-class classification with and for predicted class probabilities summing to 1 (e.g. coming from a softmax — see Ch. 1):
The last form () is what you read in code: take the
predicted probability of the true class, take its log, negate. That's
PyTorch's F.cross_entropy (modulo the fact that PyTorch's
cross_entropy expects pre-softmax logits and applies the softmax
internally for numerical stability — more on that in §0.10).
8. What we've earned
The same loss object reads three ways depending on the lens. Picking the right lens unstrucks problems:
- Statistical lens (MLE): what data-generating assumption does my loss implicitly make? E.g. mean-squared error ↔ Gaussian noise (Ch. 1).
- Information-theoretic lens (cross-entropy / KL): how far is my model from the empirical distribution? Variational and distillation methods sit here.
- Operational lens (NLL): what does the gradient look like and how do I implement it? See §0.10 for the stability tricks that make this practical.
For every loss in the rest of the course, ask which of the three views makes the calculation easy. Use that one.
A neural network outputs a single logit (a real number, no activation). The label is binary, .
- Apply the sigmoid: . Write the binary cross-entropy purely in terms of and , eliminating .
- Compute . You should get a famously clean expression involving . This is the identity that makes logistic regression numerically and didactically pleasant.
- Sanity check: at , the gradient is ; at , it is . Why does this match the intuition that "the loss pushes the logit up when and down when , proportionally to how wrong we are"?
References
- [1]
Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory (2nd ed.). Wiley. — Used in 0.6 (entropy, KL divergence) and 0.7 (cross-entropy as MLE).
↩