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 = log2โeโ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=16โ61โlog2โ(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))=21โln(2ฯeฯ2)For ฯ2=0.1:H=21โln(2ฯeร0.1)=21โln(0.2ฯe)=21โln(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=โDlog2โpฮธโ(x)โ=D1โโ cross-entropyย inย bits(โlog2โpฮธโ(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โ;ฮฑหtโโx0โ,(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โ))โ2Dโln(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)=โiโp(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=1Nโlogpฮธโ(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