Information Theory: Entropy, KL Divergence, Mutual Information, and Cross-Entropy
3955 words
20 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Information Theory: Entropy, KL Divergence, Mutual Information, and Cross-Entropy ## 🎯 Learning Objectives - Define entropy and interpret it as a measure of uncertainty - Derive and apply KL divergence for comparing probability distributions - Compute mutual information between random variables - Understand the r...

Information Theory: Entropy, KL Divergence, Mutual Information, and Cross-Entropy
🎯 Learning Objectives
- Define entropy and interpret it as a measure of uncertainty
- Derive and apply KL divergence for comparing probability distributions
- Compute mutual information between random variables
- Understand the relationship between cross-entropy, KL divergence, and log-likelihood
- Apply information-theoretic concepts to generative model analysis
- Explain how information theory connects to ELBO, GAN training, and diffusion models
📋 Prerequisites
- Probability basics: Random variables, PDF/PMF, expectation
- Generative Models Overview (Week 1): Likelihood, model distributions
- Basic calculus: Logarithms, differentiation
1. 📖 Core Content
1.1 Intuition: What is Information?
Information theory answers: "How much information does a random event convey?"
An event with low probability carries high information:
- "The sun rose this morning" — highly probable, low information
- "An earthquake hit Mumbai this morning" — low probability, high information An event with certainty carries zero information:
- "A fair coin will land either heads or tails" — 100% certain, 0 bits of information In generative modeling, information theory quantifies:
- How surprised is the model by new data? (cross-entropy)
- How different is the learned distribution from the real one? (KL divergence)
- How much does the input inform the output? (mutual information)
1.2 Self-Information
For an event x with probability p(x), the self-information is:
The base of the logarithm determines the unit:
- Base 2: bits (most common in information theory)
- Base e: nats (most common in ML, because gradients are easier)
- Base 10: dits (rare)
Worked Example 1: Computing Self-Information
Fair coin flip (p(heads) = 0.5):
Loaded coin (p(heads) = 0.95):
The tails outcome carries much more information because it's rare.
1.3 Entropy: Expected Information
Entropy is the average self-information of a random variable:
For continuous distributions (differential entropy):
1.3.1 Interpretation
Entropy measures uncertainty or unpredictability:
- High entropy → Many possible outcomes, roughly equally likely
- Low entropy → Outcome is fairly predictable
Worked Example 2: Comparing Entropies
Fair coin (p = 0.5):
Loaded coin (p = 0.95):
4-sided fair die:
Uniform over 256 values (like pixel intensities):
The uniform distribution has maximum entropy; any deviation from uniformity reduces entropy.
1.3.2 Maximum Entropy Principle
Among all distributions with a given mean and variance, the Gaussian distribution has maximum entropy. This is why:
- Gaussian noise models "maximum ignorance" — it's the least informative noise given first and second moments
- Diffusion models add Gaussian noise because it's the most entropic (most destructive) noise
1.4 KL Divergence: Measuring Distribution Difference
The Kullback-Leibler (KL) divergence measures how one probability distribution diverges from another:
1.4.1 Key Properties
- Non-negativity: DKL(p∥q)≥0 (Gibbs' inequality). Equality iff p=q almost everywhere.
- Not symmetric: DKL(p∥q)=DKL(q∥p) in general.
- Not a metric: Doesn't satisfy symmetry or triangle inequality.
1.4.2 Asymmetry in Practice
Because KL divergence is asymmetric, the direction matters:
- Forward KL DKL(p∥q): Average over p of log(p/q). This forces q>0 wherever p>0. Result: q tries to cover all modes of p (mean-seeking).
- Reverse KL DKL(q∥p): Average over q of log(q/p). This forces q to place probability mass only where p>0. Result: q tries to fit a single mode of p (mode-seeking).
Worked Example 3: KL Divergence Between Two Gaussians
For two univariate Gaussians p=N(μ1,σ12) and q=N(μ2,σ22):
Let p=N(0,1) and q=N(0.5,1.5):
Worked Example 4: Forward vs Reverse KL for a Bimodal Distribution
Let p be a bimodal distribution (two peaks: modes at -2 and +2). Let q be a single Gaussian N(μ,σ2).
Forward KL DKL(p∥q):
- We average over p, so q must have support everywhere p has mass
- q will try to cover both modes
- Optimal q: μ≈0, σ≈3 (high variance to cover both peaks, but puts probability mass between the modes where p has none) Reverse KL DKL(q∥p):
- We average over q, so q tries to avoid regions where p is very small
- Since p has a valley at 0, q will choose to model a single mode
- Optimal q: either μ≈−2, σ≈1 (left mode) or μ≈2, σ≈1 (right mode) This explains why:
- VAEs minimize reverse KL (q(z∣x) is optimized to avoid areas where p(z) has low density)
- Maximum likelihood estimation minimizes forward KL (pθ(x) must cover all modes of pdata)
1.5 Cross-Entropy
Cross-entropy measures the average number of bits needed to encode data from p using an optimal code designed for q:
Relationship to KL divergence:
Since H(p) is constant for a given data distribution, minimizing cross-entropy is equivalent to minimizing KL divergence.
1.5.1 Cross-Entropy Loss in ML
In supervised learning, the cross-entropy loss is:
where q(y∣x) is the model's predicted probability for the true class.
In generative modeling, the negative log-likelihood (NLL) is exactly the cross-entropy between the data distribution and the model distribution:
1.6 Mutual Information
Mutual information (MI) measures how much knowing one variable tells you about another:
Equivalently:
1.6.1 Interpretation
- I(X;Y) = 0: X and Y are independent (knowing X tells nothing about Y)
- I(X;Y) = H(X): Y perfectly predicts X (and vice versa)
- Higher MI → Stronger dependence between variables
Worked Example 5: Mutual Information for a Noisy Channel
Let X be the input to a noisy communication channel, and Y be the output:
- Input: X∈{0,1}, P(X=0)=P(X=1)=0.5
- Channel: transmits correctly with probability 0.9, flips with probability 0.1 Step 1: Compute H(X):
Step 2: Compute H(X∣Y):
Step 3: Mutual information:
The mutual information is 0.531 bits, which is less than the 1 bit of input entropy because the noisy channel loses some information.
1.7 Applications in Generative Models
1.7.1 ELBO Decomposition
The VAE's evidence lower bound can be written in terms of KL divergence:
The KL term pushes the approximate posterior qϕ(z∣x) toward the prior p(z), acting as a regularizer. This is reverse KL because we average over q.
1.7.2 InfoGAN: Maximizing Mutual Information
InfoGAN (Chen et al., 2016) adds a term to the GAN objective to encourage disentangled representations:
where c is a latent code and I(c;G(z,c)) is the mutual information between c and the generated image. By maximizing MI, InfoGAN ensures the latent code c has a meaningful effect on the output (e.g., c1 controls digit identity, c2 controls rotation).
In practice, the mutual information is approximated using a variational lower bound:
where q(c∣x) is a learned auxiliary distribution that approximates p(c∣x).
1.7.3 Diffusion Model ELBO
The diffusion model's variational bound can be expressed as:
At each timestep, the model learns to match the reverse conditional distribution. The KL between two Gaussians has a closed form, simplifying training.
1.8 Edge Cases & Gotchas
- KL divergence can be infinite: If q(x)=0 but p(x)>0, then DKL(p∥q)=∞. This is why VAEs use reverse KL (where q is the approximate posterior and we ensure it has broad support).
- Differential entropy can be negative: Unlike discrete entropy, continuous entropy can be negative (e.g., a very narrow Gaussian has low entropy).
- Mutual information is bounded: 0≤I(X;Y)≤min(H(X),H(Y)).
- Data processing inequality: Post-processing can't increase information. If X→Y→Z, then I(X;Z)≤I(X;Y).
1.9 Why This Matters
Information theory provides the theoretical foundation for nearly every generative model:
| Model | Key Information-Theoretic Concept |
|---|---|
| VAE | KL divergence (ELBO), reverse KL for posterior |
| GAN (InfoGAN) | Mutual information for disentanglement |
| Diffusion | KL between Gaussians, entropy of noise |
| Autoregressive | Cross-entropy (NLL) as training loss |
| Evaluation | Bits/dim (cross-entropy per dimension) |
| Normalizing Flows | Change of variables and entropy |
Understanding the information-theoretic roots of these losses helps you:
- Diagnose training failures (e.g., KL vanishing in VAEs)
- Design new model variants (e.g., β-VAE that weights KL)
- Choose appropriate evaluation metrics
2. 📐 Key Formulas / Concepts
| Concept | Formula | Interpretation |
|---|---|---|
| Entropy | H(p)=−∑p(x)logp(x) | Average uncertainty |
| Cross-entropy | H(p,q)=−∑p(x)logq(x) | Encoding cost using wrong distribution |
| KL divergence | $D_{KL}(p\ | q) = \sum p(x) \log\frac{p(x)}{q(x)}$ |
| MI | $I(X;Y) = H(X) - H(X\ | Y)$ |
| Relation | $H(p, q) = H(p) + D_{KL}(p\ | q)$ |
| Gaussian KL | $D_{KL}(\mathcal{N}_1 \ | \mathcal{N}_2) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2+(\mu_1-\mu_2)^2}{2\sigma_2^2} - \frac{1}{2}$ |
3. ⚠️ Common Pitfalls
Pitfall 1: Forgetting KL Divergence is Asymmetric
Mistake: Using DKL(p∥q) and DKL(q∥p) interchangeably.
Why: KL divergence is not symmetric. The forward KL (p∥q) averages over p; reverse KL (q∥p) averages over q. They have different optimization properties.
Correct approach: Always check which distribution you're averaging over. In the VAE ELBO, the KL term is DKL(qϕ(z∣x)∥p(z)) — reverse KL. In maximum likelihood estimation, we minimize DKL(pdata∥pθ) — forward KL.
Pitfall 2: Confusing Cross-Entropy with KL Divergence
Mistake: Saying "we minimize KL divergence" when actually minimizing cross-entropy.
Why: Cross-entropy loss H(p,q)=−∑plogq is commonly used. Since H(p,q)=H(p)+DKL(p∥q), minimizing cross-entropy is equivalent to minimizing KL divergence when H(p) is constant. But the numerical values differ.
Correct approach: When computing negative log-likelihood, you're computing cross-entropy, not KL divergence. The two differ by the (unknown) entropy of the data distribution, which is constant during optimization. Report NLL as cross-entropy, but understand it's equivalent to KL for model comparison.
Pitfall 3: Using Bits Instead of Nats
Mistake: Mixing bits and nats in the same formula without converting.
Why: log2 gives bits; ln (or loge) gives nats. Conversion: 1 nat = log2e≈1.44 bits.
Correct approach: Be consistent. In ML, nats are standard (natural gradient). In information theory papers, bits are common. When reporting results, specify which unit you're using.
Pitfall 4: Ignoring KL Vanishing in VAEs
Mistake: Not monitoring the KL term during VAE training.
Why: The KL term can "vanish" to zero, meaning the approximate posterior qϕ(z∣x) equals the prior p(z) for all x. This means the latent variable isn't being used — the model has collapsed to an unconditional generation model.
Correct approach: Monitor DKL(qϕ(z∣x)∥p(z)) during training. If it drops near zero, use KL annealing (gradually increase KL weight), β-VAE (increase β to encourage information-rich latent), or free bits (maintain minimum KL per latent dimension).
Pitfall 5: Misinterpreting Mutual Information for High-Dimensional Data
Mistake: Computing MI between two high-dimensional variables without regularization.
Why: In high-dimensional spaces, I(X;Y) is notoriously difficult to estimate because p(x,y) and p(x)p(y) require density estimation in high dimensions.
Correct approach: Use variational lower bounds (InfoNCE, MINE) for MI estimation, or use lower-dimensional representations (e.g., Inception features) for computing MI between images.
4. 📝 Practice Questions
Q1: Compute the entropy of a 6-sided fair die in bits.H=−∑i=1661log2(61)=log2(6)≈2.585 bitsThis means on average, it takes about 2.585 bits to encode the outcome of a fair 6-sided die. Q2: Two coins: Coin A is fair (p=0.5), Coin B is biased (p=0.99). Which has higher entropy and why?Coin A: H=−0.5log2(0.5)−0.5log2(0.5)=1.0 bitCoin B: H=−0.99log2(0.99)−0.01log2(0.01)≈0.081 bitsCoin A has higher entropy because its outcome is more uncertain. Coin B is highly predictable (almost always heads), so its entropy is near zero. Q3: Compute DKL(p∥q) where p is Bernoulli(0.5) and q is Bernoulli(0.8).DKL(p∥q)=∑x∈{0,1}p(x)logq(x)p(x)=0.5log0.80.5+0.5log0.20.5=0.5log(0.625)+0.5log(2.5)=0.5(−0.470)+0.5(0.916)=−0.235+0.458=0.223 natsIn bits: 0.223×log2(e)≈0.223×1.44≈0.321 bits Q4: In the VAE loss function, why does the KL term use DKL(qϕ(z∣x)∥p(z)) instead of DKL(p(z)∥qϕ(z∣x))?The ELBO derivation starts from:logp(x)=DKL(qϕ(z∣x)∥pθ(z∣x))+Lwhere L is the ELBO. We want to minimize the KL between qϕ(z∣x) and the true posterior pθ(z∣x), but we can't compute pθ(z∣x) directly. So we rearrange:L=Eqϕ(z∣x)[logpθ(x∣z)]−DKL(qϕ(z∣x)∥p(z))The KL term is DKL(qϕ(z∣x)∥p(z)) — we minimize this to make the approximate posterior close to the prior. This is reverse KL because we average over the variational distribution qϕ.Using reverse KL means qϕ(z∣x) tends to be mode-seeking — it will try to find a single good latent code for each x rather than covering all possible latent explanations. This is actually desirable: for a given input, we want a compact, informative latent representation.If we used forward KL DKL(p(z)∥qϕ(z∣x)) instead, the KL would be −∞ wherever qϕ assigns zero probability where p(z)>0, which would force qϕ to have very broad support for every input — undesirable. Q5: In InfoGAN, the mutual information I(c;G(z,c)) is maximized. Show how the variational lower bound I(c;x)≥Ex∼G(z,c)[logq(c∣x)]+H(c) is derived.Starting from the definition:I(c;x)=H(c)−H(c∣x)=H(c)+Ex∼G(z,c)[Ec′∼p(c∣x)[logp(c′∣x)]]We introduce a variational approximation q(c∣x) to the true posterior p(c∣x):I(c;x)=H(c)+Ex[Ec′∼p(c∣x)[logp(c′∣x)]]=H(c)+Ex[Ec′∼p(c∣x)[logq(c′∣x)]+DKL(p(c∣x)∥q(c∣x))]Since DKL≥0:I(c;x)≥H(c)+Ex∼G(z,c)[logq(c∣x)]This gives a tractable lower bound on mutual information that we can optimize. q(c∣x) is an auxiliary network that tries to predict the latent code c from the generated image x. Q6: A Gaussian distribution N(0,0.1) has differential entropy. Compute it.Differential entropy of a univariate Gaussian:H(N(μ,σ2))=21ln(2πeσ2)For σ2=0.1:H=21ln(2πe×0.1)=21ln(0.2πe)=21ln(1.708)≈21×0.536=0.268 natsThis is positive (low entropy for a narrow distribution). For a very narrow Gaussian (σ→0), differential entropy is −∞. Q7: Explain why bits/dim is equivalent to cross-entropy per dimension.Bits/dim measures the average number of bits needed to encode each dimension of the data under the model. For an image of D=H×W×C dimensions:bits/dim=−Dlog2pθ(x)=D1⋅cross-entropy in bits(−log2pθ(x))This is the cross-entropy between the true data distribution (which places a delta on x) and the model, normalized by dimensionality.Bits/dim allows comparison across models trained on the same dataset, regardless of image resolution. A bits/dim of 3.0 means the model compresses each pixel channel (or each data dimension) to 3 bits on average. Q8: X is uniformly distributed over {1,2,3,4}. Y=X2mod4. Compute I(X;Y).Step 1: H(X)=log2(4)=2 bitsStep 2: Y=X2mod4:
- X=1→Y=1
- X=2→Y=0
- X=3→Y=1
- X=4→Y=0
So P(Y=0)=0.5, P(Y=1)=0.5Step 3: H(Y)=1 bitStep 4: P(X∣Y=0): X∈{2,4} equally likely → H(X∣Y=0)=1 bit P(X∣Y=1): X∈{1,3} equally likely → H(X∣Y=1)=1 bitH(X∣Y)=0.5×1+0.5×1=1 bitStep 5: I(X;Y)=H(X)−H(X∣Y)=2−1=1 bitKnowing Y reduces the uncertainty about X from 2 bits to 1 bit — you still don't know whether X is the smaller or larger value in the pair. Q9: Why does adding noise increase entropy? Use this to explain the forward process in diffusion models.Adding noise increases entropy because it makes the distribution more uncertain. In the forward diffusion process:q(xt∣x0)=N(xt;αˉtx0,(1−αˉt)I)As t increases (more noise added):
- αˉt→0
- Mean goes to 0
- Variance goes to I
The entropy of q(xt∣x0) increases from near-zero (at t=0, xt is nearly deterministic given x0) to:H(q(xT))≈2Dln(2πe)which is the maximum entropy for a D-dimensional distribution with fixed variance — matching a standard Gaussian N(0,I).This increase in entropy is why diffusion models can destroy any data distribution: by gradually adding noise, we wash away the structure until only Gaussian noise remains. Q10: The β-VAE modifies the VAE loss to L=Eqϕ(z∣x)[logpθ(x∣z)]−β⋅DKL(qϕ(z∣x)∥p(z)). Explain why β>1 encourages more disentangled representations.With β>1, the KL term is weighted more heavily, which pushes each latent dimension's distribution qϕ(zi∣x) to be closer to the prior p(zi)=N(0,1).For disentanglement, this means:
- Factorized prior: The prior p(z)=∏ip(zi) is factorized (independent dimensions). By pushing qϕ(z∣x) toward p(z), we encourage the posterior to also be factorized.
- Information bottleneck: The higher KL penalty limits the total information that z can carry about x. The model must allocate this limited information efficiently, learning to encode only the most salient, independent factors of variation in separate dimensions.
- Selective pressure: Dimensions that aren't informative are "turned off" (their KL matches the prior exactly), leaving only the truly informative dimensions active.
However, β>1 also reduces reconstruction quality (the rate-distortion tradeoff). Higher β reduces "rate" (KL) but increases "distortion" (reconstruction error). Finding the right β balances disentanglement with reconstruction fidelity. Q11: Show that maximizing likelihood is equivalent to minimizing the forward KL divergence between the empirical data distribution and the model distribution.The empirical data distribution is:p^data(x)=N1∑i=1Nδ(x−x(i))where δ is the Dirac delta.Maximum likelihood estimation maximizes:N1∑i=1Nlogpθ(x(i))=Ex∼p^data[logpθ(x)]The forward KL divergence between p^data and pθ:DKL(p^data∥pθ)=Ex∼p^data[logp^data(x)−logpθ(x)]=constant (entropy of empirical dist)Ex∼p^data[logp^data(x)]−log-likelihoodEx∼p^data[logpθ(x)]Since the first term is constant with respect to θ, minimizing DKL is equivalent to maximizing the log-likelihood.This is forward KL because we average over p^data — the model must assign high probability to every data point, which forces it to cover all modes of the data distribution. Q12: For the KL divergence DKL(p∥q) where p is a mixture of two Gaussians and q is a single Gaussian, sketch the result under forward KL vs reverse KL optimization. Which one would produce a better generative model for image data?When p is bimodal (two modes) and q is a single Gaussian:Forward KL (DKL(p∥q) minimized over q):
- q spreads out to cover both modes
- Result: high-variance Gaussian centered between the modes
- q has mass in the middle where p has none
- This is "mean-seeking" — it captures the overall spread but not the structure
Reverse KL (DKL(q∥p) minimized over q):
- q picks one mode and fits it tightly
- Result: a narrow Gaussian centered on one mode
- The other mode is completely ignored
- This is "mode-seeking" or "mode-collapse"
For image data, forward KL is generally preferred because:
- Real images have many modes (cats, dogs, cars, etc.)
- Forward KL forces the model to cover all modes (better recall)
- Spread-out blurry images are better than missing entire categories
However, forward KL tends to produce blurry images (the model averages over plausible outputs). This is why VAEs (trained with forward KL through MLE) produce blurry images, while GANs (which use a different loss) can produce sharp images but may suffer from mode collapse (a reverse-KL-like failure mode).This also explains why diffusion models (trained with forward KL via NLL) can be slightly blurry with few steps but sharp with enough steps — the iterative refinement allows the model to escape the blurry mean prediction.
5. 🔗 Cross-References
- Previous: Evaluation Metrics (Week 8) — Cross-entropy as evaluation metric
- Next: Math Foundations (Week 9) — SVD, eigendecomposition for information theory
- Related: VAEs (Week 3) — ELBO, KL divergence in practice
- Related: Diffusion Models (Week 4) — KL between Gaussians in diffusion loss
- External: Cover & Thomas, "Elements of Information Theory" — Comprehensive reference Join Discord PreviousEvaluation MetricsNextMath Foundations