Skip to content
DineshKumar Sarangapani
All writing

Section 3a: f-divergences

One convex function f turns a density ratio into a divergence. Forward KL, reverse KL, Jensen–Shannon, and total variation are four choices of f.

I am working through Mathematical Foundations of Generative AI, Prof. Prathosh AP’s public lectures. The playlist is the spine. These notes are my deep dive on each section: a visual when the picture is the point, a detour when a prerequisite is doing real work, and every formula read aloud with a plain-words line right after it.

Previously: Section 2: The general principle of generative models.

Symbol cheat-sheet (used in 3a and 3b)

SymbolSay it asIn plain words
xx”x”one data point, e.g. one image as a list of numbers
X\mathcal{X}”script X”the set of all possible data points
Px(x)P_x(x)”P-x of x”how likely real data is to land at the point xx (the true density)
Pθ(x)P_\theta(x)“P-theta of x”how likely the model is to produce the point xx
θ\theta“theta”the model’s adjustable knobs (network weights)
∫…dx\int \ldots dx“integral over x”add up continuously over all possible values of xx
∑i\sum_i“sum over i”add these up, one term per item ii
Ex∼p[h(x)]\mathbb{E}_{x \sim p}[h(x)]“expected value of h, with x drawn from p”the average value of hh you would get over many random draws from pp
x∼px \sim p“x drawn from p”xx is picked at random according to distribution pp
log⁡\log, ln⁡\ln“log”, “natural log”how many times you multiply by e≈2.718e \approx 2.718 to get here; turns products into sums
ete^{t}”e to the t”multiply e≈2.718e \approx 2.718 by itself tt times; the reverse of ln⁡\ln
f(u)f(u)“f of u”a penalty function: feed in a mismatch, get back a cost
f∗(t)f^*(t)”f-star of t”the convex conjugate of ff (explained in Section 3b)
f′(u)f'(u)“f-prime of u”the slope of ff at uu
sup⁡\sup, max⁡\max“soup”, “max”the best (largest) value achievable
arg⁡min⁡\arg\min”arg-min”the setting that makes something smallest
≥\ge”at least”the left side is never smaller than the right
  ⟺  \iff”if and only if”each statement guarantees the other
D(P ∥ Q)D(P \,\|\, Q)”divergence of P from Q”how different PP is from QQ; order matters
∈\in”in”belongs to the set
R\mathbb{R}, R+\mathbb{R}_+“the reals”, “the non-negative reals”all ordinary numbers; numbers ≥0\ge 0
∣a∣\lvert a \rvert“absolute value of a”aa without its sign (distance from zero)
≈\approx“approximately”close to

Section 3a: f-divergences

Section 2 left a slot for a divergence. This note fills it with a family. One convex function ff picks the score. Section 3b is the convex conjugate: the rewrite that estimates DfD_f from samples, which is the bridge into GANs.

1. Intuition first

At every point xx, compare how much probability real data puts there with how much the model puts there, by taking the ratio of the two. A ratio of 1 means “agreement here.” Any other value is a mismatch. A penalty function ff turns each mismatch into a cost, and we average those costs over the whole space. Changing ff changes which kinds of mismatch hurt most, so one template gives a whole family of “difference scores.”

2. Real-world analogy

You are comparing synthetic traffic against the real access log, one endpoint at a time. For each endpoint, compute

ratio=real share of trafficsynthetic share of traffic\text{ratio} = \frac{\text{real share of traffic}}{\text{synthetic share of traffic}}

In plain words: how many times more often real users hit this endpoint than your generator does.

A ratio of 3 means the generator under-produces this endpoint. A ratio of 0.2 means it over-produces it. Each team picks its own penalty for mismatches: the SRE team hates missing real patterns (forward KL), the security team hates fake-looking traffic (reverse KL). Each team’s total score is a different f-divergence.

3. Visual

PxP_x is a standard bell centred at 0. PθP_\theta is the same bell shifted to μ\mu. Pick an ff. The top curve is the penalty, and it passes through (1,0)(1, 0). The coral curve is the integrand Pθ(x) f(Px(x)/Pθ(x))P_\theta(x)\, f(P_x(x)/P_\theta(x)). The four numbers are the integrals.

Three things to watch as you move μ\mu:

  • At μ=0\mu = 0, every score is 0.
  • With forward KL, the coral curve dips below zero in places, but the total never goes negative.
  • JS and TV level off as μ\mu grows; KL keeps climbing.

4. The formula, decoded

From the lectures:

