Information Theory: Entropy, KL Divergence, Mutual Information, and Cross-Entropy

3955 words
20 min read
Python Week 1: the first filter for runtime behavior
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 xx with probability p(x)p(x), the self-information is:
I(x)=โˆ’logโกp(x)I(x) = -\log p(x)
The base of the logarithm determines the unit:
  • Base 2: bits (most common in information theory)
  • Base ee: 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):
I(heads)=โˆ’logโก2(0.5)=โˆ’logโก2(2โˆ’1)=1ย bitI(\text{heads}) = -\log_2(0.5) = -\log_2(2^{-1}) = 1 \text{ bit}
Loaded coin (p(heads) = 0.95):
I(heads)=โˆ’logโก2(0.95)โ‰ˆ0.07ย bitsI(\text{heads}) = -\log_2(0.95) \approx 0.07 \text{ bits} I(tails)=โˆ’logโก2(0.05)โ‰ˆ4.32ย bitsI(\text{tails}) = -\log_2(0.05) \approx 4.32 \text{ bits}
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:
H(p)=Exโˆผp[โˆ’logโกp(x)]=โˆ’โˆ‘xp(x)logโกp(x)H(p) = \mathbb{E}_{x \sim p}[-\log p(x)] = -\sum_{x} p(x) \log p(x)
For continuous distributions (differential entropy):
H(p)=โˆ’โˆซp(x)logโกp(x)dxH(p) = -\int p(x) \log p(x) dx

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):
H=โˆ’0.5logโก2(0.5)โˆ’0.5logโก2(0.5)=1ย bitH = -0.5\log_2(0.5) - 0.5\log_2(0.5) = 1 \text{ bit}
Loaded coin (p = 0.95):
H=โˆ’0.95logโก2(0.95)โˆ’0.05logโก2(0.05)โ‰ˆ0.286ย bitsH = -0.95\log_2(0.95) - 0.05\log_2(0.05) \approx 0.286 \text{ bits}
4-sided fair die:
H=โˆ’4ร—14logโก2(14)=2ย bitsH = -4 \times \frac{1}{4}\log_2(\frac{1}{4}) = 2 \text{ bits}
Uniform over 256 values (like pixel intensities):
H=โˆ’โˆ‘i=02551256logโก2(1256)=logโก2(256)=8ย bitsH = -\sum_{i=0}^{255} \frac{1}{256}\log_2(\frac{1}{256}) = \log_2(256) = 8 \text{ bits}
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:
DKL(pโˆฅq)=Exโˆผp[logโกp(x)q(x)]=โˆ‘xp(x)logโกp(x)q(x)D_{KL}(p \| q) = \mathbb{E}_{x \sim p}\left[\log \frac{p(x)}{q(x)}\right] = \sum_x p(x) \log \frac{p(x)}{q(x)}

1.4.1 Key Properties

  1. Non-negativity: DKL(pโˆฅq)โ‰ฅ0D_{KL}(p \| q) \geq 0 (Gibbs' inequality). Equality iff p=qp = q almost everywhere.
  2. Not symmetric: DKL(pโˆฅq)โ‰ DKL(qโˆฅp)D_{KL}(p \| q) \neq D_{KL}(q \| p) in general.
  3. 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)D_{KL}(p \| q): Average over pp of logโก(p/q)\log(p/q). This forces q>0q > 0 wherever p>0p > 0. Result: qq tries to cover all modes of pp (mean-seeking).
  • Reverse KL DKL(qโˆฅp)D_{KL}(q \| p): Average over qq of logโก(q/p)\log(q/p). This forces qq to place probability mass only where p>0p > 0. Result: qq tries to fit a single mode of pp (mode-seeking).

Worked Example 3: KL Divergence Between Two Gaussians

