Language Modelling, Cross Entropy and Perplexity
The autoregressive factorisation, maximum likelihood as KL minimisation, what perplexity measures, and where the entropy floor comes from.
The factorisation
For any distribution on sequences over a finite vocabulary,
where and the factor is the marginal .
Key idea
The factorisation is an identity, not a modelling assumption. So an autoregressive language model loses no generality — it can represent any distribution over sequences. What is a modelling choice is (i) the parameterisation of each conditional, (ii) the left-to-right ordering, and (iii) the tokenisation. Only (i) limits expressivity in the usual sense; (ii) matters for what is easy to learn and for what inference is cheap; (iii) changes the object being modelled entirely.
Maximum likelihood and cross entropy
Let be the model's conditional, produced by a softmax over logits where is the final-layer representation at position and is the unembedding.
Let be the empirical distribution of the data. Then
where does not depend on . Hence minimising cross entropy is exactly minimising .
Suppose the data is drawn from a true distribution . Then for any model ,
with equality if and only if almost everywhere.
Key idea
This is the origin of the term in the scaling laws: a fitted loss curve has an irreducible floor, and the floor has an interpretation — it is (an upper bound on) the conditional entropy of the true text distribution at the given tokenisation. Language is genuinely stochastic; no model, however large, reaches zero loss.
Perplexity, and why it is not portable
If the model were uniform over equally likely options at each step, the perplexity would be exactly — hence the reading as an effective branching factor.
Let a corpus have characters and be tokenised two ways, into and tokens. Two models assigning the same probability to the corpus have per-token losses
so their perplexities differ by the factor . Only the per-character (or per-byte) loss is tokeniser-independent.
Gradients
Let be the logits, , and the loss for the true token . Then
and consequently and .
Key idea
The gradient is the prediction minus the target — exactly as in logistic regression and linear regression. The composition of softmax and cross entropy is what makes this clean: the softmax Jacobian , which saturates and kills gradients on its own, combines with the from the logarithm to cancel exactly. This is why the two are always implemented as a single fused operation, and why using a softmax with a squared loss instead reintroduces the saturation problem.
Sampling
Let . Then is non-decreasing in , with uniform as and the argmax one-hot as (assuming a unique maximum).
Problems
Maximum likelihood is KL minimisationProof · Beginner · 15 min The irreducible loss is the conditional entropyProof · Intermediate · 30 min Perplexity is not comparable across tokenisersNumerical · Beginner · 15 min The cross-entropy gradient is the prediction errorProof · Beginner · 15 min Temperature monotonically controls entropyProof · Advanced · 30 minReferences
- PaperClaude E. Shannon (1948). A Mathematical Theory of Communication
- BookThomas M. Cover and Joy A. Thomas (2006). Elements of Information Theory
- PaperYoshua Bengio, Réjean Ducharme, Pascal Vincent and Christian Jauvin (2003). A Neural Probabilistic Language Model
- PaperRico Sennrich, Barry Haddow and Alexandra Birch (2016). Neural Machine Translation of Rare Words with Subword Units
- PaperAri Holtzman, Jan Buys, Li Du, Maxwell Forbes and Yejin Choi (2020). The Curious Case of Neural Text Degeneration
These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.
Results stated on this page
- PropositionThe autoregressive factorisation is exactproof includedAny joint distribution over sequences factorises as a product of conditionals, so the modelling choice is the parameterisation and the ordering, not the factorisation.
- TheoremMaximum likelihood is empirical KL minimisationproof includedMinimising cross entropy is minimising the KL divergence from the data distribution up to a constant equal to the data entropy.
- PropositionThe loss cannot go below the conditional entropyproof includedCross entropy is bounded below by the entropy of the true conditional, with equality only for a perfect model.
- DefinitionPerplexityThe exponential of the average cross entropy, interpretable as an effective branching factor.
- PropositionPerplexity is not comparable across tokenisersproof includedChanging the tokenisation changes the per-token loss even when the per-character loss is identical.
- PropositionThe cross-entropy gradient is the prediction errorproof includedThe gradient with respect to the logits is the predicted distribution minus the one-hot target.
- PropositionTemperature monotonically controls entropyproof includedRaising the sampling temperature increases the entropy of the sampled distribution.
Problems (5)
All problems- The irreducible loss is the conditional entropyProofIntermediate⏱ 30 min
- Maximum likelihood is KL minimisationProofBeginner⏱ 15 min
- Perplexity is not comparable across tokenisersNumericalBeginner⏱ 15 min
- The cross-entropy gradient is the prediction errorProofBeginner⏱ 15 min
- Temperature monotonically controls entropyProofAdvanced⏱ 30 min
Papers that use this mathematics
Attention Is All You Need
Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin · 2017
Scaling Laws for Neural Language Models
Jared Kaplan, Sam McCandlish, Tom Henighan, Tom B. Brown, Benjamin Chess, Rewon Child, Scott Gray, Alec Radford, Jeffrey Wu, Dario Amodei · 2020
Direct Preference Optimization: Your Language Model is Secretly a Reward Model
Rafael Rafailov, Archit Sharma, Eric Mitchell, Stefano Ermon, Christopher D. Manning, Chelsea Finn · 2023
Training Compute-Optimal Large Language Models
Jordan Hoffmann, Sebastian Borgeaud, Arthur Mensch, Elena Buchatskaya, Trevor Cai, Eliza Rutherford, Diego de Las Casas, Lisa Anne Hendricks, Johannes Welbl, Aidan Clark and others · 2022
Training language models to follow instructions with human feedback
Long Ouyang, Jeffrey Wu, Xu Jiang, Diogo Almeida, Carroll L. Wainwright, Pamela Mishkin, Chong Zhang, Sandhini Agarwal, Katarina Slama, Alex Ray and others · 2022
Language Models are Few-Shot Learners
Tom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, Jared Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell and others · 2020
Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
Patrick Lewis, Ethan Perez, Aleksandra Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Küttler, Mike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela · 2020
BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova · 2019