Information Theory
Cross-entropy is the default classification loss for a precise reason: it measures how many extra bits you waste describing reality with the wrong distribution, and minimising it means matching the truth. This page builds entropy, cross-entropy, and KL divergence from scratch so that reason stops being a slogan and becomes a derivation.
Cross-entropy is the average number of bits you waste by encoding reality with the wrong distribution; minimising it means matching the truth.

Information content of an event
An event that's certain to happen tells you nothing when it occurs; an event that's rare and surprising tells you a lot. Information content formalises "surprise":
A probability-1 event has bits of information; a probability-0.5 event (a fair coin flip) carries exactly 1 bit.
Entropy
Entropy is the expected information content — the average surprise, in bits, of a distribution:
A fair coin () has entropy bit — maximum uncertainty for a binary variable. A biased coin () has lower entropy (~0.47 bits) — you're less surprised on average because you can already guess the likely outcome.
Cross-entropy
Cross-entropy measures the average number of bits needed to encode data from true distribution using a code optimised for a different distribution :
If , cross-entropy equals entropy — the minimum possible. Any mismatch between and adds extra bits.
KL divergence
The Kullback-Leibler divergence measures exactly that extra cost — how much worse is than the true :
always, and equals zero only when . It is not symmetric — in general, because "the extra cost of using when the truth is " is a different question from "the extra cost of using when the truth is ."
The decomposition
Cross-entropy splits into an irreducible part (the entropy of the true distribution — you can never do better than this) and a reducible part (the KL divergence — the model's error). Training a classifier by minimising cross-entropy is equivalent to minimising KL divergence, because is fixed by the data and doesn't depend on the model at all.
| Symbol | Meaning |
|---|---|
| entropy of the true distribution | |
| cross-entropy between true and model | |
| KL divergence — how much worse is than |
Mutual information
Measures how much knowing reduces uncertainty about (and vice versa) — zero exactly when are independent.
Perplexity
Exponentiated cross-entropy, used throughout Language Modeling Basics. A perplexity of 20 means the model is, on average, as uncertain as if it were choosing uniformly among 20 equally likely options.
Where each shows up
- Cross-entropy: the default classification loss (Loss Functions).
- KL divergence: the regulariser in a VAE's ELBO (Variational Autoencoders), the constraint in TRPO/PPO (PPO and Trust Regions).
- Entropy: exploration bonuses in policy gradient methods, dropout's information-theoretic framing.
- Perplexity: language model evaluation.
Code: entropy, KL, and the asymmetry, from scratch
import numpy as np
def entropy(p):
p = np.asarray(p)
p = p[p > 0] # 0 * log(0) := 0
return -np.sum(p * np.log2(p))
def cross_entropy(p, q):
p, q = np.asarray(p), np.asarray(q)
mask = p > 0
return -np.sum(p[mask] * np.log2(q[mask]))
def kl_divergence(p, q):
return cross_entropy(p, q) - entropy(p)
p = np.array([0.7, 0.2, 0.1])
q = np.array([0.4, 0.3, 0.3])
print("H(p) =", entropy(p))
print("H(p,q) =", cross_entropy(p, q))
print("D_KL(p||q) =", kl_divergence(p, q))
print("D_KL(q||p) =", kl_divergence(q, p), " <- different from D_KL(p||q), confirming asymmetry")
# --- Cross-entropy loss on a batch of softmax outputs ---
def softmax(logits):
exp = np.exp(logits - logits.max(axis=-1, keepdims=True))
return exp / exp.sum(axis=-1, keepdims=True)
logits = np.array([[2.0, 1.0, 0.1], [0.5, 2.5, 0.3]])
true_labels = np.array([0, 1]) # class indices
probs = softmax(logits)
batch_ce = -np.log(probs[np.arange(len(true_labels)), true_labels]).mean()
print("batch cross-entropy loss:", batch_ce)
See also
- Loss Functions — cross-entropy as a trainable objective.
- Statistics and Estimation — the MLE connection that makes cross-entropy the "correct" loss under a categorical noise model.