Df(Px ∥ Pθ)=∫XPθ(x) f ⁣(Px(x)Pθ(x))dxD_f(P_x \,\|\, P_\theta) = \int_{\mathcal{X}} P_\theta(x)\, f\!\left(\frac{P_x(x)}{P_\theta(x)}\right) dx

Read aloud: “The f-divergence of the real distribution from the model equals: for every possible data point xx, take the model’s probability at xx, multiply it by the penalty ff applied to the ratio (real probability at xx divided by model probability at xx), and add all of these up over the whole data space.”

Piece by piece:

  • Df(Px ∥ Pθ)D_f(P_x \,\|\, P_\theta): a single number ≥0\ge 0, the total “difference score.” The subscript ff says which penalty function is used.
  • ∫X…dx\int_{\mathcal{X}} \ldots dx: add up over every possible data point.
  • Pθ(x)P_\theta(x), outside: a weight. Points the model produces often count more.
  • Px(x)Pθ(x)\frac{P_x(x)}{P_\theta(x)}: a single number ≥0\ge 0, the local mismatch at xx.
  • f(⋅)f(\cdot): converts that mismatch into a cost.

In plain words: the f-divergence is the model-weighted total cost of how badly the two distributions disagree, point by point.

We require three things of ff: it maps R+→R\mathbb{R}_+ \to \mathbb{R} (non-negative numbers in, any number out), it is convex, and it is lower-semicontinuous, with

f(1)=0f(1) = 0

In plain words: perfect agreement at a point costs nothing.

Key properties:

Df(Px ∥ Pθ)≥0,Df(Px ∥ Pθ)=0  ⟺  Px=PθD_f(P_x \,\|\, P_\theta) \ge 0, \qquad D_f(P_x \,\|\, P_\theta) = 0 \iff P_x = P_\theta

In plain words: the score is never negative, and it is exactly zero only when the model matches reality everywhere.

Detour 1: The density ratio

u(x)=Px(x)Pθ(x)u(x) = \frac{P_x(x)}{P_\theta(x)}

Read aloud: “u at x is the real probability at x divided by the model probability at x.”

In plain words: how many times more often real data shows up at xx than model data does.

  • u=1u = 1: agreement.
  • u>1u > 1: the model under-produces here.
  • u<1u < 1: the model over-produces here.
  • u=0u = 0: the model puts mass where real data never goes.

Back to the main thread. ff only ever sees this one number, so it only ever judges “how mismatched are we here?”

Detour 2: Convexity

For any u1,u2u_1, u_2 and any mixing weight λ\lambda between 0 and 1,

λf(u1)+(1−λ)f(u2)  ≥  f(λu1+(1−λ)u2)\lambda f(u_1) + (1-\lambda) f(u_2) \;\ge\; f\big(\lambda u_1 + (1-\lambda) u_2\big)

Read aloud: “a weighted average of two outputs of ff is at least as large as ff evaluated at the same weighted average of the inputs.”

In plain words: the curve is bowl-shaped, so the straight line between two points on it lies on or above the curve.

Tiny check with f(u)=u2f(u) = u^2, u1=0u_1 = 0, u2=2u_2 = 2, and λ=12\lambda = \tfrac12 (an equal mix):

  • Average of the outputs: 12(0)+12(4)=2\tfrac12(0) + \tfrac12(4) = 2.
  • Output at the average input: f(1)=1f(1) = 1.
  • 2≥12 \ge 1 ✓

Back to the main thread. Convexity is the property that forces Df≥0D_f \ge 0 (proof below).

Detour 3: Expectation as an integral

For a discrete random variable (a finite list of outcomes):

E[h(X)]=∑ipi h(xi)\mathbb{E}[h(X)] = \sum_i p_i \, h(x_i)

Read aloud: “the expected value of hh is the sum, over all outcomes, of each outcome’s probability times hh of that outcome.”

In plain words: a probability-weighted average.

Example: for a fair die, E[roll]=16(1+2+3+4+5+6)=3.5\mathbb{E}[\text{roll}] = \tfrac16(1 + 2 + 3 + 4 + 5 + 6) = 3.5.

For a continuous variable, the sum becomes an integral:

Ex∼p[h(x)]=∫p(x) h(x) dx\mathbb{E}_{x \sim p}[h(x)] = \int p(x)\, h(x)\, dx

In plain words: the same weighted average, but added up continuously over all values instead of over a list.

Back to the main thread. The f-divergence has exactly this shape, with p=Pθp = P_\theta and h=f(ratio)h = f(\text{ratio}), so

