What does it cost to describe data with the wrong probability model, and why is that cost the loss nearly every language model is trained on? Cross-entropy measures that cost, and KL divergence isolates the part that comes from the model being wrong.
Coding with the wrong distribution
Entropy told us that an outcome of probability deserves a codeword of about bits. Now suppose the data really come from , but we design our code from a model . Each outcome then costs , and the average cost is the cross-entropy:
Split it into two parts:
The first term is the unavoidable cost of the source's own uncertainty. The second is the Kullback–Leibler divergence, introduced by Solomon Kullback and Richard Leibler in 1951 as a measure of the information available for telling two distributions apart.[1] In coding terms, it is the expected number of extra bits you pay per symbol for using in place of .[2]
Gibbs' inequality
, with equality if and only if . One line of proof uses Jensen's inequality on the concave logarithm:
So . No model can beat the true distribution at coding its own data, and the gap is exactly the KL divergence.[2]
Worked example: KL is not symmetric
Let and let be uniform, . In bits:
- .
- , because every outcome costs under the uniform code.
- bits.
Now reverse the roles:
The two directions give different numbers. KL divergence is not a distance metric: it is asymmetric and does not satisfy the triangle inequality. Which direction you minimise changes what you get, as discussed below.
A model assigns probability to an outcome that actually occurs with . What is , and what does this imply for language-model training?
Show answer +Hide answer −
It is infinite: the term diverges. Under the forward direction, a model that rules out anything that can happen pays an unbounded penalty. That is why softmax outputs, which are never exactly zero, are a natural fit for cross-entropy training, and why language models spread at least a little probability over everything plausible.
Why this is the language-model loss
A language model defines conditional distributions over a vocabulary. Training on a corpus minimises the average negative log-probability of each actual next token:
This is cross-entropy with a "one-hot" target: the empirical distribution puts all its mass on the token that actually appeared. Averaged over the data distribution , it is
Since does not depend on , minimising cross-entropy is the same as minimising the forward KL divergence from the data to the model. It is also the same as maximising likelihood, because summed over the data is the negative log-likelihood. The probabilistic view of this is in Maximum likelihood. Three standard ways of describing training, as maximum likelihood, KL minimisation and code-length minimisation, all describe the same objective.
Units and perplexity
Training code usually computes the loss in nats. Dividing by gives bits per token. Perplexity is the exponentiated loss, . Suppose a model's validation loss is 2.0 nats per token. That is bits per token, and its perplexity is . On average the model is as uncertain as if it were choosing uniformly among about 7.4 tokens. Because the irreducible is unknown, a loss value alone does not tell you how close a model is to the floor. Loss comparisons are meaningful only on the same data with the same tokenizer.
The gradient is "prediction minus target"
With logits and , the cross-entropy against a target distribution (one-hot or soft) has a very simple gradient:
Each logit is pushed up by the probability the model failed to give the right answer and pushed down by the probability it gave the wrong ones. The gradient is bounded and well scaled, which is part of why softmax with cross-entropy is the default output layer for classifiers and language models. The full backward pass is covered in Backpropagation.
A three-token vocabulary gives softmax probabilities , and the correct token is the second one. What is the loss in nats, and what is the gradient with respect to the logits?
Show answer +Hide answer −
Loss nats. Gradient . A gradient-descent step raises the correct logit and lowers the other two in proportion to their probabilities.
Forward versus reverse KL
The direction of the divergence decides the behaviour of the fitted model:
| Objective | Penalises heavily | Typical behaviour of | Where it appears |
|---|---|---|---|
| Forward, | small where is large | Mass-covering: spreads out to cover every mode of | Maximum-likelihood pretraining; distillation |
| Reverse, | large where is small | Mode-seeking: concentrates on one high-probability region | Variational inference; KL penalties on a policy |
Two places in modern training make the distinction concrete:
- Distillation. A small "student" is trained to match a large "teacher" by minimising cross-entropy against the teacher's full output distribution ("soft targets"), not only the one-hot label.[3] Since the teacher's entropy is fixed, this is forward-KL minimisation toward the teacher. The soft targets carry information about which wrong answers are nearly right.
- Fine-tuning with a KL leash. In reinforcement learning from human feedback, the policy is rewarded by a learned reward model but penalised by a per-token KL term against the supervised starting model. The penalty discourages it from drifting into outputs the reward model scores well but the original model considers very unlikely.[4] Here the expectation is taken under the policy's own samples, which makes it the reverse direction.
Key takeaways
- Cross-entropy is the average code length when data from are coded with model . It equals .
- KL divergence is non-negative and zero only when . It is asymmetric, and the direction you choose determines whether the fitted model is mass-covering or mode-seeking.
- The language-model loss is per-token cross-entropy. Minimising it is the same as maximum likelihood and the same as minimising forward KL from data to model.
- The softmax cross-entropy gradient is : prediction minus target.
- Perplexity is . Loss numbers can be compared only on the same data and tokenizer, because the entropy floor is unknown.
Sources
-
S. Kullback and R. A. Leibler (1951). "On Information and Sufficiency." The Annals of Mathematical Statistics 22(1), 79–86. https://doi.org/10.1214/aoms/1177729694 ↩
-
T. M. Cover and J. A. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley-Interscience. https://doi.org/10.1002/047174882X ↩ ↩2
-
G. Hinton, O. Vinyals and J. Dean (2015). "Distilling the Knowledge in a Neural Network." arXiv:1503.02531. https://arxiv.org/abs/1503.02531 ↩
-
L. Ouyang, J. Wu, X. Jiang et al. (2022). "Training language models to follow instructions with human feedback." arXiv:2203.02155. https://arxiv.org/abs/2203.02155 ↩
