ML Systems Lab Open interactive version →
Intermediate 30 min read entropyKL divergencecross-entropymutual information

Information Theory for ML

Entropy, cross-entropy, KL divergence, mutual information

You are building a compression system for text messages. The word "the" appears in 7% of positions; the word "zymurgy" appears in 0.0001% of positions. If you give both words the same-length code, you waste bits every time you encode "the." Efficient encoding gives short codes to common words and long codes to rare ones. But how short and how long, exactly?

The answer falls out of a simple observation: the minimum number of bits needed to encode an event with probability $p$ is $-log_2(p)$. A word appearing with probability 0.07 needs $-log_2(0.07) \approx 3.8$ bits. A word at probability 0.000001 needs about 20 bits. If all events were equally probable, the average bits needed would be $log_2(N)$ where $N$ is the number of possible events. In general, the average bits over the whole distribution is the entropy: $H = -\sum p(x) \log p(x)$. Entropy measures how uncertain the distribution is — how many bits you need on average to describe a draw from it.

Now suppose your compression algorithm was designed for the wrong distribution. You built it assuming word frequencies $q$, but the true frequencies are $p$. The average bits you actually spend is $H(p, q) = -\sum p(x) \log q(x)$ — cross-entropy. The extra bits you waste compared to an optimal encoder is $KL(p | q) = H(p, q) - H(p)$. Since $H(p)$ is fixed by the true distribution, minimising cross-entropy is identical to minimising KL divergence — the extra waste from using the wrong distribution.

This is where ML enters. Your model produces a predicted distribution $\hat{y}$. The true label is a one-hot distribution $y$. Cross-entropy loss $= -\log(\hat{y}_{correct})$ measures how many bits your model`s encoding wastes relative to optimal. Minimising cross-entropy is fitting your model`s distribution to the true data distribution.

NOT this. Most people think cross-entropy is just a loss function that happens to work well. Cross-entropy is an information-theoretic quantity measuring encoding efficiency. When the model is perfectly calibrated, cross-entropy equals entropy — no wasted bits. The loss function framing hides why cross-entropy is the *right* choice, not merely a convenient one. It also hides the direction of KL: forward KL ($KL(p | q)$, used in training) forces the model to cover all modes of the true distribution. Reverse KL ($KL(q | p)$) is mode-seeking. A VAE's ELBO decomposes as $log p(x) = ext{ELBO} + KL(q(z|x)|p(z|x))$, so maximising the ELBO implicitly minimises the reverse KL between the approximate and true posterior — that's what lets the approximate posterior concentrate on a subset of the true posterior's modes rather than spreading across all of them. (The ELBO's explicit $q(z|x)$-to-prior term is a separate regulariser toward $p(z)$, not the mechanism behind this mode-seeking behaviour.) In VAEs this shows up as *posterior collapse* (an uninformative latent code and blurry, averaged reconstructions), not literally a single repeated output — that failure mode is GAN-style mode collapse instead. These produce qualitatively different learned distributions — mode-covering versus mode-seeking.

Key points

Takeaway

Minimising cross-entropy is minimising KL divergence from the true distribution to your model. Cross-entropy is not an arbitrary loss — it is the exact measure of encoding waste, and that framing tells you why the KL direction matters.

Recap

Check your understanding

Q1. Compute H(X) for a fair six-sided die. Then compute H(Y) where Y = 'even or odd' (2 outcomes). Why is H(Y) < H(X)?

Q2. What is KL divergence D_KL(P||Q), and why is it asymmetric? Give an ML example where direction matters.

Q3. Mutual information I(X;Y) = 0. Which TWO of the following statements about this condition, and how it relates to Cov(X,Y) = 0, are true?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →