Skip to content
nonconvexcard

Language Modelling, Cross Entropy and Perplexity

The autoregressive factorisation, maximum likelihood as KL minimisation, what perplexity measures, and where the entropy floor comes from.

LLMsGraduate⏱ 3 hours12 min readlanguage modelcross entropyperplexitykl divergencetokenisation
5 problems →

The factorisation

PropositionThe autoregressive factorisation is exact

For any distribution pp on sequences x=(x1,…,xT)x = (x_1,\dots,x_T) over a finite vocabulary,

p(x1,…,xT)=∏t=1Tp(xt∣x<t),p(x_1,\dots,x_T) = \prod_{t=1}^T p\big(x_t \mid x_{<t}\big),

where x<t=(x1,…,xt−1)x_{<t} = (x_1,\dots,x_{t-1}) and the t=1t=1 factor is the marginal p(x1)p(x_1).

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 qθ(⋅∣x<t)q_\theta(\cdot\mid x_{<t}) be the model's conditional, produced by a softmax over logits zt=WUhtz_t = W_U h_t where hth_t is the final-layer representation at position tt and WU∈R∣\Vcal∣×dW_U\in\R^{|\Vcal|\times d} is the unembedding.

DefinitionCross-entropy loss
L(θ)=−1∣D∣∑x∈D∑t=1Tlog⁡qθ(xt∣x<t).\mathcal{L}(\theta) = -\frac{1}{|\Dcal|}\sum_{x\in\Dcal}\sum_{t=1}^{T}\log q_\theta\big(x_t\mid x_{<t}\big) .
TheoremMaximum likelihood is empirical KL minimisation

Let p^\hat p be the empirical distribution of the data. Then

L(θ)=H(p^)+KL(p^ ∥ qθ),\mathcal{L}(\theta) = H(\hat p) + \mathrm{KL}\big(\hat p \,\|\, q_\theta\big),

where H(p^)=−∑xp^(x)log⁡p^(x)H(\hat p) = -\sum_x\hat p(x)\log\hat p(x) does not depend on θ\theta. Hence minimising cross entropy is exactly minimising KL(p^∥qθ)\mathrm{KL}(\hat p\|q_\theta).

PropositionThe loss cannot go below the conditional entropy

Suppose the data is drawn from a true distribution pp. Then for any model qq,

Ex<t∼p[−∑vp(v∣x<t)log⁡q(v∣x<t)]  ≥  Ex<t∼p[H(p(⋅∣x<t))],\E_{x_{<t}\sim p}\Big[-\sum_{v}p(v\mid x_{<t})\log q(v\mid x_{<t})\Big] \;\ge\; \E_{x_{<t}\sim p}\big[H\big(p(\cdot\mid x_{<t})\big)\big],

with equality if and only if q=pq = p almost everywhere.

Key idea

This is the origin of the L∞\mathcal{L}_\infty term in the scaling laws: a fitted loss curve L(N,D)=L∞+AN−α+BD−β\mathcal{L}(N,D) = \mathcal{L}_\infty + AN^{-\alpha}+BD^{-\beta} 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

DefinitionPerplexity
PPL=exp⁡(1T∑t=1T−log⁡qθ(xt∣x<t))=eLˉ.\mathrm{PPL} = \exp\left(\frac{1}{T}\sum_{t=1}^T -\log q_\theta(x_t\mid x_{<t})\right) = e^{\bar{\mathcal{L}}} .

If the model were uniform over kk equally likely options at each step, the perplexity would be exactly kk — hence the reading as an effective branching factor.

PropositionPerplexity is not comparable across tokenisers

Let a corpus have CC characters and be tokenised two ways, into T1T_1 and T2T_2 tokens. Two models assigning the same probability to the corpus have per-token losses

Lˉi=−log⁡q(corpus)Ti,\bar{\mathcal{L}}_i = \frac{-\log q(\text{corpus})}{T_i},

so their perplexities differ by the factor exp⁡(−log⁡q⋅(1/T1−1/T2))\exp\big(-\log q\cdot(1/T_1-1/T_2)\big). Only the per-character (or per-byte) loss −log⁡q(corpus)/C-\log q(\text{corpus})/C is tokeniser-independent.

Gradients

PropositionThe cross-entropy gradient is the prediction error

Let z∈R∣\Vcal∣z\in\R^{|\Vcal|} be the logits, q=softmax⁡(z)q = \operatorname{softmax}(z), and the loss ℓ=−log⁡qv⋆\ell = -\log q_{v^\star} for the true token v⋆v^\star. Then

∂ℓ∂z=q−ev⋆,\frac{\partial\ell}{\partial z} = q - e_{v^\star},

and consequently ∂ℓ∂WU=(q−ev⋆)h⊤\dfrac{\partial\ell}{\partial W_U} = (q-e_{v^\star})h^\top and ∂ℓ∂h=WU⊤(q−ev⋆)\dfrac{\partial\ell}{\partial h} = W_U^\top(q-e_{v^\star}).

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 diag⁡(q)−qq⊤\diag(q)-qq^\top, which saturates and kills gradients on its own, combines with the 1/qv⋆1/q_{v^\star} 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

PropositionTemperature monotonically controls entropy

Let q(τ)=softmax⁡(z/τ)q^{(\tau)} = \operatorname{softmax}(z/\tau). Then H(q(τ))H(q^{(\tau)}) is non-decreasing in τ>0\tau>0, with q(τ)→q^{(\tau)}\to uniform as τ→∞\tau\to\infty and q(τ)→q^{(\tau)}\to the argmax one-hot as τ→0+\tau\to0^+ (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 min

References

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

Problems (5)

All problems

Papers that use this mathematics