S. Roy

Blog Post

Probability for LLMs: Distributions, Entropy, and KL Divergence

A language model is a probability distribution over sequences. Training it means pushing that distribution toward the data distribution. The math of how you measure and minimize that gap is what this post covers.

Views: 5 min readCite

Part 1 treated the model as a machine that transforms vectors, but it never said what those vectors are for. They are for assigning probabilities, because a language model is, formally, a probability distribution over sequences of tokens. Using the chain rule of probability, the probability it assigns to a whole sequence factors into a product of next-token probabilities.

pθ(x)=t=1Tpθ(xtx<t)p_\theta(x) = \prod_{t=1}^{T} p_\theta(x_t \mid x_{<t})

Every sequence — every sentence, every document, every continuation — receives a number under this distribution, and training is the search for parameters θ\theta that make pθ(x)p_\theta(x) large for the sequences that actually occur in real text and small for the ones that do not.

To make that idea precise we need a way to quantify uncertainty, and the foundational quantity is entropy: the average surprise of a distribution, where surprise is measured as logp(x)-\log p(x).

H(p)=xp(x)logp(x)H(p) = -\sum_x p(x) \log p(x)

A uniform distribution over a vocabulary of VV tokens has entropy logV\log V, the maximum possible — every token is equally likely, so each one is maximally surprising — while a distribution that places all its mass on a single token has entropy zero, because nothing it produces is ever a surprise. English text sits somewhere well below the maximum; Shannon's 1951 estimate put the entropy of English at roughly 10 to 11 bits per word, and a model's perplexity is just the exponential of the cross-entropy it achieves, a direct reading of how surprised the model is, on average, by real text.

Entropy measures one distribution against itself; cross-entropy measures the cost of using the wrong one. It is the average surprise you incur when the data truly comes from pp but you encode it as though it came from qq.

H(p,q)=xp(x)logq(x)H(p, q) = -\sum_x p(x) \log q(x)

This is not a side quantity — it is the training loss. The standard language-modeling objective is the cross-entropy between the data distribution and the model, which is identical to the negative log-likelihood of the data, so minimizing the loss and maximizing the probability the model assigns to real text are the same thing said two ways.

L=Expdata[logpθ(x)]L = -\mathbb{E}_{x \sim p_\text{data}}[\log p_\theta(x)]

What cross-entropy does not tell you on its own is how far apart two distributions are, because it bundles in the irreducible entropy of the data. Subtracting that off leaves the Kullback–Leibler divergence, the number of extra bits you pay for using qq in place of the true pp.

KL(pq)=xp(x)logp(x)q(x)=H(p,q)H(p)\mathrm{KL}(p \,\Vert\, q) = \sum_x p(x) \log \frac{p(x)}{q(x)} = H(p, q) - H(p)

By Gibbs' inequality this quantity is always at least zero and is zero only when pp and qq are identical, which makes it the natural measure of a gap between distributions — but it is not symmetric, and the asymmetry has teeth. Forward KL, KL(pq)\mathrm{KL}(p \,\Vert\, q), is mode-covering: it punishes qq for putting low probability anywhere pp has mass, so it spreads qq out to cover everything. Reverse KL, KL(qp)\mathrm{KL}(q \,\Vert\, p), is mode-seeking: it lets qq ignore parts of pp entirely and concentrate on one mode. This distinction is not academic — in RLHF the penalty βKL(πθπref)\beta \cdot \mathrm{KL}(\pi_\theta \,\Vert\, \pi_\text{ref}) keeps the fine-tuned policy from drifting too far from the reference model, and which direction of KL you use shapes whether the policy hedges across many behaviors or commits hard to a few.

All of this presumes the model can produce a valid distribution in the first place, and the mechanism that turns raw logits into probabilities is the softmax. It exponentiates each logit and divides by the sum, so the outputs are positive and sum to one.

p(xi)=exp(zi)jexp(zj)p(x_i) = \frac{\exp(z_i)}{\sum_j \exp(z_j)}

The denominator is the partition function, the normalizer that makes the whole thing a probability distribution rather than just a set of scores, and a single knob — temperature — reshapes it: dividing the logits by τ\tau before exponentiating, p(xi)=exp(zi/τ)/jexp(zj/τ)p(x_i) = \exp(z_i/\tau) / \sum_j \exp(z_j/\tau), makes the distribution peakier and more confident as τ0\tau \to 0 until it collapses to the argmax of greedy decoding, and flatter and more uniform as τ\tau \to \infty until sampling becomes purely random. This is the same softmax that turns attention scores into attention weights, which is why the geometry of Part 1 and the probability of this post are really one continuous story.

Underneath the next-token prediction is conditional probability, the probability of one event given another, defined as the joint divided by the marginal.

p(AB)=p(A,B)p(B)p(A \mid B) = \frac{p(A, B)}{p(B)}

The model computes exactly this — pθ(xtx<t)p_\theta(x_t \mid x_{<t}), the next token given the context — and the same definition, rearranged, gives Bayes' theorem, p(θdata)p(dataθ)p(θ)p(\theta \mid \text{data}) \propto p(\text{data} \mid \theta)\, p(\theta). Maximum likelihood estimation drops the prior and maximizes p(dataθ)p(\text{data} \mid \theta) alone, which is what plain cross-entropy training does; adding L2 weight regularization quietly reintroduces a Gaussian prior on θ\theta and turns the procedure into maximum-a-posteriori estimation — the same weight decay whose correct implementation we will return to in Part 3.

Probability gives us the vocabulary of distributions and a precise way to measure the distance between the model and the data. What it does not give us is the machinery to actually close that distance over billions of parameters — that machinery is optimization, and it is where Part 3 goes next.

Cite this work

Generated from article front matter.

Roy, Swastik. (2024). Probability for LLMs: Distributions, Entropy, and KL Divergence. S. Roy. https://swastikroy.me/blog/math-llm-probability

Export PDF opens your browser’s print dialog — choose “Save as PDF” for a Zenodo-ready file.