Type at least 2 letters. Enter opens the first result, Esc closes.

    Information Theory · Lesson 4 · KL divergence, cross-entropy and compression

    Cross-entropy: the loss of classifiers and language models

    Intermediate 70 min 5 interactive figures 3 videos Colab notebook

    Before this lesson: lesson 01 (entropy), lesson 02 (conditional entropy) and lesson 03 (KL divergence, Gibbs' inequality). Maximum likelihood from the probability track; the softmax gradient also appears in calculus lesson 06. Logistic regression (ML fundamentals track, in preparation) can be read in parallel. Time: about 70 minutes, plus the notebook. This is a central lesson; take a break after section 3.4.


    Why this matters #

    You meet cross-entropy every day in machine learning.

    • Classifiers (images, text, tabular data) are trained with softmax cross-entropy.
    • Language models are trained to predict the next token with cross-entropy. Papers report the result as a loss in nats, as perplexity, or as bits per byte.
    • Image and audio generative models report bits per dimension, which is a cross-entropy.
    • Label smoothing and knowledge distillation change the target of the cross-entropy.
    • Scaling laws for language models have an irreducible term: the entropy part of the loss.

    When a loss is nan, when two perplexities cannot be compared, or when a training loss is "too good to be true", the explanation is usually in this lesson.

    As in the whole track, log⁡\log is the natural log (nats) and log⁡2\log_2 gives bits; PyTorch and NumPy losses are in nats.

    1. Predict first #

    2. The picture #

    Cross-entropy: average surprise under your model #

    Recall the coin of lesson 03. The coin is fair, p=(0.5,0.5)p = (0.5, 0.5), but the model says q=(0.8,0.2)q = (0.8, 0.2). Heads surprises the model by −log⁡20.8=0.322-\log_2 0.8 = 0.322 bits and tails by −log⁡20.2=2.322-\log_2 0.2 = 2.322 bits. Each side comes up half of the time, so the model's average surprise is

    H(p,q)=0.5⋅0.322+0.5⋅2.322=1.322 bits.H(p, q) = 0.5 \cdot 0.322 + 0.5 \cdot 2.322 = 1.322 \text{ bits}.

    This is the cross-entropy. It has two parts:

    1.322⏟H(p, q)=1⏟H(p)+0.322⏟KL(p ∥ q).\underbrace{1.322}_{H(p,\,q)} = \underbrace{1}_{H(p)} + \underbrace{0.322}_{\mathrm{KL}(p\,\|\,q)} .

    The entropy H(p)=1H(p) = 1 bit is there because the coin is random. The KL part, 0.3220.322 bits, is there because the model is wrong.

    Even a perfect model is surprised by a fair coin: 1 bit per flip. Only the second part depends on the model.

    Interactive figure: loading…

    Drag the orange bars (the model qq) against the fixed blue bars (the data pp). The stacked bar shows the cross-entropy H(p,q)H(p, q) as H(p)H(p) (blue) plus KL(p ∥ q)\mathrm{KL}(p\,\|\,q) (aqua). The blue part does not move when you change qq. The aqua part becomes 0 only when the orange bars match the blue bars, so H(p)H(p) is the floor. Change the data with the presets.

    One example at a time: the log-loss #

    A classifier sees an input xx and outputs probabilities qθ(y∣x)q_\theta(y \mid x) for every class yy. The true label is one class, y⋆y^\star. The log-loss (or negative log-likelihood, NLL) of this example is the surprise of the model at the true label:

    ℓ=−log⁡qθ(y⋆∣x).\ell = -\log q_\theta(y^\star \mid x).
    • The model gives the true class probability 1: ℓ=0\ell = 0.
    • Probability 0.5: ℓ=log⁡2=0.693\ell = \log 2 = 0.693 nats (1 bit).
    • Probability 0.01: ℓ=log⁡100=4.61\ell = \log 100 = 4.61 nats.
    • Probability →0\to 0: ℓ→∞\ell \to \infty. A confident mistake has no upper limit on its cost.
    Interactive figure: loading…

    Move the slider for the probability the model gives to the true class. The aqua curve is the log-loss −log⁡q-\log q; the grey dashed curve is the squared error (1−q)2(1 - q)^2; the light grey step is the 0–1 loss of the predicted class (two classes). The yellow line marks the current value. On the right, compare the gradients with respect to the logit: for a confident mistake, only the log-loss still pushes hard.

    Quick check

    A classifier gives the true class a probability of 0.01. What is the log-loss on this example?

    3. Definitions and main results #

    3.1 Cross-entropy #

    The cross-entropy of a distribution qq relative to a distribution pp is

    H(p,q)=−∑xp(x)log⁡q(x)=Ex∼p[−log⁡q(x)].H(\htmlClass{c1}{p}, \htmlClass{c2}{q}) = -\sum_x p(x) \log q(x) = \mathbb{E}_{x \sim p}\big[-\log q(x)\big].

    Read it as: the data come from pp, you predict with qq, and you pay −log⁡q(x)-\log q(x) for each outcome. The first argument is the data distribution, the second is the model. As for KL, H(p,q)=∞H(p, q) = \infty if q(x)=0q(x) = 0 for an outcome with p(x)>0p(x) > 0, and outcomes with p(x)=0p(x) = 0 cost nothing.

    Theorem 1 (cross-entropy = entropy + KL).

    H(p,q)=H(p)+KL(p ∥ q).H(p, q) = H(p) + \mathrm{KL}(p\,\|\,q).

    So H(p,q)≥H(p)H(p, q) \ge H(p), with equality if and only if q=pq = p.

    Proof.

    1. For every xx with p(x)>0p(x) > 0: −log⁡q(x)=−log⁡p(x)+log⁡p(x)q(x)-\log q(x) = -\log p(x) + \log \frac{p(x)}{q(x)} (add and subtract log⁡p(x)\log p(x)).
    2. Take the average under pp. The first term gives H(p)H(p), the second gives KL(p ∥ q)\mathrm{KL}(p\,\|\,q).
    3. Gibbs' inequality (lesson 03) says KL(p ∥ q)≥0\mathrm{KL}(p\,\|\,q) \ge 0, with equality only for q=pq = p. ■\blacksquare

    Log-loss is a strictly proper scoring rule. A scoring rule gives a loss S(q,y)S(q, y) to a predicted distribution qq when the outcome is yy. It is strictly proper if the expected loss Ey∼p[S(q,y)]\mathbb{E}_{y \sim p}[S(q, y)] is smallest only at q=pq = p. For the log-loss, the expected loss is H(p,q)H(p, q), and Theorem 1 shows that its only minimizer is q=pq = p. So a model trained with log-loss has a reason to report its true beliefs, not more confident or less confident ones.

    3.2 Minimizing the log-loss is maximum likelihood #

    Take a data set x1,…,xNx_1, \dots, x_N (discrete values) and a model qθq_\theta. The empirical distribution p^\hat p puts mass 1/N1/N on each data point: p^(x)=1N#{i:xi=x}\hat p(x) = \frac{1}{N}\#\{i : x_i = x\}.

    Theorem 2 (log-loss = NLL = forward KL).

    1N∑i=1N−log⁡qθ(xi)=H(p^,qθ)=H(p^)+KL(p^ ∥ qθ).\begin{aligned} \frac{1}{N}\sum_{i=1}^N -\log q_\theta(x_i) &= H(\hat p, q_\theta) \\ &= H(\hat p) + \mathrm{KL}(\hat p\,\|\,q_\theta). \end{aligned}

    So the three problems "minimize the average log-loss", "maximize the likelihood ∏iqθ(xi)\prod_i q_\theta(x_i)" and "minimize KL(p^ ∥ qθ)\mathrm{KL}(\hat p\,\|\,q_\theta)" have the same solutions.

    Proof.

    1. Group equal data points. The value xx appears Np^(x)N \hat p(x) times, so the sum over the data points equals a sum over the values: ∑i−log⁡qθ(xi)=∑xNp^(x) (−log⁡qθ(x))\sum_i -\log q_\theta(x_i) = \sum_x N\hat p(x)\,(-\log q_\theta(x)). Divide by NN: the average log-loss is ∑xp^(x)(−log⁡qθ(x))=H(p^,qθ)\sum_x \hat p(x)(-\log q_\theta(x)) = H(\hat p, q_\theta).
    2. Theorem 1 with p=p^p = \hat p gives H(p^,qθ)=H(p^)+KL(p^ ∥ qθ)H(\hat p, q_\theta) = H(\hat p) + \mathrm{KL}(\hat p\,\|\,q_\theta).
    3. H(p^)H(\hat p) depends on the data only, not on θ\theta. So the minimizers of the average log-loss and of KL(p^ ∥ qθ)\mathrm{KL}(\hat p\,\|\,q_\theta) are the same.
    4. 1N∑i−log⁡qθ(xi)=−1Nlog⁡∏iqθ(xi)\frac1N \sum_i -\log q_\theta(x_i) = -\frac1N \log \prod_i q_\theta(x_i), and −1Nlog⁡-\frac1N \log is decreasing. So minimizing the average log-loss is maximizing the likelihood. ■\blacksquare

    As N→∞N \to \infty, the average log-loss tends to H(pdata,qθ)=H(pdata)+KL(pdata ∥ qθ)H(p_{\text{data}}, q_\theta) = H(p_{\text{data}}) + \mathrm{KL}(p_{\text{data}}\,\|\,q_\theta). The KL has the data first and the model second: this is the forward KL. The other direction, KL(qθ ∥ p)\mathrm{KL}(q_\theta\,\|\,p) (the reverse KL), is what variational inference minimizes, and it behaves very differently (lesson 06).

    Quick check

    You train a model qθq_\theta by minimizing the average cross-entropy on samples from pdatap_{\text{data}}. Which divergence are you minimizing?

    3.3 Classification: softmax and its gradient #

    A classifier with KK classes computes a vector of logits z=(z1,…,zK)z = (z_1, \dots, z_K) (real numbers) and turns them into probabilities with the softmax

    qk=softmax⁡(z)k=ezk∑j=1Kezj.q_k = \operatorname{softmax}(z)_k = \frac{e^{z_k}}{\sum_{j=1}^K e^{z_j}} .

    Write the true label as a one-hot vector yy: yk=1y_k = 1 for the true class k=ck = c, and 0 for the others. The loss is the cross-entropy between yy and qq:

    ℓ(z)=H(y,q)=−∑kyklog⁡qk=−log⁡qc=−zc+log⁡∑jezj.\begin{aligned} \ell(z) = H(y, q) &= -\sum_k y_k \log q_k = -\log q_c \\ &= -z_c + \log \sum_{j} e^{z_j} . \end{aligned}

    The last form is how libraries compute it. The function LSE⁡(z)=log⁡∑jezj\operatorname{LSE}(z) = \log \sum_j e^{z_j} (log-sum-exp) is evaluated as m+log⁡∑jezj−mm + \log \sum_j e^{z_j - m} with m=max⁡jzjm = \max_j z_j, so no exponential overflows.

    Theorem 3 (gradient of softmax cross-entropy). ∂ℓ∂zk=qk−yk\dfrac{\partial \ell}{\partial z_k} = q_k - y_k, that is, ∇zℓ=q−y\nabla_z \ell = q - y: prediction minus target.

    So the gradient on the true logit is qc−1<0q_c - 1 < 0 (raise it), and on every other logit it is qk>0q_k > 0 (lower it). The entries add up to ∑kqk−∑kyk=1−1=0\sum_k q_k - \sum_k y_k = 1 - 1 = 0.

    Proof of Theorem 3
    1. ℓ(z)=−zc+LSE⁡(z)\ell(z) = -z_c + \operatorname{LSE}(z).
    2. The derivative of the first term: ∂∂zk(−zc)=−1\frac{\partial}{\partial z_k}(-z_c) = -1 if k=ck = c and 0 otherwise, that is, −yk-y_k.
    3. The derivative of log-sum-exp: ∂∂zklog⁡∑jezj=ezk∑jezj=qk\frac{\partial}{\partial z_k} \log \sum_j e^{z_j} = \frac{e^{z_k}}{\sum_j e^{z_j}} = q_k (chain rule: the derivative of log⁡s\log s is 1/s1/s, and ∂s/∂zk=ezk\partial s / \partial z_k = e^{z_k}).
    4. Add: ∂ℓ∂zk=qk−yk\frac{\partial \ell}{\partial z_k} = q_k - y_k. ■\blacksquare

    The same proof works for any target distribution tt (not only one-hot) with ℓ=H(t,q)\ell = H(t, q): ∇zℓ=q−t\nabla_z \ell = q - t, because ∑ktk=1\sum_k t_k = 1. Calculus lesson 06 derives the batch version and the Hessian.

    Binary case. With two classes, a model often outputs one logit zz and q=σ(z)=1/(1+e−z)q = \sigma(z) = 1/(1 + e^{-z}), the probability of class 1. The loss with label y∈{0,1}y \in \{0, 1\} is the binary cross-entropy (BCE)

    ℓ=−ylog⁡σ(z)−(1−y)log⁡(1−σ(z)),\ell = -y \log \sigma(z) - (1 - y)\log(1 - \sigma(z)),

    and its gradient is ∂ℓ∂z=σ(z)−y\dfrac{\partial \ell}{\partial z} = \sigma(z) - y.

    This is softmax cross-entropy with the two logits (z,0)(z, 0), so the gradient is again prediction minus target.

    Interactive figure: loading…

    Move the logit sliders and choose the true class. The orange bars are the softmax probabilities qq; the blue outline is the one-hot target yy; the dark arrows are the gradient q−yq - y on each logit. The arrow on the true logit points down (the gradient is negative, so gradient descent raises that logit); the others point up. The arrows always add up to 0. Raise the temperature TT to see a flatter qq and a smaller gradient.

    Quick check

    The softmax gives q=(0.665,0.245,0.090)q = (0.665, 0.245, 0.090) and the true class is class 1. What is the gradient of the loss with respect to the first logit z1z_1?

    3.4 The floor: the entropy of the labels #

    A classifier is trained on pairs (x,y)(x, y) from a data distribution p(x,y)p(x, y). Its expected loss is E(x,y)∼p[−log⁡qθ(y∣x)]\mathbb{E}_{(x, y) \sim p}\big[-\log q_\theta(y \mid x)\big].

    Theorem 4 (the floor).

    E(x,y)[−log⁡qθ(y∣x)]=H(Y∣X)+Ex[KL(p(⋅∣x) ∥ qθ(⋅∣x))].\begin{aligned} \mathbb{E}_{(x, y)}\big[-\log q_\theta(y \mid x)\big] &= H(Y \mid X) \\ &\quad + \mathbb{E}_{x}\Big[\mathrm{KL}\big(p(\cdot \mid x)\,\|\,q_\theta(\cdot \mid x)\big)\Big]. \end{aligned}

    Proof.

    1. Write the expectation as an outer average over xx and an inner average over yy given xx. The inner average is ∑yp(y∣x)(−log⁡qθ(y∣x))\sum_y p(y \mid x)\big(-\log q_\theta(y \mid x)\big), which is the cross-entropy H(p(⋅∣x),qθ(⋅∣x))H\big(p(\cdot \mid x), q_\theta(\cdot \mid x)\big).
    2. Apply Theorem 1 inside, for each xx: H(p(⋅∣x))+KL(p(⋅∣x) ∥ qθ(⋅∣x))H(p(\cdot \mid x)) + \mathrm{KL}(p(\cdot \mid x)\,\|\,q_\theta(\cdot \mid x)).
    3. The average over xx of H(p(⋅∣x))H(p(\cdot \mid x)) is the conditional entropy H(Y∣X)H(Y \mid X) (lesson 02). ■\blacksquare

    Only the KL part depends on the model. No model can get below H(Y∣X)H(Y \mid X), the uncertainty of the label that remains when you know the input.

    Example: label noise. The clean label is a function of xx, but each label is flipped with probability 0.1. For every xx, p(⋅∣x)p(\cdot \mid x) is a coin with probabilities (0.9,0.1)(0.9, 0.1), so H(Y∣X)=Hb(0.1)=0.469H(Y \mid X) = H_b(0.1) = 0.469 bits =0.325= 0.325 nats. The best model predicts 0.9 for the clean class. A training loss far below 0.325 nats means the model has memorized the flipped labels; its test loss cannot go below 0.325 nats.

    For a language model, the floor is the entropy of the next token given the context. Natural text is not deterministic, so this floor is positive. The irreducible term in the scaling laws of language models plays this role (LLMs track, in preparation).

    3.5 Soft targets: label smoothing and distillation #

    The target does not have to be one-hot. For a target distribution tt, the loss is H(t,q)H(t, q), and by Theorem 1

    H(t,q)=H(t)⏟floor, fixed+KL(t ∥ q)⏟trainable,∇zH(t,q)=q−t.H(t, q) = \underbrace{H(t)}_{\text{floor, fixed}} + \underbrace{\mathrm{KL}(t\,\|\,q)}_{\text{trainable}}, \qquad \nabla_z H(t, q) = q - t .

    The minimum over qq is H(t)H(t), reached at q=tq = t.

    Label smoothing. Replace the one-hot target by t=(1−ε) y+εKt = (1 - \varepsilon)\,y + \frac{\varepsilon}{K} (every class gets ε/K\varepsilon/K extra). With ε=0.1\varepsilon = 0.1 and K=3K = 3: t=(0.933,0.033,0.033)t = (0.933, 0.033, 0.033). The model is never pushed towards qc=1q_c = 1, so its logits stay finite and its predictions are less overconfident. Label smoothing was used in Inception-v3 and in the original Transformer, both with ε=0.1\varepsilon = 0.1.

    Knowledge distillation. A large teacher network with logits zTz^{\text{T}} gives soft targets t=softmax⁡(zT/T)t = \operatorname{softmax}(z^{\text{T}} / T), where T≥1T \ge 1 is a temperature. A higher TT gives a flatter distribution and shows which wrong classes the teacher finds similar ("dark knowledge"). For teacher logits (3,1,0.2)(3, 1, 0.2): T=1T = 1 gives (0.836,0.113,0.051)(0.836, 0.113, 0.051), and T=5T = 5 gives (0.446,0.299,0.255)(0.446, 0.299, 0.255). The student is trained on KL(t ∥ qT)\mathrm{KL}(t\,\|\,q_T), where qTq_T is its own softmax at the same temperature, usually mixed with the normal loss on the true labels. Hinton et al. multiply this term by T2T^2, because its gradient shrinks like 1/T21/T^2.

    Cross-entropy or KL as the loss? H(t,q)H(t, q) and KL(t ∥ q)\mathrm{KL}(t\,\|\,q) differ by H(t)H(t), which does not depend on the student. So they have the same gradient and the same minimizer. The difference is only the reported number: the KL is 0 at the optimum, the cross-entropy is H(t)H(t).

    Interactive figure: loading…

    Choose a target: one-hot, label smoothing (slider ε\varepsilon) or a teacher at temperature TT. The blue bars are the target tt and the orange bars the student q=softmax⁡(s⋅(2,1,0))q = \operatorname{softmax}(s \cdot (2, 1, 0)). The stacked bar splits the loss into H(t)H(t) and KL(t ∥ q)\mathrm{KL}(t\,\|\,q). With a one-hot target, a larger ss always lowers the loss. With label smoothing, the loss is smallest at a moderate ss, and a very confident student (large ss) has a larger loss.

    Quick check

    You train with label smoothing (ε=0.1\varepsilon = 0.1, K=3K = 3). What is the smallest possible loss on one example?

    3.6 Why not squared error or accuracy? #

    Accuracy (or the 0–1 loss) is piecewise constant in the logits: its gradient is 0 almost everywhere, so gradient descent cannot use it. It is also not strictly proper: every qq with the right most likely class gets the same score, so it does not reward good probabilities.

    Squared error on probabilities (the Brier score, ∑k(qk−yk)2\sum_k (q_k - y_k)^2) is a strictly proper scoring rule too, so the issue is not its minimizer. The issue is the gradient through the sigmoid or softmax. For two classes with true label 1, ddz(1−σ(z))2=−2σ(z)(1−σ(z))2\frac{d}{dz}(1 - \sigma(z))^2 = -2\sigma(z)(1 - \sigma(z))^2. For a confident mistake, z=−5z = -5: σ(z)=0.0067\sigma(z) = 0.0067, the squared-error gradient is −0.013-0.013, but the log-loss gradient is σ(z)−1=−0.993\sigma(z) - 1 = -0.993. The log cancels the flat tail of the sigmoid, so the log-loss learns fast from exactly the examples that are most wrong. And the log-loss is the likelihood, so all of maximum-likelihood theory applies.

    Calibration. A model is calibrated if, among the inputs where it says "70%", about 70% are correct. Because the log-loss is proper, a model that minimizes the expected log-loss is calibrated. Large networks still end up overconfident on test data, because they overfit the training loss. Temperature scaling (dividing the logits by one number TT fitted on validation data) fixes most of it (ML fundamentals track, in preparation).

    3.7 Units: nats, bits, perplexity, bits per dimension #

    These are all the same quantity, an average NLL, in different units. Let ℓˉ\bar\ell be the average NLL per token (or per symbol) in nats.

    unit formula meaning
    bits per token ℓˉ/ln⁡2\bar\ell / \ln 2 the same loss with log⁡2\log_2
    perplexity PPL=eℓˉ=2ℓˉ/ln⁡2\mathrm{PPL} = e^{\bar\ell} = 2^{\bar\ell / \ln 2} effective number of equally likely choices
    bits per dimension NLLnats/(Dln⁡2)\text{NLL}_{\text{nats}} / (D \ln 2) NLL of one image (or sound) divided by its DD numbers
    bits per byte ℓˉ/(ln⁡2⋅bytes per token)\bar\ell / (\ln 2 \cdot \text{bytes per token}) does not depend on the tokenizer
    • Perplexity is the geometric mean of 1/q(xt)1/q(x_t) over the tokens: eℓˉ=(∏t=1n1q(xt))1/ne^{\bar\ell} = \big(\prod_{t=1}^n \frac{1}{q(x_t)}\big)^{1/n}. A model that is uniform over VV tokens has ℓˉ=log⁡V\bar\ell = \log V and PPL=V\mathrm{PPL} = V: it "chooses among VV tokens". By Theorem 1, the perplexity is at least eHe^{H}, where HH is the entropy per token of the text.
    • Bits per dimension (bpd): an image has D=height×width×channelsD = \text{height} \times \text{width} \times \text{channels} numbers. Dividing by DD lets you compare images of different sizes.
    • Bits per byte: a tokenizer with longer tokens has fewer tokens per text, so its loss per token is larger. Dividing the total bits by the number of raw bytes removes this effect.
    Interactive figure: loading…

    Move the loss slider and read the same number as bits, perplexity and bits per byte. The curve is the perplexity eℓˉe^{\bar\ell} on a log scale; the yellow dot is the current model. Try the preset "uniform over 50 257 tokens". Switch to the image model to convert an NLL per image into bits per dimension.

    Quick check

    A language model has a loss of 3 bits per token. What is its perplexity?

    Continuous models of discrete data: dequantization

    Images are integers, x∈{0,…,255}Dx \in \{0, \dots, 255\}^D, but flows and diffusion models use densities pp on RD\mathbb{R}^D. A density can put arbitrarily tall spikes on the integer points, so the likelihood of the raw integers can be made arbitrarily large and means nothing. The standard fix is uniform dequantization: train and evaluate on x+ux + u with u∼Uniform[0,1)Du \sim \mathrm{Uniform}[0, 1)^D.

    Theorem 5 (dequantization bound, Theis et al. 2016, §3.1). Define the discrete model P(x)=∫[0,1)Dp(x+u) duP(x) = \int_{[0,1)^D} p(x + u)\,du for integer points xx. Then ∑xP(x)≤1\sum_x P(x) \le 1, and

    Eu[log⁡p(x+u)]≤log⁡P(x).\mathbb{E}_{u}\big[\log p(x + u)\big] \le \log P(x).

    Proof.

    1. The unit cubes x+[0,1)Dx + [0, 1)^D do not overlap, so ∑xP(x)\sum_x P(x) is the mass of pp on their union, which is at most 1.
    2. The cube has volume 1, so P(x)=Eu[p(x+u)]P(x) = \mathbb{E}_u[p(x + u)]. Jensen for the concave log⁡\log (lesson 03): Eu[log⁡p(x+u)]≤log⁡Eu[p(x+u)]=log⁡P(x)\mathbb{E}_u[\log p(x + u)] \le \log \mathbb{E}_u[p(x + u)] = \log P(x). ■\blacksquare

    So the continuous NLL on dequantized data is an upper bound on −log⁡P(x)-\log P(x), the code length of a real discrete model: the reported bits per dimension are conservative.

    Rescaling. Models often work on y=(x+u)/256∈[0,1)Dy = (x + u)/256 \in [0, 1)^D. The density changes by the Jacobian: py(y)=256D p(x+u)p_y(y) = 256^D\,p(x + u). So −log⁡2p(x+u)=−log⁡2py(y)+8D-\log_2 p(x + u) = -\log_2 p_y(y) + 8D: add 8 bits per dimension to an NLL measured on the [0,1)[0, 1) scale. For data scaled by 1/a1/a, add log⁡2a\log_2 a bits per dimension.

    Answers to section 1. (1) −log⁡0.01=4.61-\log 0.01 = 4.61 nats. (2) 23=82^3 = 8. (3) No. The floor is H(Y∣X)=Hb(0.1)=0.325H(Y \mid X) = H_b(0.1) = 0.325 nats per example (section 3.4), even for a perfect model.

    4. Worked example #

    (a) Softmax cross-entropy from logits. Logits z=(2,1,0)z = (2, 1, 0), true class 1.

    1. Exponentials: e2=7.389e^2 = 7.389, e1=2.718e^1 = 2.718, e0=1e^0 = 1. Sum: 11.10711.107.
    2. Softmax: q=(7.389,2.718,1)/11.107q = (7.389, 2.718, 1)/11.107 =(0.665,0.245,0.090)= (0.665, 0.245, 0.090).
    3. Loss: −log⁡0.665=0.408-\log 0.665 = 0.408 nats =0.588= 0.588 bits. With log-sum-exp: −z1+log⁡11.107=−2+2.408=0.408-z_1 + \log 11.107 = -2 + 2.408 = 0.408. ✓
    4. Gradient: q−y=(0.665−1,0.245,0.090)q - y = (0.665 - 1, 0.245, 0.090) =(−0.335,0.245,0.090)= (-0.335, 0.245, 0.090). The entries add up to 0.
    5. If the true class were class 3, the loss would be −log⁡0.090=2.408-\log 0.090 = 2.408 nats: the same logits, a confident mistake, six times the loss.

    (b) Label smoothing. Same logits, ε=0.1\varepsilon = 0.1, K=3K = 3: t=(0.933,0.033,0.033)t = (0.933, 0.033, 0.033).

    1. Loss: H(t,q)=−(0.933log⁡0.665+0.033log⁡0.245+0.033log⁡0.090)H(t, q) = -(0.933 \log 0.665 + 0.033 \log 0.245 + 0.033 \log 0.090) =0.933⋅0.408+0.033⋅1.408+0.033⋅2.408=0.380+0.047+0.080=0.508= 0.933 \cdot 0.408 + 0.033 \cdot 1.408 + 0.033 \cdot 2.408 = 0.380 + 0.047 + 0.080 = 0.508 nats.
    2. Entropy of the target: H(t)=−0.933log⁡0.933−2⋅0.033log⁡0.033=0.064+0.227=0.291H(t) = -0.933 \log 0.933 - 2 \cdot 0.033 \log 0.033 = 0.064 + 0.227 = 0.291 nats.
    3. KL part: 0.508−0.291=0.2160.508 - 0.291 = 0.216 nats. So 0.508=0.291+0.2160.508 = 0.291 + 0.216, the split of Theorem 1.
    4. Gradient: q−t=(0.665−0.933, 0.245−0.033, 0.090−0.033)q - t = (0.665 - 0.933,\ 0.245 - 0.033,\ 0.090 - 0.033) =(−0.268,0.211,0.057)= (-0.268, 0.211, 0.057).

    (c) Distillation. Teacher logits (3,1,0.2)(3, 1, 0.2), student logits (2,1,0)(2, 1, 0).

    • T=1T = 1: t=(0.836,0.113,0.051)t = (0.836, 0.113, 0.051) and q=(0.665,0.245,0.090)q = (0.665, 0.245, 0.090). H(t,q)=0.622=H(t) 0.548+KL 0.075H(t, q) = 0.622 = H(t)\ 0.548 + \mathrm{KL}\ 0.075 nats.
    • T=5T = 5: t=(0.446,0.299,0.255)t = (0.446, 0.299, 0.255) and q5=softmax⁡((2,1,0)/5)q_5 = \operatorname{softmax}((2, 1, 0)/5) =(0.402,0.329,0.269)= (0.402, 0.329, 0.269). KL(t ∥ q5)=0.0042\mathrm{KL}(t\,\|\,q_5) = 0.0042 nats; times T2=25T^2 = 25 gives 0.1040.104.

    (d) Units. A language model has ℓˉ=2.5\bar\ell = 2.5 nats per token.

    • Bits per token: 2.5/0.693=3.612.5 / 0.693 = 3.61. Perplexity: e2.5=12.2e^{2.5} = 12.2.
    • Model A: 3.0 nats per token, 4.0 bytes per token. Model B: 2.0 nats per token, 2.5 bytes per token. Perplexity: A e3=20.1e^3 = 20.1, B e2=7.39e^2 = 7.39. Bits per byte: A 3.0/(4.0⋅0.693)=1.083.0/(4.0 \cdot 0.693) = 1.08, B 2.0/(2.5⋅0.693)=1.152.0/(2.5 \cdot 0.693) = 1.15. B has the lower perplexity, but A predicts the text better.
    • An image model has an NLL of 25 000 nats on one 64×64×364 \times 64 \times 3 image, so D=12 288D = 12\,288 and bpd=25 000/(12 288⋅0.693)=2.94\text{bpd} = 25\,000/(12\,288 \cdot 0.693) = 2.94.

    5. In code #

    NumPy and SciPy check the numbers above. The notebook builds these functions step by step.

    import numpy as np
    from scipy.special import logsumexp, softmax
    from scipy.stats import entropy
    
    # (a) softmax cross-entropy from logits, the stable way
    z, c = np.array([2.0, 1.0, 0.0]), 0                # true class 1 is index 0
    loss = logsumexp(z) - z[c]
    q = softmax(z)
    y = np.eye(3)[c]
    print(q, loss, loss / np.log(2))                    # [0.6652 0.2447 0.09  ] 0.4076 0.5881
    print(q - y, np.isclose((q - y).sum(), 0))          # [-0.3348  0.2447  0.09  ] True
    
    # H(p, q) = H(p) + KL(p || q) for the coin, in bits
    p, qc = np.array([0.5, 0.5]), np.array([0.8, 0.2])
    print(-(p * np.log2(qc)).sum(), entropy(p, base=2) + entropy(p, qc, base=2))   # 1.3219 1.3219
    
    # (b) label smoothing: loss = H(t) + KL(t || q)
    t = 0.9 * y + 0.1 / 3
    print(-(t * np.log(q)).sum(), entropy(t), entropy(t, q))   # 0.5076 0.2911 0.2165
    
    # (d) units
    nll = 2.5
    print(nll / np.log(2), np.exp(nll))                          # 3.6067 12.1825
    print(np.log(50257), np.exp(np.log(50257)))                  # 10.8249 50257.0 (uniform model)
    print(25_000 / (64 * 64 * 3 * np.log(2)))                    # 2.9352 bits per dimension
    

    In PyTorch, F.cross_entropy takes logits (not probabilities) and class indices or soft targets. F.kl_div(input, target) computes KL(target ∥ ⋅)\mathrm{KL}(\text{target}\,\|\,\cdot) and expects input as log-probabilities: the model goes first, the target second, the opposite of the math notation.

    import torch, torch.nn.functional as F
    
    z = torch.tensor([[2.0, 1.0, 0.0]])
    print(F.cross_entropy(z, torch.tensor([0])).item())                       # 0.4076
    print(F.cross_entropy(z, torch.tensor([0]), label_smoothing=0.1).item())  # 0.5076
    p, q = torch.tensor([[0.5, 0.5]]), torch.tensor([[0.8, 0.2]])
    print(F.kl_div(q.log(), p, reduction="batchmean").item())                 # 0.2231 = KL(p || q)
    

    Never compute torch.log(torch.softmax(z)): for large logits the softmax underflows to 0 and the log gives -inf. Use F.log_softmax or F.cross_entropy, which use log-sum-exp.

    6. Where it shows up #

    • Image and text classifiers. Softmax cross-entropy on one-hot labels, sometimes with label smoothing (deep learning lesson 06). Binary and multi-label problems use the sigmoid with BCE on each output.
    • Language models. Pretraining minimizes the average next-token cross-entropy. By the chain rule (lesson 02), the sum of per-token conditional losses is the NLL of the whole sequence. Papers report the loss in nats, perplexity, or bits per byte (LLMs track, in preparation).
    • Scaling laws. The fitted loss curve L(N,D)=E+A/Nα+B/DβL(N, D) = E + A/N^\alpha + B/D^\beta has an irreducible term EE: the entropy part of the cross-entropy, which more parameters or data cannot remove.
    • Distillation. DistilBERT and many small language and vision models are trained on soft teacher outputs with a KL term at a temperature.
    • Generative models. Autoregressive image models, flows and diffusion models report bits per dimension on dequantized data (generative models track).
    • ML for science. The log score −log⁡q(y)-\log q(y) evaluates probabilistic forecasts (precipitation categories, earthquake magnitudes, extreme-event warnings). Because it is strictly proper, a forecaster cannot improve the expected score by hedging or by exaggerating its confidence.

    7. Common mistakes #

    • The arguments in the wrong order. H(p,q)H(p, q) averages under the data pp and takes the log of the model qq. H(q,p)H(q, p) is a different number.
    • log(softmax(z)) in code. It underflows to log⁡0=−∞\log 0 = -\infty. Use log_softmax or a loss that takes logits.
    • F.kl_div with probabilities or swapped arguments. It expects input = log-probabilities of the model and target = probabilities of the target. Wrong inputs give a wrong number and no error.
    • Gradient with respect to the probability. ∂ℓ/∂qc=−1/qc\partial \ell / \partial q_c = -1/q_c, but the gradient that training uses is with respect to the logits: q−yq - y.
    • Averaging perplexities. Perplexity is the exponential of the average loss. The average of per-document perplexities is larger: for losses 1 and 3 nats, e2=7.39e^{2} = 7.39, but 12(e1+e3)=11.4\frac12(e^1 + e^3) = 11.4 (Jensen, exe^x is convex).
    • Comparing perplexities across tokenizers. Use bits per byte.
    • Expecting a loss of 0. With noisy labels, label smoothing or ambiguous inputs the floor is H(Y∣X)>0H(Y \mid X) > 0 or H(t)>0H(t) > 0. A training loss below the floor means memorization.
    • Bits per dimension without the +8+8 after rescaling 8-bit data to [0,1)[0, 1), or on raw integers without dequantization.

    8. Check yourself #

    1. A binary classifier outputs σ(z)=0.8\sigma(z) = 0.8 for an example with label y=0y = 0. Compute the loss in nats and the gradient with respect to zz.
      Answerℓ=−log⁡(1−0.8)=−log⁡0.2=1.609\ell = -\log(1 - 0.8) = -\log 0.2 = 1.609 nats. ∂ℓ/∂z=σ(z)−y=0.8\partial \ell / \partial z = \sigma(z) - y = 0.8: positive, so gradient descent lowers zz.
    2. Show that the gradient of H(t,q)H(t, q) with respect to the logits is the same as the gradient of KL(t ∥ q)\mathrm{KL}(t\,\|\,q), for any fixed target tt.
      AnswerH(t,q)=H(t)+KL(t ∥ q)H(t, q) = H(t) + \mathrm{KL}(t\,\|\,q) (Theorem 1), and H(t)H(t) does not depend on the logits. So both have the gradient q−tq - t.
    3. The labels of a data set are flipped with probability 0.2. Your training loss is 0.30 nats and your test loss is 0.70 nats. What do you conclude?
      AnswerThe floor is Hb(0.2)=0.500H_b(0.2) = 0.500 nats. The training loss of 0.30 is below the floor, so the model has memorized some flipped labels. The test loss of 0.70 is 0.20 nats above the floor: that is the KL part, the room for improvement on new data.
    4. A language model is uniform over a vocabulary of V=32 000V = 32\,000 tokens. Give its loss in nats and in bits per token, and its perplexity.
      AnswerEach token has q=1/Vq = 1/V, so ℓˉ=log⁡32 000=10.37\bar\ell = \log 32\,000 = 10.37 nats =log⁡232 000=14.97= \log_2 32\,000 = 14.97 bits per token, and PPL=e10.37=32 000\mathrm{PPL} = e^{10.37} = 32\,000.
    5. A flow on 32×32×332 \times 32 \times 3 images, with data scaled to [0,1)[0, 1), reports log⁡py(y)=+8000\log p_y(y) = +8000 nats per image. What are the bits per dimension on the 8-bit scale? Is the positive log-density a bug?
      AnswerD=3072D = 3072. On the [0,1)[0, 1) scale, −8000/(3072⋅0.693)=−3.757-8000/(3072 \cdot 0.693) = -3.757 bits per dimension; add 8: 4.244.24 bits per dimension. Not a bug: a density on [0,1)D[0, 1)^D can be larger than 1, so its log can be positive. Only the corrected number bounds a code length.
    6. Label smoothing with ε=0.1\varepsilon = 0.1 on K=10K = 10 classes. What is the target for the true class and for each other class? What is the gradient on a wrong logit when the model predicts qk=0q_k = 0 for it?
      AnswerTrue class: 0.9+0.01=0.910.9 + 0.01 = 0.91; each other class: 0.010.01. Gradient qk−tk=0−0.01=−0.01q_k - t_k = 0 - 0.01 = -0.01: negative, so gradient descent *raises* that logit a little. The model is pushed away from total confidence.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. Define the cross-entropy H(p,q)H(p, q).

    2. How do cross-entropy, entropy and KL divergence relate?

    3. Why is minimizing the average cross-entropy on data the same as maximum likelihood?

    4. What is the gradient of softmax cross-entropy with respect to the logits?

    5. What is the lowest expected loss a classifier can reach?

    6. Compute the loss floor for binary labels flipped with probability 0.1.

    7. What is the label-smoothing target for the true class and the other classes?

    8. What does a knowledge-distillation loss minimize?

    9. How is perplexity computed from the average loss ℓˉ\bar\ell in nats?

    10. How do you compute bits per dimension from an NLL in nats?

    Open the review →

    9. Interview angle #

    • "Why cross-entropy and not MSE for classification?" It is the NLL of a categorical model, so training is maximum likelihood. With a softmax, its gradient is q−yq - y, which stays large for confident mistakes; squared error through a sigmoid has a gradient that vanishes there.
    • "Derive the gradient of softmax cross-entropy." ℓ=LSE⁡(z)−zc\ell = \operatorname{LSE}(z) - z_c, and ∂LSE⁡/∂zk=qk\partial \operatorname{LSE} / \partial z_k = q_k, so ∇zℓ=q−y\nabla_z \ell = q - y.
    • "What is perplexity? What is it for a uniform model over 50k tokens?" The exponential of the average NLL per token in nats, the effective number of choices; 50k.
    • "Your training loss is much lower than you expect from the label noise. What is happening?" The loss cannot go below H(Y∣X)H(Y \mid X) on new data; the model is memorizing noisy labels. Use early stopping, regularization or label smoothing.
    • "Cross-entropy or KL for distillation?" They differ by the entropy of the teacher's targets, which is a constant, so the gradients are the same.
    • "How do you compute cross-entropy without overflow?" From the logits with log-sum-exp: LSE⁡(z)−zc\operatorname{LSE}(z) - z_c with the max subtracted first.

    10. Practice #

    The notebook for this lesson runs in Colab. It has five exercises, each with a check cell and a folded solution:

    1. cross_entropy(p, q, base), and a check of H(p,q)=H(p)+KL(p ∥ q)H(p, q) = H(p) + \mathrm{KL}(p\,\|\,q).
    2. A numerically stable softmax cross-entropy from logits (log-sum-exp), checked against sklearn.metrics.log_loss, also for logits of size 1000.
    3. A gradient check of q−yq - y by finite differences.
    4. Logistic regression by gradient descent on a toy set whose labels are flipped with probability 0.1; plot the loss against the floor Hb(0.1)=0.325H_b(0.1) = 0.325 nats.
    5. perplexity(nll_nats) and bits_per_dim(nll_nats, D), with the uniform-model checks.

    11. Watch and read #

    Cross-entropy as the cost of coding with the wrong distribution, and why it is the loss of language models.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The softmax cross-entropy loss on a small network, computed by hand.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The derivative of the loss with respect to the logits, step by step; compare with Theorem 3.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play