Module 3. Cross-Entropy and KL Divergence
How to read this module. Sections 3.1–3.7 form the main route. We begin with log-loss, separate uncertainty in the data from model mismatch, carry the decomposition into classification and language modeling, and end with the logit gradient and perplexity. Section 3.8 is a mathematical deepening on KL direction under a restricted model family. Section 3.9 is a map of applications; read it selectively and return to it as variational inference, distillation, and RLHF become relevant.
3.1. The model returns a distribution; the world returns one outcome
A probabilistic model rarely answers with a single class. It returns a distribution. Taking the argmax keeps the winner and discards most of the information.
Consider two three-class classifiers:
Both choose the first class. If that class is observed, model receives a much better logarithmic score:
But if the second class is correct, the picture reverses:
Confidence is valuable when it points in the right direction and expensive when the model is confidently wrong. This is exactly what logarithmic loss (log-loss) measures. For an observed outcome ,
The logarithm does two jobs at once. It turns the product of sequence probabilities into a sum of token losses, and it strongly penalizes a model that declared an observed event nearly impossible.
We now introduce the two roles that run through the module:
- is the distribution generating the data;
- is a probabilistic model with parameters .
In classification these are conditional distributions
and in an autoregressive language model,
We do not receive as a ready-made table. We observe individual outcomes from and ask, each time: how much probability did the model assign to what actually happened? Averaging those scores leads to cross-entropy.
Module 2 used entropy to quantify the uncertainty of one distribution. We now have two distributions, and the engineering question becomes: how much of the average loss is unavoidable uncertainty in the data, and how much comes from the model failing to match them?
Unless stated otherwise, the alphabet on the main route is finite. We write the logarithm base as : gives bits and gives nats. Deep-learning libraries normally use the natural logarithm, so training losses are usually reported in nats.
The historical line has two steps. Shannon tied to the information length of a message; Kullback and Leibler formalized a directed discrepancy between two distributions. Modern cross-entropy training joins those ideas.
3.2. Cross-entropy: the model's average logarithmic bill
For distributions and on the same alphabet, cross-entropy is
Read the formula by role:
- the first argument says where outcomes come from and what distribution defines the average;
- the second argument says whose probabilities appear inside the logarithm.
Cross-entropy is therefore directed:
in general. Swapping the arguments changes the experiment: it changes both the source of events and the model being scored.
From individual losses to an expectation
If are observations from , the sample average
estimates . Under standard conditions, the law of large numbers turns the sample average into the population expectation. That is why a training loop can work one example at a time even though the formal statement about model quality lives at the distribution level.
For conditional prediction—classification, probabilistic regression, or next-token prediction—the natural quantity is
The semicolon separates the data distribution from the predictor being evaluated. Below we usually call this simply conditional cross-entropy.
One label is one observation, not the entire conditional law
In supervised learning, a target label is often represented by a one-hot vector . The per-example loss is then
This is a convenient representation of the observed event. It does not turn the true into a degenerate one-hot law. The same input may admit ambiguous annotation, several valid continuations, label noise, or hidden context. A dataset reveals one outcome at a time; the distribution emerges through repetition and structure.
When the model calls a possible event impossible
Two boundary conventions matter. An outcome with zero mass under contributes nothing:
But if and , then
The model assigned zero probability to an event that occurs, so its average logarithmic bill is infinite. Finiteness is written as : the support of must lie inside the support of .
With finite logits, a softmax network assigns formally positive probability to every class. Numerically, however, very small values may underflow to zero. Cross-entropy is therefore computed through a stable log_softmax form rather than by applying softmax and log as separate operations.
3.3. KL divergence: the part of the loss attributable to the model
The entropy of the source is
Add and subtract inside the cross-entropy:
The second term is the KL divergence, or relative entropy:
This gives the central identity of the module:
Read the decomposition in words.
- is the uncertainty that remains even for an ideal predictor that knows ;
- is the extra bill caused by the mismatch between model and data distribution .
A useful compact formulation is:
KL is the average surcharge for using the wrong probabilistic model.