For two univariate Gaussians p=N(ฮผ1,ฯƒ12)p = \mathcal{N}(\mu_1, \sigma_1^2) and q=N(ฮผ2,ฯƒ22)q = \mathcal{N}(\mu_2, \sigma_2^2):
DKL(pโˆฅq)=logโกฯƒ2ฯƒ1+ฯƒ12+(ฮผ1โˆ’ฮผ2)22ฯƒ22โˆ’12D_{KL}(p \| q) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac{1}{2}
Let p=N(0,1)p = \mathcal{N}(0, 1) and q=N(0.5,1.5)q = \mathcal{N}(0.5, 1.5):
DKL(pโˆฅq)=logโก1.51+1+(0โˆ’0.5)22ร—1.52โˆ’0.5=logโก(1.5)+1+0.254.5โˆ’0.5D_{KL}(p \| q) = \log\frac{1.5}{1} + \frac{1 + (0 - 0.5)^2}{2 \times 1.5^2} - 0.5 = \log(1.5) + \frac{1 + 0.25}{4.5} - 0.5 =0.405+0.278โˆ’0.5=0.183ย nats= 0.405 + 0.278 - 0.5 = 0.183 \text{ nats}

Worked Example 4: Forward vs Reverse KL for a Bimodal Distribution

Let pp be a bimodal distribution (two peaks: modes at -2 and +2). Let qq be a single Gaussian N(ฮผ,ฯƒ2)\mathcal{N}(\mu, \sigma^2). Forward KL DKL(pโˆฅq)D_{KL}(p \| q):
  • We average over pp, so qq must have support everywhere pp has mass
  • qq will try to cover both modes
  • Optimal qq: ฮผโ‰ˆ0\mu \approx 0, ฯƒโ‰ˆ3\sigma \approx 3 (high variance to cover both peaks, but puts probability mass between the modes where pp has none) Reverse KL DKL(qโˆฅp)D_{KL}(q \| p):
  • We average over qq, so qq tries to avoid regions where pp is very small
  • Since pp has a valley at 0, qq will choose to model a single mode
  • Optimal qq: either ฮผโ‰ˆโˆ’2\mu \approx -2, ฯƒโ‰ˆ1\sigma \approx 1 (left mode) or ฮผโ‰ˆ2\mu \approx 2, ฯƒโ‰ˆ1\sigma \approx 1 (right mode) This explains why:
  • VAEs minimize reverse KL (q(zโˆฃx)q(z|x) is optimized to avoid areas where p(z)p(z) has low density)
  • Maximum likelihood estimation minimizes forward KL (pฮธ(x)p_\theta(x) must cover all modes of pdatap_{\text{data}})

1.5 Cross-Entropy

Cross-entropy measures the average number of bits needed to encode data from pp using an optimal code designed for qq:
H(p,q)=Exโˆผp[โˆ’logโกq(x)]=โˆ’โˆ‘xp(x)logโกq(x)H(p, q) = \mathbb{E}_{x \sim p}[-\log q(x)] = -\sum_x p(x) \log q(x)
Relationship to KL divergence:
H(p,q)=H(p)+DKL(pโˆฅq)H(p, q) = H(p) + D_{KL}(p \| q)
Since H(p)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:
LCE=โˆ’1Nโˆ‘i=1Nlogโกq(y(i)โˆฃx(i))\mathcal{L}_{CE} = -\frac{1}{N}\sum_{i=1}^N \log q(y^{(i)} | x^{(i)})
where q(yโˆฃx)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:
NLL=โˆ’Exโˆผpdata[logโกpฮธ(x)]=H(pdata,pฮธ)\text{NLL} = -\mathbb{E}_{x \sim p_{\text{data}}}[\log p_\theta(x)] = H(p_{\text{data}}, p_\theta)

1.6 Mutual Information

Mutual information (MI) measures how much knowing one variable tells you about another:
I(X;Y)=DKL(p(x,y)โˆฅp(x)p(y))=โˆ‘x,yp(x,y)logโกp(x,y)p(x)p(y)I(X; Y) = D_{KL}(p(x, y) \| p(x)p(y)) = \sum_{x, y} p(x, y) \log \frac{p(x, y)}{p(x)p(y)}
Equivalently:
I(X;Y)=H(X)โˆ’H(XโˆฃY)=H(Y)โˆ’H(YโˆฃX)I(X; Y) = H(X) - H(X|Y) = H(Y) - H(Y|X)

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 XX be the input to a noisy communication channel, and YY be the output:
  • Input: Xโˆˆ{0,1}X \in \{0, 1\}, P(X=0)=P(X=1)=0.5P(X=0) = P(X=1) = 0.5
  • Channel: transmits correctly with probability 0.9, flips with probability 0.1 Step 1: Compute H(X)H(X):
