Information Theory Basics: Entropy / Cross-Entropy / KL Divergence
In one sentence: SFT's loss is cross-entropy, the core constraint of DPO and RLHF is KL divergence, and distillation is essentially KL minimization—these terms are really one and the same language of "how many bits it takes to describe a distribution." This page strings them together with "bits" in plain words; once you get it, revisiting the loss functions in the main text becomes much clearer.
A great many loss functions across this site are built on these few concepts, yet they are often glossed over as "already known." Instead of piling on formulas, let's first make clear what each quantity actually measures, why its unit is the "bit," and how they relate to one another.
A unified perspective: information = "how surprising"
Information theory starts from a plain intuition: the less likely an event is, the more information it carries when it actually happens.
- "The sun rises as usual tomorrow"—nearly certain, saying it is as good as saying nothing, information ≈ 0.
- "A meteorite lands on our city tomorrow"—extremely improbable, but once it happens the information explodes.
To quantify this intuition: an event with probability
I = \log_2 \frac{1}{p} = -\log_2 p .We take the
The real meaning of a bit: one bit = the uncertainty a single yes/no question can eliminate. An event with probability
In machine learning, loss functions often use the natural logarithm
(base ), in which case the unit is the nat. Bits and nats differ only by the constant ; the conversion changes no optimization conclusion, so in the main text does not specify a base.
Entropy: how uncertain a distribution is "on average"
A single event has information content, so how much information does an entire probability distribution carry on average? That is the entropy—the expectation of self-information weighted by probability:
H(p) = \mathbb{E}_{x\sim p}\big[-\log p(x)\big] = -\sum_x p(x)\log p(x).Intuitively, entropy measures how "messy" / how hard to guess the distribution is:
- A fair coin,
, entropy = 1 bit (most uncertain, hardest to guess). - A biased coin,
, entropy ≈ 0.08 bits (almost always heads, easy to guess). - A coin that always lands heads,
, entropy = 0 (no suspense, zero information).
From an engineering angle: entropy is "with the optimal coding scheme, the minimum number of bits needed on average to store each sample." The more certain a distribution, the more compressible, and the lower its entropy.
Cross-entropy: how many extra bits it costs to encode with the "wrong" distribution
Now two distributions appear:
: the true distribution (the actual distribution of tokens in the data; during training it is the one-hot label). : the model's predicted distribution (the softmax probabilities output by ).
Cross-entropy measures: the true data comes from
H(p, q) = \mathbb{E}_{x\sim p}\big[-\log q(x)\big] = -\sum_x p(x)\log q(x).Note the distinction: the expectation is taken over the true distribution
This is precisely SFT's loss. During training the true label is one-hot (the correct token has probability 1, the rest 0), so the cross-entropy
\mathcal{L}_{\text{SFT}}(\theta) = -\,\mathbb{E}_{(x, y) \sim \mathbb{D}} \left[ \sum_{t=1}^{\lvert y \rvert} \log \pi_\theta\big(y_t \mid x, y_{\lt t}\big) \right].Minimizing cross-entropy = making the model assign as high a probability as possible to the correct token. See SFT Overview.
KL divergence: how far apart two distributions are
Cross-entropy actually mixes two parts: the data's inherent uncertainty (entropy
\mathrm{KL}(p \,\|\, q) = \mathbb{E}_{x\sim p}\!\left[\log \frac{p(x)}{q(x)}\right] = \sum_x p(x)\log\frac{p(x)}{q(x)}.It measures "how many bits per sample are wasted on average by passing off
- Non-negative:
, and equals 0 if and only if . The more alike the two distributions, the smaller the KL. - Asymmetric:
. That is why it is called a "divergence," not a "distance," and you must keep straight which one comes first. - It is exactly cross-entropy minus entropy—the core identity that ties all three together.
Tying it together: a one-line identity
\underbrace{H(p, q)}_{\text{cross-entropy}} = \underbrace{H(p)}_{\text{entropy of the true distribution}} + \underbrace{\mathrm{KL}(p \,\|\, q)}_{\text{model bias}}.In plain words:
The total cost of encoding the true data with model
(cross-entropy) = the data's intrinsic, incompressible cost (entropy) + the extra penalty paid because the model is inaccurate (KL divergence).
This explains why these losses look the way they do:
- Why does simply minimizing cross-entropy work when training SFT? Because
is intrinsic to the data, a constant independent of the parameters , so minimizing the cross-entropy is exactly equivalent to minimizing —that is, making the model distribution approach the data distribution. - Why does distillation use KL rather than cross-entropy? In distillation the "true distribution" is the teacher model's soft labels (not one-hot), so
is no longer a constant; writing it directly as is cleaner and clearer in meaning: make the student distribution match the teacher distribution. See Distillation Overview.
Perplexity: the "exponential version" of cross-entropy
The perplexity (PPL) you hear about most often when evaluating language models is not actually anything new—it is just cross-entropy exponentiated:
\text{PPL}(p, q) = e^{H(p, q)} = \exp\!\left(-\frac{1}{N}\sum_{t=1}^{N}\log q(y_t \mid y_{\lt t})\right).Why bother taking an exponent? Because it switches to a more intuitive unit:
- Cross-entropy lives on a logarithmic scale, with units of bits / nats, answering "how many bits each token costs on average";
- Perplexity exponentiates back to a linear scale, answering "at each step, the model is hesitating as if among how many equally likely options"—i.e., the "effective branching factor."
For example: a model with PPL = 20 on a test set means that its uncertainty when predicting the next word is equivalent to choosing among 20 equally likely words at each step. Lower is better: a perfect model has PPL = 1 (certain at every step, not perplexed at all), and a purely random 1-out-of-
So the several quantities on this page form a chain: self-information → entropy / cross-entropy (take the expectation) → perplexity (then take the exponent). The three correspond monotonically, so minimizing SFT's cross-entropy loss is exactly equivalent to minimizing perplexity—which is why PPL is the most common intrinsic evaluation metric for language models.
It is everywhere in post-training
KL divergence shows up repeatedly in the alignment stage, always playing the same role: acting as a "don't stray too far" rein—optimizing the objective while forbidding the new model from drifting too far from the reference model
| Where it appears | What KL is doing |
|---|---|
| SFT | The loss is cross-entropy / NLL, equivalent to minimizing |
| PPO / RLHF | A |
| DPO | The |
| Distillation | Directly minimize the KL between the student's and teacher's output distributions |
This also explains why the Notation Conventions define
A quick-reference table
| Quantity | Formula | One-line meaning |
|---|---|---|
| Self-information | How "surprising" a single event is, in bits | |
| Entropy | How uncertain a distribution is on average / minimum bits to store it | |
| Cross-entropy | Bits needed on average to encode data from | |
| KL divergence | Bits wasted by passing off | |
| Perplexity | Cross-entropy exponentiated; how many "options" the model hesitates among on average | |
| Core relationship | Total cost = intrinsic cost + model bias |
Once you understand this table, revisiting the loss functions of SFT, DPO, PPO, and Distillation will no longer leave you stuck on words like "cross-entropy" and "KL."