KL is not a metric: it is asymmetric and need not satisfy the triangle inequality. For optimization, that is not a defect. The problem itself is directed: outcomes come from , while probabilities are supplied by .
Why the surcharge is non-negative
If some has and , the KL divergence is . Otherwise, let
be the positive support and use :
Hence
Equality requires both on and no -mass outside . Therefore,
This is Gibbs' inequality. Module 4 will recover the same result from Jensen's inequality and the log-sum inequality. The mechanism matters here: average log-loss splits into an unavoidable term and a non-negative model error.
Log-loss asks for the full distribution
The decomposition immediately gives
with a unique minimum at . Log-loss is therefore a strictly proper scoring rule: in expectation, reporting the true distribution is optimal, not merely reporting the correct most likely class.
This is stronger than an argmax guarantee. Two predictors can have identical accuracy and different cross-entropy because one allocates probability more faithfully between the observed outcome and its alternatives. That is why log-loss is useful when probabilities, calibration, and downstream decisions under different error costs matter.
What maximum likelihood actually does
For a model , maximizing likelihood is equivalent to minimizing empirical negative log-likelihood (NLL). At the population level the objective is
because does not depend on .
If the model family contains and the usual statistical conditions hold, the population optimum is the true distribution. Under misspecification, the model chooses the best available KL projection:
This does not weaken the meaning of maximum likelihood; it states it precisely. The objective says what approximation counts as best inside the chosen family. Finite data, sample quality, and optimization determine how closely a particular training run approaches that population solution.
3.4. The wrong code: how KL becomes extra bits
In bits, the quantity
is the ideal information length of outcome under model . If actual symbols come from , the average bill is
An ideal code matched to the source itself would have average information length . The difference is
This gives KL an operational reading: the average number of extra bits paid when data from are encoded using probabilities from model .
A real binary prefix code needs integer lengths. The connection survives with a small overhead. A Shannon code built from uses
so
For blocks of length , the overhead is less than one bit per block, hence less than bit per symbol. Arithmetic and range coding implement this idea in practice. Thus “KL is extra bits” is not merely a metaphor; it is an asymptotic engineering statement.
A dyadic example: equal entropy, unequal mismatch cost
Let
Both distributions are dyadic, so their ideal lengths are integers:
| outcome | ||||
|---|---|---|---|---|
| 1 | 3 | |||
| 2 | 1 | |||
| 3 | 2 | |||
| 3 | 3 |
Their entropies are equal:
But the code matched to is especially expensive for the most frequent event , giving
In the reverse direction,
Equal entropy means that the two sources have equal average uncertainty. It does not mean that substituting one model for the other has equal cost. Direction determines which events occur often and what prices the mismatched code assigns to them.