H(X)=โˆ’0.5logโก2(0.5)โˆ’0.5logโก2(0.5)=1ย bitH(X) = -0.5\log_2(0.5) - 0.5\log_2(0.5) = 1 \text{ bit}
Step 2: Compute H(XโˆฃY)H(X|Y):
P(Y=0)=P(X=0)โ‹…0.9+P(X=1)โ‹…0.1=0.5P(Y=0) = P(X=0) \cdot 0.9 + P(X=1) \cdot 0.1 = 0.5 P(Y=1)=0.5P(Y=1) = 0.5 P(X=0โˆฃY=0)=0.9,P(X=1โˆฃY=0)=0.1P(X=0|Y=0) = 0.9, P(X=1|Y=0) = 0.1 H(XโˆฃY=0)=โˆ’0.9logโก2(0.9)โˆ’0.1logโก2(0.1)โ‰ˆ0.469H(X|Y=0) = -0.9\log_2(0.9) - 0.1\log_2(0.1) \approx 0.469 H(XโˆฃY=1)=0.469ย (byย symmetry)H(X|Y=1) = 0.469 \text{ (by symmetry)} H(XโˆฃY)=0.5โ‹…0.469+0.5โ‹…0.469=0.469H(X|Y) = 0.5 \cdot 0.469 + 0.5 \cdot 0.469 = 0.469
Step 3: Mutual information:
I(X;Y)=H(X)โˆ’H(XโˆฃY)=1โˆ’0.469=0.531ย bitsI(X;Y) = H(X) - H(X|Y) = 1 - 0.469 = 0.531 \text{ bits}
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:
logโกp(x)=DKL(qฯ•(zโˆฃx)โˆฅpฮธ(zโˆฃx))+L(x;ฮธ,ฯ•)\log p(x) = D_{KL}(q_\phi(z|x) \| p_\theta(z|x)) + \mathcal{L}(x; \theta, \phi) L(x;ฮธ,ฯ•)=Eqฯ•(zโˆฃx)[logโกpฮธ(xโˆฃz)]โŸReconstructionโˆ’DKL(qฯ•(zโˆฃx)โˆฅp(z))โŸKLย regularization\mathcal{L}(x; \theta, \phi) = \underbrace{\mathbb{E}_{q_\phi(z|x)}[\log p_\theta(x|z)]}_{\text{Reconstruction}} - \underbrace{D_{KL}(q_\phi(z|x) \| p(z))}_{\text{KL regularization}}
The KL term pushes the approximate posterior qฯ•(zโˆฃx)q_\phi(z|x) toward the prior p(z)p(z), acting as a regularizer. This is reverse KL because we average over qq.

1.7.2 InfoGAN: Maximizing Mutual Information

InfoGAN (Chen et al., 2016) adds a term to the GAN objective to encourage disentangled representations:
minโกGmaxโกDV(D,G)โˆ’ฮปI(c;G(z,c))\min_G \max_D V(D, G) - \lambda I(c; G(z, c))
where cc is a latent code and I(c;G(z,c))I(c; G(z, c)) is the mutual information between cc and the generated image. By maximizing MI, InfoGAN ensures the latent code cc has a meaningful effect on the output (e.g., c1c_1 controls digit identity, c2c_2 controls rotation). In practice, the mutual information is approximated using a variational lower bound:
I(c;G(z,c))โ‰ฅExโˆผG(z,c)[logโกq(cโˆฃx)]+H(c)I(c; G(z, c)) \geq \mathbb{E}_{x \sim G(z, c)}[\log q(c|x)] + H(c)
where q(cโˆฃx)q(c|x) is a learned auxiliary distribution that approximates p(cโˆฃx)p(c|x).

1.7.3 Diffusion Model ELBO

