Lesson 3 of 5 · Information Theory

Cross-entropy

Cross-entropy measures the average number of bits needed to encode data from p with a code designed for q; it is the loss function of classification and language models.

Mathematics for deep learning Difficulty: Intermediate Study time: 45 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

If the data come from a true distribution \(p\) and we use a model distribution \(q\) to predict them, the cross-entropy \(H(p,q)\) is our average surprise.

Learning objectives

  • Computing the cross-entropy between two distributions
  • Simplifying it with one-hot labels
  • Linking it to Softmax and computing its gradient
  • Computing perplexity

2 Why study this topic?

When \(p\) is the one-hot distribution of the correct class, \(H(p,q) = -\log q_y\), which is exactly the classification loss. Its average over the tokens of a text is the training loss of language models, and its exponential is perplexity.

3 Theoretical explanation

Definition

\[H(p, q) = -\sum_x p(x)\log q(x)\]

It satisfies \(H(p,q) \ge H(p)\), and the difference between them is the KL divergence (next lesson).

With one-hot labels

If the correct class is y, \(p\) is 1 at y and 0 elsewhere, so a single term remains: \(H(p,q) = -\log q_y\).

With Softmax

If \(q = \operatorname{softmax}(\mathbf{z})\), the gradient of the loss with respect to the logits is very simple:

\[\frac{\partial \ell}{\partial z_k} = q_k - p_k\]

that is, "prediction minus truth", a gradient that does not saturate when the model is confidently wrong.

Perplexity

For language models: \(\mathrm{PPL} = \exp\big(\frac1T\sum_t -\ln q(w_t\mid w_{\lt t})\big)\), which can be read as the number of equally likely choices the model "hesitates" between on average.

4 Mathematical definitions

Cross-entropy
The average surprise of model q at data coming from p.
\[H(p,q) = -\sum_x p(x)\log q(x)\]
Perplexity
The exponential of the average cross-entropy per token in a language model.
\[\mathrm{PPL} = e^{\frac{1}{T}\sum_t -\ln q(w_t\mid w_{\lt t})}\]
Cross-entropy
The average surprise of the model distribution q on data coming from the true distribution p; it is the standard classification loss.
\[H(p,q)=-\sum_x p(x)\log q(x)\]
Perplexity
The exponential of the average cross-entropy per token, used to evaluate language models.
\[\mathrm{PPL}=\exp(\bar\ell)\]

5 The equations

\[H(p,q) = -\sum_x p(x)\log q(x)\]
Definition
\[\ell = -\log q_y\]
With a one-hot label
\[\frac{\partial\ell}{\partial z_k} = q_k - p_k\]
The gradient with respect to the Softmax logits
\[H(p,q) = H(p) + D_{KL}(p\,\|\,q)\]
The link to KL

6 Step-by-step worked example

A three-class classification loss

Problem: The correct class is the first, and the model gave \(q = (0.7, 0.2, 0.1)\). Compute the loss and its gradient with respect to the logits.

  1. \(p = (1, 0, 0)\), so \(\ell = -\ln 0.7 \approx 0.3567\) nats.
  2. The gradient is \(q - p = (-0.3, 0.2, 0.1)\).
  3. The first component is negative, so gradient descent raises the logit of the correct class and lowers the other two.

Result: \(\ell \approx 0.3567\) and the gradient is \((-0.3, 0.2, 0.1)\)

7 Practical application in AI

  • Image and text classification: Softmax with cross-entropy is the standard setup.
  • Language models: Training by minimizing the cross-entropy of the next token, and evaluating with perplexity.
  • Label smoothing: Replacing the one-hot label with a soft distribution (such as 0.9, 0.05 and 0.05) limits overconfidence.

8 Interactive lab linked to the lesson

Calculators for entropy, cross-entropy, KL divergence and information gain, and a visual comparison of two probability distributions.

Open the Information Theory Lab

9 Practice exercises

Review questions for reflection

  • Why do cross-entropy gradients with Softmax not saturate?
  • What is the perplexity of a model that gives each of 1000 tokens the same probability?

Common mistakes

  • Computing Softmax and then the logarithm separately, which causes overflow or \(\log 0\); a fused, numerically stable function is better.
  • Reversing the order of p and q.
  • Confusing cross-entropy with KL divergence.

Practice 5 questions from the question bank for this lesson

10 Lesson summary

  • \(H(p,q) = -\sum p\log q\).
  • With a one-hot label it becomes \(-\log q_y\).
  • Its gradient with Softmax is \(q - p\).

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.