3.5. One formula for classification and language modeling
Let be a context and a label or next token. The expected conditional log-loss is
Apply the previous decomposition at each context and then average over :
The distinction between task uncertainty and model error is now explicit.
- is the ambiguity of the next outcome after the available context;
- the conditional KL is the average mismatch between the true and modeled continuation distributions.
If the same context admits several natural answers, even an ideal model need not achieve zero loss on every example. It should distribute probability correctly across valid continuations.
An autoregressive sequence
For a sequence of length ,
The logarithm turns the product into a sum:
After averaging,
The KL chain rule then decomposes the joint mismatch step by step:
This is the exact information-theoretic form of standard language-model training: every token sends the model a logarithmic bill, and total NLL adds those bills along the sequence.
Teacher forcing: score the model where the data live
Under teacher forcing, the context comes from the data. This makes the objective tractable and directly evaluates the model's conditional probabilities on real prefixes. Those are precisely the prefixes weighting the KL terms above.
During free generation, the model gradually creates its own contexts. An early error changes the next input, then the next one, and the visited-prefix distribution may move from toward the distribution induced by . Validation NLL under teacher forcing therefore measures conditional modeling quality on the data distribution, but it does not replace a separate evaluation of behavior on the model's own trajectories.
This boundary is useful rather than discouraging. It suggests an evaluation design: held-out NLL tests probabilistic modeling of the data; generation tests, interactive scenarios, and robustness checks test behavior after the model has accumulated its own decisions.
What the next-token objective promises
At the population level, the objective rewards
for contexts drawn from the data. That is a strong, precise statement. Factual correctness, useful abstention, and preference alignment arise to the extent that the data and task definition encode them. Retrieval, verification tools, preference fine-tuning, and specialized evaluation add signals that ordinary next-token NLL does not contain.
Low NLL is therefore a foundational property of a probabilistic language model, but not the only metric of a deployed system.
3.6. Why CrossEntropyLoss is so convenient to optimize
Let the network produce logits , with probabilities
For a target distribution , where and , the cross-entropy loss is
This form exposes the optimization structure: logsumexp accounts for all classes, while the second term raises logits according to the target distribution.
Differentiation gives one of the most useful formulas in deep learning:
For a one-hot target ,
The model lowers logits for classes to which it assigned more probability than the target requires and raises the observed class logit. The update is already scaled by the current probabilities; no separate hand-designed softmax factor is needed.
The Hessian is
For every vector ,
Cross-entropy is therefore convex in the logits. A deep network is still non-convex in its weights because the logits are a nonlinear function of many layers of parameters.
The numerically stable form
Computing softmax and then log separately is unsafe: exponentials of large logits may overflow, while tiny probabilities may underflow to zero. Use
Accordingly, PyTorch's nn.CrossEntropyLoss and F.cross_entropy expect raw, unnormalized logits. With class-index targets, the fused operation corresponds to log_softmax followed by NLL:
import torch
import torch.nn.functional as F
logits = torch.tensor([[2.0, -1.0, 0.5]], requires_grad=True)
target = torch.tensor([2])
loss_fused = F.cross_entropy(logits, target)
loss_manual = F.nll_loss(F.log_softmax(logits, dim=-1), target)
assert torch.allclose(loss_fused, loss_manual)
loss_fused.backward()
The fused implementation is not only faster; it preserves the correct numerical scale of the computation.
One API can define several statistical objectives
With label smoothing, the one-hot target is replaced, for example, by
where is a smoothing distribution, often uniform. The logit gradient remains , but the population optimum now corresponds to the smoothed conditional distribution. This is a deliberate regularizer rather than ordinary NLL on the original one-hot observations.
Class weights change the relative cost of errors. ignore_index, masking, and the reduction mode determine which elements enter the average and with what normalization. The name CrossEntropyLoss therefore does not fully specify the statistical risk: targets, weights, masks, and normalization matter.
Token perplexity is normally computed from an unsmoothed, unweighted mean NLL over exactly the target tokens included by the evaluation protocol.
3.7. Perplexity: returning from the logarithmic scale
NLL is convenient for addition and optimization, but its logarithmic scale is not always intuitive. Perplexity (PPL) exponentiates the average loss back to a multiplicative scale.
For observed targets,
and
If NLL is measured in nats,
while in bits,
The equivalent form is especially informative:
PPL is therefore the geometric mean reciprocal probability assigned to the observed targets.
A small example
Suppose the model assigns the observed tokens probabilities
The mean NLL in bits is
so
The same result follows from the geometric mean:
The phrase “effective number of equally likely choices” is exact when the model really is uniform over the same number of outcomes at every step. For a general distribution it is a useful scale, not a literal count of candidates.
PPL and predictive entropy answer different questions
The model's predictive entropy at context is
It measures how spread out the model's own distribution is. NLL and PPL look at the probability of the realized outcome.
A confident model can therefore have very low predictive entropy and enormous PPL if it is confident in the wrong answer. Conversely, an objectively ambiguous context can have high entropy and good expected NLL when probability mass is allocated correctly across valid continuations.

At the population level, the decomposition from Section 3.5 gives
with equality for an ideal conditional predictor. The minimum inside a restricted family may be higher. A finite validation estimate is random and can occasionally fall below its expectation, so a single validation number or extrapolated scaling-law asymptote should not automatically be called the “true entropy of language.”
Comparing perplexities
PPL depends on what counts as a prediction event. Different tokenizers split the same string into different numbers of tokens, so token-level perplexities cannot be compared directly across tokenizers.
A sound comparison requires at least:
- the same corpus and preprocessing;
- the same tokenizer;
- the same context length and context policy;
- the same target mask and normalization.
Bits per byte or per character provide a common physical unit and improve comparability, although corpus composition and the context protocol remain part of the experiment. Module 6 will make this connection literal: a probabilistic model becomes a compressor, and NLL becomes a measurable file length.
3.8. Mathematical deepening: why KL direction changes the answer
Optional on a first pass. For the main route, it is enough to know that maximum likelihood minimizes and that direction matters most when the model family cannot represent the target distribution exactly.
We now move from sums to densities:
In this section we call
forward KL from target to approximation , and
reverse KL. The terminology depends on which distribution is designated as the target, so the argument order is always more important than the label.
The two directions average in different places:
- forward KL takes an expectation under : every region containing data gets a vote;
- reverse KL takes an expectation under : the model pays mainly for regions where it places its own mass.
If all distributions are allowed and is exactly representable, both directions share the minimum . The difference becomes visible under model misspecification.
Canonical example: two peaks and one Gaussian
Let the target density be
while the model is restricted to a single Gaussian
Forward KL: both peaks must be explained
For fixed , minimizing is equivalent to minimizing
The optimal Gaussian matches the mean and variance of :
Thus
The model covers both modes and inevitably puts mass between them. In this family, this is the behavior commonly described as mode-covering or mean-seeking.
Reverse KL: selecting one safe region can be cheaper
For
there is no closed-form solution here. Numerical optimization gives two symmetric global minima near
Each fits one mode well and almost ignores the other: the canonical mode-seeking pattern.
The landscape is richer than the slogan. There is also a broader central local minimum near
but its reverse-KL value is higher. Initialization, parameterization, and the optimizer affect which solution is found.