The diffusion model's variational bound can be expressed as:
LDM=โˆ‘t=1TEx0,ฯตt[DKL(q(xtโˆ’1โˆฃxt,x0)โˆฅpฮธ(xtโˆ’1โˆฃxt))โŸKLย betweenย trueย posteriorย andย model]\mathcal{L}_{DM} = \sum_{t=1}^T \mathbb{E}_{x_0, \epsilon_t}[\underbrace{D_{KL}(q(x_{t-1}|x_t, x_0) \| p_\theta(x_{t-1}|x_t))}_{\text{KL between true posterior and model}}]
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)=0q(x) = 0 but p(x)>0p(x) > 0, then DKL(pโˆฅq)=โˆžD_{KL}(p \| q) = \infty. This is why VAEs use reverse KL (where qq 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))0 \leq I(X;Y) \leq \min(H(X), H(Y)).
  • Data processing inequality: Post-processing can't increase information. If Xโ†’Yโ†’ZX \to Y \to Z, then I(X;Z)โ‰คI(X;Y)I(X;Z) \leq I(X;Y).

1.9 Why This Matters

Information theory provides the theoretical foundation for nearly every generative model:
ModelKey Information-Theoretic Concept
VAEKL divergence (ELBO), reverse KL for posterior
GAN (InfoGAN)Mutual information for disentanglement
DiffusionKL between Gaussians, entropy of noise
AutoregressiveCross-entropy (NLL) as training loss
EvaluationBits/dim (cross-entropy per dimension)
Normalizing FlowsChange of variables and entropy
Understanding the information-theoretic roots of these losses helps you:
  1. Diagnose training failures (e.g., KL vanishing in VAEs)
  2. Design new model variants (e.g., ฮฒ-VAE that weights KL)
  3. Choose appropriate evaluation metrics

2. ๐Ÿ“ Key Formulas / Concepts

