All courses

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

▶ Watch this lesson Start the whole course

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.

Two training goals, or one?

Training is described in two ways: "make the training data as probable as possible" and "minimize cross-entropy." Are these two goals that merely agree, or one goal in two notations? If they are literally the same, every fact about cross-entropy carries over to likelihood. That includes its floor, its link to KL, and what the best possible model looks like.

Group identical examples, then average

The likelihood is a product of the model's probabilities for each example. Taking the log turns it into a sum of log-probabilities. Identical examples add identical terms, so you can group them: each distinct outcome's $\log q$ times how often it occurs. Divide by the number of examples and the counts become frequencies. A frequency-weighted average of $-\log q$ is exactly cross-entropy.

The identity

Data $x_1,\dots,x_n$; $c(x)$ = count of outcome $x$; empirical distribution $\hat p(x)=c(x)/n$; model $q_\theta$. $$\sum_{i=1}^n \log q_\theta(x_i)=\sum_x c(x)\log q_\theta(x)=-n\,H(\hat p,q_\theta)$$ So the average negative log-likelihood is $H(\hat p,q_\theta)=H(\hat p)+\mathrm{KL}(\hat p\|q_\theta)$. $H(\hat p)$ does not depend on $\theta$. Because log is increasing, likelihood and log-likelihood peak at the same $\theta$. So maximizing likelihood, minimizing cross-entropy and minimizing KL all have the same best $\theta$.

Data a, a, b, c

$\hat p=(0.5,0.25,0.25)$. Model $q=(0.4,0.4,0.2)$. Log-likelihood: $3\ln0.4+\ln0.2=-2.749-1.609=-4.358$. Divided by $-4$: **1.090** nats. Cross-entropy: $0.75(0.916)+0.25(1.609)=$ **1.090**. They match. Floor: $H(\hat p)=0.5\ln2+0.5\ln4=1.040$, reached only when $q=\hat p$. The gap $0.050$ is $\mathrm{KL}(\hat p\|q)$.

Check it numerically

Average negative log-likelihood over examples equals cross-entropy against the empirical frequencies. Subtracting the empirical entropy leaves the KL.
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

Average negative log-likelihood on a dataset *is* $H(\hat p,q_\theta)$, the cross-entropy from the data's empirical frequencies to the model. Its floor is $H(\hat p)$, and the excess above that floor is $\mathrm{KL}(\hat p\|q_\theta)$. For an LLM, each example is a (context $x_{\le t}$, next token $x_{t+1}$) pair and $q_\theta$ is $p_\theta(x_{t+1}\mid x_{\le t})$. The same grouping applies, and the average over all pairs is the training loss $\mathcal{L}$.

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.