Cross-Entropy and KL Divergence
Cross-entropy measures how many bits a model needs to describe data really drawn from ; KL divergence is the excess over the minimum.
Needs a step first: 18
If outcomes come from but you encode them as if they came from , the average cost is the cross-entropy . It is never less than the entropy , and the gap is zero only when . For continuous distributions the sums become integrals of densities. Training a classifier or language model means minimising cross-entropy with the true labels, which is the same as maximising likelihood; KL divergence also appears in the loss of diffusion and other generative models.
Where it shows up: It is the training loss of logistic regression, neural network classifiers and language models, and KL terms appear in generative models.
How to hold it: In your head
See cross-entropy as the bits you pay for coding with the wrong model, KL divergence as the excess over the minimum, and why minimising it is maximum likelihood.
What knowing it looks like
- Compute the cross-entropy and KL divergence between two small distributions.
- Show that for a one-hot label the cross-entropy loss is of the probability given to the right answer.
- Explain why minimising cross-entropy is the same as maximum likelihood.
Test yourself
A fair coin has . A model predicts . Compute , and in bits. Use .
Show the worked answer
Needs a step first: 18
Learn it with an AI
Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.
Be my maths tutor. I'm learning "Cross-Entropy and KL Divergence" (Undergraduate II tier): Cross-entropy measures how many bits a model $q$ needs to describe data really drawn from $p$; KL divergence is the excess over the minimum.
I already know: Information and Entropy, Probability Densities.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: A fair coin has $p = (\tfrac12, \tfrac12)$. A model predicts $q = (\tfrac14, \tfrac34)$. Compute $H(p)$, $H(p, q)$ and $D_{\mathrm{KL}}(p\,\|\,q)$ in bits. Use $\log_2 3 \approx 1.585$.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.Needs first
- Information and EntropyEntropy measures the average surprise of a random outcome, in bits: the shortest average code length.Undergraduate I · Optimisation & Information
- Probability DensitiesProbability for quantities that vary continuously, described by a density whose area over an interval is the chance of landing there.Undergraduate I · Probability & Statistics
Leads to
- Training Deep NetworksThe practical craft that makes deep networks learn: sensible initialisation, normalisation, learning-rate schedules, minibatches and watching validation loss.Frontier · Machine Learning
- Softmax and Language ModelsSoftmax turns a list of scores into probabilities; a language model uses it to predict the next token, trained by cross-entropy.Frontier · Machine Learning
On the way to
Recommended
- Mathematics for Machine LearningDeisenroth, Faisal and Ong (Cambridge University Press) · BookA free book that gathers exactly the linear algebra, vector calculus, probability and optimisation machine learning uses, then applies them to regression, PCA and classification.
- Convex OptimizationStephen Boyd and Lieven Vandenberghe · BookThe standard text on convex sets, duality and Lagrange multipliers, free as a PDF from Stanford along with lecture slides and exercises.
- Information Theory, Inference, and Learning AlgorithmsDavid MacKay · BookMacKay's book connecting entropy, coding, Bayesian inference and neural networks, free to read online and still the one recommended for information theory with intuition.