ConceptFormulaInterpretation
EntropyH(p)=โˆ’โˆ‘p(x)logโกp(x)H(p) = -\sum p(x) \log p(x)Average uncertainty
Cross-entropyH(p,q)=โˆ’โˆ‘p(x)logโกq(x)H(p, q) = -\sum p(x) \log q(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)D_{KL}(p \| q) and DKL(qโˆฅp)D_{KL}(q \| p) interchangeably. Why: KL divergence is not symmetric. The forward KL (pโˆฅqp\|q) averages over pp; reverse KL (qโˆฅpq\|p) averages over qq. 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))D_{KL}(q_\phi(z|x) \| p(z)) โ€” reverse KL. In maximum likelihood estimation, we minimize DKL(pdataโˆฅpฮธ)D_{KL}(p_{\text{data}} \| p_\theta) โ€” 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)=โˆ’โˆ‘plogโกqH(p, q) = -\sum p \log q is commonly used. Since H(p,q)=H(p)+DKL(pโˆฅq)H(p, q) = H(p) + D_{KL}(p \| q), minimizing cross-entropy is equivalent to minimizing KL divergence when H(p)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: logโก2\log_2 gives bits; lnโก\ln (or logโกe\log_e) gives nats. Conversion: 1 nat = logโก2eโ‰ˆ1.44\log_2 e \approx 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)q_\phi(z|x) equals the prior p(z)p(z) for all xx. 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))D_{KL}(q_\phi(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)I(X;Y) is notoriously difficult to estimate because p(x,y)p(x,y) and p(x)p(y)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=1616logโก2(16)=logโก2(6)โ‰ˆ2.585ย bitsH = -\sum_{i=1}^6 \frac{1}{6}\log_2(\frac{1}{6}) = \log_2(6) \approx 2.585 \text{ bits}
This 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.5p=0.5), Coin B is biased (p=0.99p=0.99). Which has higher entropy and why?
Coin A: H=โˆ’0.5logโก2(0.5)โˆ’0.5logโก2(0.5)=1.0ย bitH = -0.5\log_2(0.5) - 0.5\log_2(0.5) = 1.0 \text{ bit}
Coin B: H=โˆ’0.99logโก2(0.99)โˆ’0.01logโก2(0.01)โ‰ˆ0.081ย bitsH = -0.99\log_2(0.99) - 0.01\log_2(0.01) \approx 0.081 \text{ bits}
Coin 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)D_{KL}(p \| q) where pp is Bernoulli(0.5) and qq is Bernoulli(0.8).
DKL(pโˆฅq)=โˆ‘xโˆˆ{0,1}p(x)logโกp(x)q(x)D_{KL}(p \| q) = \sum_{x \in \{0,1\}} p(x) \log\frac{p(x)}{q(x)}
=0.5logโก0.50.8+0.5logโก0.50.2= 0.5 \log\frac{0.5}{0.8} + 0.5 \log\frac{0.5}{0.2}
=0.5logโก(0.625)+0.5logโก(2.5)= 0.5 \log(0.625) + 0.5 \log(2.5)
=0.5(โˆ’0.470)+0.5(0.916)= 0.5(-0.470) + 0.5(0.916)
=โˆ’0.235+0.458=0.223ย nats= -0.235 + 0.458 = 0.223 \text{ nats}
In bits: 0.223ร—logโก2(e)โ‰ˆ0.223ร—1.44โ‰ˆ0.321ย bits0.223 \times \log_2(e) \approx 0.223 \times 1.44 \approx 0.321 \text{ bits} Q4: In the VAE loss function, why does the KL term use DKL(qฯ•(zโˆฃx)โˆฅp(z))D_{KL}(q_\phi(z|x) \| p(z)) instead of DKL(p(z)โˆฅqฯ•(zโˆฃx))D_{KL}(p(z) \| q_\phi(z|x))?
The ELBO derivation starts from:
logโกp(x)=DKL(qฯ•(zโˆฃx)โˆฅpฮธ(zโˆฃx))+L\log p(x) = D_{KL}(q_\phi(z|x) \| p_\theta(z|x)) + \mathcal{L}
where L\mathcal{L} is the ELBO. We want to minimize the KL between qฯ•(zโˆฃx)q_\phi(z|x) and the true posterior pฮธ(zโˆฃx)p_\theta(z|x), but we can't compute pฮธ(zโˆฃx)p_\theta(z|x) directly. So we rearrange:
L=Eqฯ•(zโˆฃx)[logโกpฮธ(xโˆฃz)]โˆ’DKL(qฯ•(zโˆฃx)โˆฅp(z))\mathcal{L} = \mathbb{E}_{q_\phi(z|x)}[\log p_\theta(x|z)] - D_{KL}(q_\phi(z|x) \| p(z))
The KL term is DKL(qฯ•(zโˆฃx)โˆฅp(z))D_{KL}(q_\phi(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ฯ•q_\phi.
Using reverse KL means qฯ•(zโˆฃx)q_\phi(z|x) tends to be mode-seeking โ€” it will try to find a single good latent code for each xx 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))D_{KL}(p(z) \| q_\phi(z|x)) instead, the KL would be โˆ’โˆž-\infty wherever qฯ•q_\phi assigns zero probability where p(z)>0p(z) > 0, which would force qฯ•q_\phi to have very broad support for every input โ€” undesirable. Q5: In InfoGAN, the mutual information I(c;G(z,c))I(c; G(z,c)) is maximized. Show how the variational lower bound I(c;x)โ‰ฅExโˆผG(z,c)[logโกq(cโˆฃx)]+H(c)I(c; x) \geq \mathbb{E}_{x \sim G(z,c)}[\log q(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)[logโกp(cโ€ฒโˆฃx)]]I(c; x) = H(c) - H(c|x) = H(c) + \mathbb{E}_{x \sim G(z,c)}[\mathbb{E}_{c' \sim p(c|x)}[\log p(c'|x)]]
We introduce a variational approximation q(cโˆฃx)q(c|x) to the true posterior p(cโˆฃx)p(c|x):
I(c;x)=H(c)+Ex[Ecโ€ฒโˆผp(cโˆฃx)[logโกp(cโ€ฒโˆฃx)]]I(c; x) = H(c) + \mathbb{E}_{x}[\mathbb{E}_{c' \sim p(c|x)}[\log p(c'|x)]]
=H(c)+Ex[Ecโ€ฒโˆผp(cโˆฃx)[logโกq(cโ€ฒโˆฃx)]+DKL(p(cโˆฃx)โˆฅq(cโˆฃx))]= H(c) + \mathbb{E}_{x}[\mathbb{E}_{c' \sim p(c|x)}[\log q(c'|x)] + D_{KL}(p(c|x) \| q(c|x))]
Since DKLโ‰ฅ0D_{KL} \geq 0:
I(c;x)โ‰ฅH(c)+ExโˆผG(z,c)[logโกq(cโˆฃx)]I(c; x) \geq H(c) + \mathbb{E}_{x \sim G(z,c)}[\log q(c|x)]
This gives a tractable lower bound on mutual information that we can optimize. q(cโˆฃx)q(c|x) is an auxiliary network that tries to predict the latent code cc from the generated image xx. Q6: A Gaussian distribution N(0,0.1)\mathcal{N}(0, 0.1) has differential entropy. Compute it.
Differential entropy of a univariate Gaussian:
H(N(ฮผ,ฯƒ2))=12lnโก(2ฯ€eฯƒ2)H(\mathcal{N}(\mu, \sigma^2)) = \frac{1}{2}\ln(2\pi e \sigma^2)
For ฯƒ2=0.1\sigma^2 = 0.1:
H=12lnโก(2ฯ€eร—0.1)=12lnโก(0.2ฯ€e)=12lnโก(1.708)โ‰ˆ12ร—0.536=0.268ย natsH = \frac{1}{2}\ln(2\pi e \times 0.1) = \frac{1}{2}\ln(0.2\pi e) = \frac{1}{2}\ln(1.708) \approx \frac{1}{2} \times 0.536 = 0.268 \text{ nats}
This is positive (low entropy for a narrow distribution). For a very narrow Gaussian (ฯƒโ†’0\sigma \to 0), differential entropy is โˆ’โˆž-\infty. 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ร—CD = H \times W \times C dimensions:
bits/dim=โˆ’logโก2pฮธ(x)D=1Dโ‹…(โˆ’logโก2pฮธ(x))โŸcross-entropyย inย bits\text{bits/dim} = -\frac{\log_2 p_\theta(x)}{D} = \frac{1}{D} \cdot \underbrace{(-\log_2 p_\theta(x))}_{\text{cross-entropy in bits}}
This is the cross-entropy between the true data distribution (which places a delta on xx) 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: XX is uniformly distributed over {1,2,3,4}\{1, 2, 3, 4\}. Y=X2modโ€‰โ€‰4Y = X^2 \mod 4. Compute I(X;Y)I(X; Y).
Step 1: H(X)=logโก2(4)=2ย bitsH(X) = \log_2(4) = 2 \text{ bits}
Step 2: Y=X2modโ€‰โ€‰4Y = X^2 \mod 4:
  • X=1โ†’Y=1X=1 \to Y=1
  • X=2โ†’Y=0X=2 \to Y=0
  • X=3โ†’Y=1X=3 \to Y=1
  • X=4โ†’Y=0X=4 \to Y=0