When supports contain hard zeros, directionality can produce infinite penalties: forbids on -mass, while forbids -mass where . In the Gaussian example all densities are positive on the real line, so the difference appears even without hard zeros—through different expectation weights and the restricted family.
The slogan “forward covers, reverse selects a mode” is a strong and useful heuristic. It is not a universal theorem about every architecture, divergence, and optimization run.
3.9. A map of KL objectives in machine learning
Reference section. The same mathematics appears in several roles. In every example, first identify the first KL argument, the second argument, and the distribution over which contexts are sampled.
Maximum likelihood and language modeling
Ordinary data NLL minimizes
more precisely the mean conditional KL on data contexts. That is the exact conclusion of Section 3.5. Mode-covering intuition can help with restricted families, but the observed behavior of a large language model also depends on finite data, architecture, decoding, post-training, and the deployment distribution.
Variational inference
For a latent-variable model, the standard ELBO identity is
With the generative model fixed, maximizing the ELBO over minimizes reverse KL to the posterior. A restricted variational family may underrepresent some posterior modes.
Posterior collapse in a VAE is a different phenomenon. It commonly refers to a regime in which
so the latent variable carries little information about . Mode undercoverage and posterior collapse may occur in the same system, but they are not two names for the same effect.
Knowledge distillation
Classical soft-target distillation minimizes
which, for a fixed teacher, is equivalent to
The student receives not only the winning class but also relative probabilities among the other classes—the teacher's dark knowledge.
For an autoregressive model there is another choice: whose prefixes define the comparison. Training on a fixed set uses prefixes from data or teacher generations. Generalized Knowledge Distillation (GKD) adds on-policy sequences generated by the student and allows alternative divergences. This separates two design axes: the context distribution and the local discrepancy between teacher and student.
KL-regularized preference learning
An idealized objective for a prompt is
Reward pulls the policy toward preferred answers, while KL assigns a cost to moving away from the reference policy. The coefficient sets the exchange rate between those forces. Module 8 will solve this variational problem exactly and obtain an exponential tilt of the reference distribution.
Practical PPO also includes a policy-gradient objective, clipping, sampling, and approximate control of policy drift; it is not defined by one KL term alone. DPO is derived from the same KL-regularized setup but optimizes a pairwise preference-classification loss. Its link to KL lies in the underlying model and derivation rather than in an explicit KL penalty computed on every minibatch.
3.11. References and primary sources
- C. E. Shannon, A Mathematical Theory of Communication, Bell System Technical Journal, 1948.
- S. Kullback and R. A. Leibler, On Information and Sufficiency, Annals of Mathematical Statistics, 22(1):79–86, 1951.
- T. M. Cover and J. A. Thomas, Elements of Information Theory, 2nd ed., Wiley, 2006.
- T. Gneiting and A. E. Raftery, Strictly Proper Scoring Rules, Prediction, and Estimation, JASA, 102(477):359–378, 2007.
- D. M. Blei, A. Kucukelbir, and J. D. McAuliffe, Variational Inference: A Review for Statisticians, JASA, 2017.
- G. Hinton, O. Vinyals, and J. Dean, Distilling the Knowledge in a Neural Network, 2015.
- R. Agarwal et al., On-Policy Distillation of Language Models: Learning from Self-Generated Mistakes, 2023.
- L. Ouyang et al., Training Language Models to Follow Instructions with Human Feedback, 2022.
- R. Rafailov et al., Direct Preference Optimization: Your Language Model Is Secretly a Reward Model, 2023.
- J. He et al., Lagging Inference Networks and Posterior Collapse in Variational Autoencoders, 2019.
- PyTorch documentation:
torch.nn.CrossEntropyLossandtorch.nn.functional.cross_entropy.