Df(Px ∥ Pθ)=Ex∼Pθ ⁣[f ⁣(Px(x)Pθ(x))]D_f(P_x \,\|\, P_\theta) = \mathbb{E}_{x \sim P_\theta}\!\left[ f\!\left(\frac{P_x(x)}{P_\theta(x)}\right) \right]

Read aloud: “the f-divergence is the expected value, over points drawn from the model, of the penalty applied to the density ratio.”

In plain words: draw many points from the model, compute the mismatch penalty at each one, and average the penalties.

Detour 4: Jensen’s inequality

For any convex ff and any random number UU:

E[f(U)]  ≥  f(E[U])\mathbb{E}[f(U)] \;\ge\; f\big(\mathbb{E}[U]\big)

Read aloud: “the average of ff of UU is at least ff of the average of UU.”

In plain words: for a bowl-shaped function, averaging after applying ff gives at least as much as applying ff after averaging. It is the convexity rule from Detour 2, extended from two points to any number of points.

Check with f(u)=u2f(u) = u^2 and UU equal to 0 or 2, each with probability ½:

  • E[f(U)]=12(0)+12(4)=2\mathbb{E}[f(U)] = \tfrac12(0) + \tfrac12(4) = 2
  • f(E[U])=f(1)=1f(\mathbb{E}[U]) = f(1) = 1
  • 2≥12 \ge 1 ✓

Back to the main thread. Here is the proof that Df≥0D_f \ge 0. The lectures state this without proof; the proof is my addition.

Step 1, write DfD_f as an expectation (Detour 3):

Df=Ex∼Pθ ⁣[f ⁣(Px(x)Pθ(x))]D_f = \mathbb{E}_{x \sim P_\theta}\!\left[ f\!\left(\frac{P_x(x)}{P_\theta(x)}\right) \right]

In plain words: the average penalty over model samples.

Step 2, apply Jensen (Detour 4), since ff is convex:

Df  ≥  f ⁣(Ex∼Pθ ⁣[Px(x)Pθ(x)])D_f \;\ge\; f\!\left( \mathbb{E}_{x \sim P_\theta}\!\left[ \frac{P_x(x)}{P_\theta(x)} \right] \right)

In plain words: the average penalty is at least the penalty of the average ratio.

Step 3, write that inner average as an integral, where PθP_\theta cancels:

Ex∼Pθ ⁣[Px(x)Pθ(x)]=∫Pθ(x) Px(x)Pθ(x) dx=∫Px(x) dx=1\mathbb{E}_{x \sim P_\theta}\!\left[ \frac{P_x(x)}{P_\theta(x)} \right] = \int P_\theta(x)\, \frac{P_x(x)}{P_\theta(x)}\, dx = \int P_x(x)\, dx = 1

In plain words: the model-weighted average of the ratio is always exactly 1, because the model weight cancels the denominator, leaving the total probability of real data, which is 1.

Step 4, combine:

Df  ≥  f(1)=0D_f \;\ge\; f(1) = 0

In plain words: the score is at least the penalty for perfect agreement, which is zero.

So a single point’s contribution can be negative (the coral dips in the widget), but the total never is.

The family: one template, different ff

Namef(u)f(u)In plain words, this ff mostly punishes…
Forward KLulog⁡uu \log ularge ratios: the model missing real data
Reverse KL−log⁡u-\log uratios near 0: the model producing data that real data never has
Jensen–Shannon (JS)12[ulog⁡u−(u+1)log⁡u+12]\tfrac12\left[u \log u - (u+1)\log\tfrac{u+1}{2}\right]both directions, symmetrically, with a cap
Total variation (TV)12∣u−1∣\tfrac12 \lvert u - 1 \rvertany mismatch, in proportion to its size

Substituting f(u)=ulog⁡uf(u) = u \log u into the template:

Df=∫Pθ⋅PxPθlog⁡PxPθ dx=∫Px(x)log⁡Px(x)Pθ(x) dx=DKL(Px ∥ Pθ)D_f = \int P_\theta \cdot \frac{P_x}{P_\theta} \log\frac{P_x}{P_\theta}\, dx = \int P_x(x) \log\frac{P_x(x)}{P_\theta(x)}\, dx = D_{KL}(P_x \,\|\, P_\theta)

Read aloud: “the f-divergence with ulog⁡uu \log u equals the integral of real probability times the log of real over model probability, which is the forward KL divergence.”