So P(Y=0)=0.5P(Y=0) = 0.5, P(Y=1)=0.5P(Y=1) = 0.5
Step 3: H(Y)=1ย bitH(Y) = 1 \text{ bit}
Step 4: P(XโˆฃY=0)P(X|Y=0): Xโˆˆ{2,4}X \in \{2, 4\} equally likely โ†’ H(XโˆฃY=0)=1ย bitH(X|Y=0) = 1 \text{ bit} P(XโˆฃY=1)P(X|Y=1): Xโˆˆ{1,3}X \in \{1, 3\} equally likely โ†’ H(XโˆฃY=1)=1ย bitH(X|Y=1) = 1 \text{ bit}
H(XโˆฃY)=0.5ร—1+0.5ร—1=1ย bitH(X|Y) = 0.5 \times 1 + 0.5 \times 1 = 1 \text{ bit}
Step 5: I(X;Y)=H(X)โˆ’H(XโˆฃY)=2โˆ’1=1ย bitI(X; Y) = H(X) - H(X|Y) = 2 - 1 = 1 \text{ bit}
Knowing YY reduces the uncertainty about XX from 2 bits to 1 bit โ€” you still don't know whether XX 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)q(x_t | x_0) = \mathcal{N}(x_t; \sqrt{\bar{\alpha}_t} x_0, (1-\bar{\alpha}_t)I)
As tt increases (more noise added):
  • ฮฑห‰tโ†’0\bar{\alpha}_t \to 0
  • Mean goes to 0
  • Variance goes to II
