Squared error for regression, cross-entropy for classification, next-token log-loss for language models: these loss functions look like separate conventions, but they are one idea applied to different noise models. Maximum likelihood says to choose the parameters under which the data you actually observed would have been most probable.
The likelihood function
A model is a family of probability distributions indexed by parameters : a coin's bias, a Gaussian's mean, or the billions of weights of a neural network. Given independent observations , the likelihood is the probability of those data viewed as a function of :
The likelihood is not a probability distribution over : it need not integrate to one, and treating it as a posterior requires a prior (see Bayes' rule). It is a score for how well each candidate explains the data.
The maximum likelihood estimate is . R. A. Fisher set out the method and its theory in his 1922 paper on the foundations of theoretical statistics, which also introduced the now-standard distinction between a population parameter and a sample statistic.[1]
Because a product of many small numbers underflows, and because logarithm is increasing (so it does not move the maximum), one always works with the log-likelihood and, by convention in machine learning, minimises its negative:
That negative log-likelihood (NLL) is the loss function.
Worked example: a biased coin
Observe 10 flips with heads. Model each flip as heads with probability . The log-likelihood is
Set the derivative to zero: , so and . In general : the maximum likelihood estimate of a probability is the observed frequency. Unsurprising, but derived rather than assumed.
The same calculation shows a weakness. With 3 flips and 3 heads, : the model declares tails impossible. Maximum likelihood trusts the sample completely. Laplace's rule of succession, , which results from a uniform prior on , gives instead.[2] In language modelling the same failure appears as assigning zero probability to unseen word sequences, which is why classical -gram models needed smoothing.
Different noise, different loss
Gaussian noise gives squared error
Suppose a model predicts and the observed target is with . Then
With fixed, the second term is a constant, so maximising likelihood is exactly minimising the sum of squared errors.[3] Least squares is maximum likelihood under Gaussian noise. If the noise were Laplace-distributed, the same derivation would give absolute error instead, which is less sensitive to outliers. Choosing a loss is choosing a noise model, whether or not you say so.
For data modelled as with known , what is the maximum likelihood estimate of ?
Show answer +Hide answer −
The NLL is plus a constant. Setting its derivative to zero gives , the sample mean.
Categorical outcomes give cross-entropy
For a classifier over classes, the model outputs a distribution , usually via a softmax. The probability of the observed label is , and the NLL over a dataset is
This is the cross-entropy between the data's labels and the model's predictions. Maximum likelihood is equivalent to minimising the KL divergence from the empirical data distribution to the model, because the two differ only by the data's own entropy, which does not depend on .[4] The information theory course develops that view.
Language models are maximum likelihood estimators
An autoregressive language model factorises a text with the chain rule, . Its log-likelihood is a sum over positions, so the training loss for a corpus is the average per-token NLL:
Every token in the training data is a classification example whose label is the token that actually came next. Bengio and colleagues' 2003 neural language model was trained by maximising this log-likelihood over its training corpus,[5] and the same objective, scaled up by many orders of magnitude, underlies modern pretraining. The empirical scaling laws of Kaplan and colleagues are statements about this cross-entropy loss as a function of model size, data and compute.[6]
Worked example: loss and perplexity
A model predicts four tokens of a sentence, assigning the true tokens probabilities 0.5, 0.25, 0.8 and 0.1. The per-token NLLs in nats are
The mean is nats per token. Perplexity is its exponential, : the model was, on average, as uncertain as a uniform choice among about three tokens. Equivalently, perplexity is one over the geometric mean of the assigned probabilities, . Note how the single confident miss (0.1) contributes half the loss: log-loss punishes assigning low probability to what actually happened far more than it rewards being right with high confidence.
A model assigns probability 0.02 to a token that actually occurs. What does that one token contribute to the loss, in nats? What would it contribute if the model had assigned probability 0?
Show answer +Hide answer −
nats. At probability 0 the loss is : maximum likelihood forbids ever declaring an observed outcome impossible, which is one reason softmax outputs are never exactly zero.
What maximum likelihood guarantees, and what it does not
Under regularity conditions and with a correctly specified model, the maximum likelihood estimator is consistent (it converges to the true parameter as ) and asymptotically efficient (no consistent estimator has smaller asymptotic variance). Fisher's 1922 paper introduced consistency and efficiency as criteria for estimators and argued that maximum likelihood meets them; fully rigorous proofs came later.[1]
Three caveats matter more in practice:
- Overfitting. With many parameters and finite data, the likelihood can be pushed up by memorising the sample. The coin with three heads is the smallest example. Adding a penalty, such as (weight decay), is equivalent to maximising a posterior under a Gaussian prior on the weights, known as MAP estimation.[3] See Generalization.
- Misspecification. If the true distribution is not in the model family, maximum likelihood finds the member closest in KL divergence. That can still be useful, but "best in the family" is not "correct".
- Likelihood is not the goal. A language model trained to maximise the likelihood of internet text learns to reproduce that text's distribution, errors included. Later stages of training (preference learning, reinforcement learning with verifiable rewards) exist precisely because the maximum likelihood objective does not specify helpfulness or correctness; see From RLHF to verifiable rewards.
Key takeaways
- Maximum likelihood picks the parameters that make the observed data most probable; in practice one minimises the negative log-likelihood.
- The loss encodes a noise model: Gaussian noise gives squared error, categorical outcomes give cross-entropy.
- Language-model pretraining is maximum likelihood estimation: minimising average per-token NLL, reported as loss in nats or as perplexity, its exponential.
- Maximum likelihood is consistent and efficient for well-specified models with enough data, but it overfits small samples; priors and regularisation are the correction.
Sources
-
R. A. Fisher (1922). "On the Mathematical Foundations of Theoretical Statistics." Philosophical Transactions of the Royal Society A 222: 309–368. https://doi.org/10.1098/rsta.1922.0009 ↩ ↩2
-
P.-S. Laplace (1814). Essai philosophique sur les probabilités. Paris: Courcier. Digitised copy, Kyoto University Rare Materials Digital Archive: https://rmda.kulib.kyoto-u.ac.jp/en/item/rb00028954 ↩
-
C. M. Bishop (2006). Pattern Recognition and Machine Learning. Springer. Free PDF via Microsoft Research: https://www.microsoft.com/en-us/research/publication/pattern-recognition-machine-learning/ ↩ ↩2
-
I. Goodfellow, Y. Bengio, A. Courville (2016). Deep Learning, ch. 5 "Machine Learning Basics." MIT Press. https://www.deeplearningbook.org/ ↩
-
Y. Bengio, R. Ducharme, P. Vincent, C. Jauvin (2003). "A Neural Probabilistic Language Model." Journal of Machine Learning Research 3: 1137–1155. https://www.jmlr.org/papers/v3/bengio03a.html ↩
-
J. Kaplan et al. (2020). "Scaling Laws for Neural Language Models." arXiv:2001.08361. https://arxiv.org/abs/2001.08361 ↩