In plain words: averaged over real data, how much more surprised the model is by real points than the real distribution itself would be.

Substituting f(u)=−log⁡uf(u) = -\log u:

Df=∫Pθ(x)log⁡Pθ(x)Px(x) dx=DKL(Pθ ∥ Px)D_f = \int P_\theta(x) \log\frac{P_\theta(x)}{P_x(x)}\, dx = D_{KL}(P_\theta \,\|\, P_x)

In plain words: the same idea in the opposite direction, averaged over model samples.

For TV, substituting gives

DTV=12∫∣Px(x)−Pθ(x)∣ dxD_{TV} = \tfrac12 \int \lvert P_x(x) - P_\theta(x) \rvert\, dx

In plain words: half the total amount by which the two density curves differ.

Notes on the lecture slides:

  • The reverse-KL ff is my addition; the slides name reverse KL but do not write its ff.
  • The JS and TV formulas above are the standard forms satisfying f(1)=0f(1) = 0.
  • Week 2 uses ulog⁡u−(u+1)log⁡(u+1)u \log u - (u+1)\log(u+1) for GANs; that is the same as JS up to scaling and a constant.

5. Tiny worked example

Two outcomes. Real: Px=(0.5,0.5)P_x = (0.5, 0.5), a fair coin. Model: Pθ=(0.8,0.2)P_\theta = (0.8, 0.2), a biased coin.

The ratios:

u1=0.50.8=0.625,u2=0.50.2=2.5u_1 = \frac{0.5}{0.8} = 0.625, \qquad u_2 = \frac{0.5}{0.2} = 2.5

In plain words: the model over-produces outcome 1 (ratio below 1) and under-produces outcome 2 (ratio above 1).

The discrete template:

Df=∑iPθ(i) f(ui)D_f = \sum_i P_\theta(i)\, f(u_i)

In plain words: for each outcome, model probability times penalty, then add.

DivergenceComputationValue
Forward KL0.8⋅(0.625ln⁡0.625)+0.2⋅(2.5ln⁡2.5)=0.8(−0.294)+0.2(2.291)0.8 \cdot (0.625 \ln 0.625) + 0.2 \cdot (2.5 \ln 2.5) = 0.8(-0.294) + 0.2(2.291)≈ 0.223
Reverse KL0.8⋅(−ln⁡0.625)+0.2⋅(−ln⁡2.5)=0.8(0.470)+0.2(−0.916)0.8 \cdot (-\ln 0.625) + 0.2 \cdot (-\ln 2.5) = 0.8(0.470) + 0.2(-0.916)≈ 0.193
JS0.8⋅(0.0218)+0.2⋅(0.1660)0.8 \cdot (0.0218) + 0.2 \cdot (0.1660)≈ 0.051
TV0.8⋅12(0.375)+0.2⋅12(1.5)0.8 \cdot \tfrac12(0.375) + 0.2 \cdot \tfrac12(1.5)0.300

What the numbers show:

  • All four are positive, but they disagree on how far apart the coins are.
  • Forward KL ≠ reverse KL: direction matters.
  • In forward KL, outcome 1 contributes −0.235-0.235 and outcome 2 contributes +0.458+0.458. A negative piece, but a positive total: Jensen at work.
  • If you set Pθ=(0.5,0.5)P_\theta = (0.5, 0.5), every ratio is 1, so every row is f(1)=0f(1) = 0.

The catch

The formula needs the values Px(x)P_x(x) and Pθ(x)P_\theta(x) at every point. But PxP_x is unknown (we only have the dataset), and PθP_\theta has no formula (we can only sample from gθ(z)g_\theta(z)). So we cannot compute the ratio anywhere. What we can compute are averages over samples. Section 3b rewrites DfD_f using only such averages.

6. Connect back

  • Weeks 2–3: GANs use a JS-like ff.
  • Weeks 4–5, 7–8, 10: VAEs, DDPMs, and autoregressive models all minimise forward KL, which is the same as maximum likelihood.
  • Week 6: WGAN replaces bounded divergences (which level off) with Wasserstein.
  • Week 12: RLHF and DPO keep the tuned model close to the original using a KL term.

Quick check (optional)

  1. Why does f(1)=0f(1) = 0 have to hold? What breaks if f(1)=5f(1) = 5?
  2. In the worked example, outcome 1 contributes a negative amount to forward KL. Why does that not break Df≥0D_f \ge 0?
  3. Why can I not plug the dataset into the DfD_f formula and compute it directly?

Next: Section 3b: The convex conjugate and the variational bound.