The entropy of q(xtโˆฃx0)q(x_t | x_0) increases from near-zero (at t=0t=0, xtx_t is nearly deterministic given x0x_0) to:
H(q(xT))โ‰ˆD2lnโก(2ฯ€e)H(q(x_T)) \approx \frac{D}{2}\ln(2\pi e)
which is the maximum entropy for a DD-dimensional distribution with fixed variance โ€” matching a standard Gaussian N(0,I)\mathcal{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 ฮฒ\beta-VAE modifies the VAE loss to L=Eqฯ•(zโˆฃx)[logโกpฮธ(xโˆฃz)]โˆ’ฮฒโ‹…DKL(qฯ•(zโˆฃx)โˆฅp(z))\mathcal{L} = \mathbb{E}_{q_\phi(z|x)}[\log p_\theta(x|z)] - \beta \cdot D_{KL}(q_\phi(z|x) \| p(z)). Explain why ฮฒ>1\beta > 1 encourages more disentangled representations.
With ฮฒ>1\beta > 1, the KL term is weighted more heavily, which pushes each latent dimension's distribution qฯ•(ziโˆฃx)q_\phi(z_i|x) to be closer to the prior p(zi)=N(0,1)p(z_i) = \mathcal{N}(0, 1).
For disentanglement, this means:
  1. Factorized prior: The prior p(z)=โˆip(zi)p(z) = \prod_i p(z_i) is factorized (independent dimensions). By pushing qฯ•(zโˆฃx)q_\phi(z|x) toward p(z)p(z), we encourage the posterior to also be factorized.
  2. Information bottleneck: The higher KL penalty limits the total information that zz can carry about xx. The model must allocate this limited information efficiently, learning to encode only the most salient, independent factors of variation in separate dimensions.
  3. 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\beta > 1 also reduces reconstruction quality (the rate-distortion tradeoff). Higher ฮฒ\beta reduces "rate" (KL) but increases "distortion" (reconstruction error). Finding the right ฮฒ\beta 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)=1Nโˆ‘i=1Nฮด(xโˆ’x(i))\hat{p}_{\text{data}}(x) = \frac{1}{N} \sum_{i=1}^N \delta(x - x^{(i)})
where ฮด\delta is the Dirac delta.
Maximum likelihood estimation maximizes:
1Nโˆ‘i=1Nlogโกpฮธ(x(i))=Exโˆผp^data[logโกpฮธ(x)]\frac{1}{N} \sum_{i=1}^N \log p_\theta(x^{(i)}) = \mathbb{E}_{x \sim \hat{p}_{\text{data}}}[\log p_\theta(x)]
The forward KL divergence between p^data\hat{p}_{\text{data}} and pฮธp_\theta:
DKL(p^dataโˆฅpฮธ)=Exโˆผp^data[logโกp^data(x)โˆ’logโกpฮธ(x)]D_{KL}(\hat{p}_{\text{data}} \| p_\theta) = \mathbb{E}_{x \sim \hat{p}_{\text{data}}}[\log \hat{p}_{\text{data}}(x) - \log p_\theta(x)]
=Exโˆผp^data[logโกp^data(x)]โŸconstantย (entropyย ofย empiricalย dist)โˆ’Exโˆผp^data[logโกpฮธ(x)]โŸlog-likelihood= \underbrace{\mathbb{E}_{x \sim \hat{p}_{\text{data}}}[\log \hat{p}_{\text{data}}(x)]}_{\text{constant (entropy of empirical dist)}} - \underbrace{\mathbb{E}_{x \sim \hat{p}_{\text{data}}}[\log p_\theta(x)]}_{\text{log-likelihood}}
Since the first term is constant with respect to ฮธ\theta, minimizing DKLD_{KL} is equivalent to maximizing the log-likelihood.
This is forward KL because we average over p^data\hat{p}_{\text{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)D_{KL}(p \| q) where pp is a mixture of two Gaussians and qq 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 pp is bimodal (two modes) and qq is a single Gaussian:
Forward KL (DKL(pโˆฅq)D_{KL}(p \| q) minimized over qq):
  • qq spreads out to cover both modes
  • Result: high-variance Gaussian centered between the modes
  • qq has mass in the middle where pp has none
  • This is "mean-seeking" โ€” it captures the overall spread but not the structure
Reverse KL (DKL(qโˆฅp)D_{KL}(q \| p) minimized over qq):
  • qq 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:
  1. Real images have many modes (cats, dogs, cars, etc.)
  2. Forward KL forces the model to cover all modes (better recall)
  3. 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

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.