Maximum likelihood as cross-entropy minimization
Show that maximizing the log-likelihood of a dataset equals minimizing the average cross-entropy between the empirical data distribution and the model.
Builds on: Cross-entropy and KL divergence
What the animation says
Two names, one goal?
Training has two descriptions: make the data probable, or minimize cross-entropy. One goal, or two? Four examples: a, a, b, c. The model gives a point four, b point four, c point two. Likelihood multiplies the model's probability for each example. That's about point zero one two eight. Logs turn the product into a sum: four terms adding to minus four point three six. Divide by minus four. The average negative log-likelihood is one point zero nine nats. Now compute cross-entropy from the data's frequencies to the model. How does it compare?
Group, then average
It's exactly one point zero nine, not roughly. Here's why. Identical examples add identical terms. So group by outcome: a's term twice, b's once, c's once. Divide by four, and every count becomes a frequency: a one half, b a quarter, c a quarter. Now we're averaging minus log of the model's probability, weighted by how often the data shows each outcome. That weighted average is the definition of cross-entropy, data first, model second. This works on any dataset, of any size.
The floor and the gap
Cross-entropy splits in two: the data's own entropy, plus the KL divergence from data to model. Here the entropy is one point zero four. No model can score below that floor. Our model sits at one point zero nine. The gap, about point zero five, is the KL. The entropy ignores the model's parameters, and taking logs never changes which parameters win. So likelihood, cross-entropy and KL share one best model. Three candidate models. Which one scores lowest on this data? Tap it.
Check it in code
Copying the data's frequencies lands on the floor, KL zero. Overweighting a, at point six, costs about point zero two. Now check the identity in code. This line averages negative log-probabilities over the four examples. These lines turn counts into frequencies and take cross-entropy. Both prints show one point zero eight nine six. Subtract the data's entropy, and what's left is the KL: about point zero five. Last step: for a language model, what counts as one example?
The same, for an LLM
One example is a context plus the token that follows it. The model's probability is its prediction for that next token, given the context before it. Every position in the text gives one such pair. Average minus the log over all of them, and that is the training loss. So the loss is cross-entropy from the data to the model. Its floor is the data's entropy, and everything above it is KL. Maximizing likelihood, minimizing cross-entropy: one goal, two notations.
Group identical examples, then average
The identity
Data a, a, b, c
Check it numerically
import numpy as np
data = np.array([0, 0, 1, 2]) # a, a, b, c
q = np.array([0.4, 0.4, 0.2]) # model probabilities
avg_nll = -np.log(q[data]).mean()
p_hat = np.bincount(data, minlength=3) / len(data)
cross_ent = -(p_hat * np.log(q)).sum()
entropy = -(p_hat * np.log(p_hat)).sum()
print(avg_nll, cross_ent) # ≈ 1.0896 1.0896
print(cross_ent - entropy) # KL ≈ 0.0499
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses