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)
| Symbol | Say it as | In plain words |
|---|---|---|
| ”x” | one data point, e.g. one image as a list of numbers | |
| ”script X” | the set of all possible data points | |
| ”P-x of x” | how likely real data is to land at the point (the true density) | |
| “P-theta of x” | how likely the model is to produce the point | |
| “theta” | the model’s adjustable knobs (network weights) | |
| “integral over x” | add up continuously over all possible values of | |
| “sum over i” | add these up, one term per item | |
| “expected value of h, with x drawn from p” | the average value of you would get over many random draws from | |
| “x drawn from p” | is picked at random according to distribution | |
| , | “log”, “natural log” | how many times you multiply by to get here; turns products into sums |
| ”e to the t” | multiply by itself times; the reverse of | |
| “f of u” | a penalty function: feed in a mismatch, get back a cost | |
| ”f-star of t” | the convex conjugate of (explained in Section 3b) | |
| “f-prime of u” | the slope of at | |
| , | “soup”, “max” | the best (largest) value achievable |
| ”arg-min” | the setting that makes something smallest | |
| ”at least” | the left side is never smaller than the right | |
| ”if and only if” | each statement guarantees the other | |
| ”divergence of P from Q” | how different is from ; order matters | |
| ”in” | belongs to the set | |
| , | “the reals”, “the non-negative reals” | all ordinary numbers; numbers |
| “absolute value of a” | without its sign (distance from zero) | |
| “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 picks the score. Section 3b is the convex conjugate: the rewrite that estimates from samples, which is the bridge into GANs.
1. Intuition first
At every point , 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 turns each mismatch into a cost, and we average those costs over the whole space. Changing 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
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
is a standard bell centred at 0. is the same bell shifted to . Pick an . The top curve is the penalty, and it passes through . The coral curve is the integrand . The four numbers are the integrals.
Three things to watch as you move :
- At , 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 grows; KL keeps climbing.
4. The formula, decoded
From the lectures:
Read aloud: “The f-divergence of the real distribution from the model equals: for every possible data point , take the model’s probability at , multiply it by the penalty applied to the ratio (real probability at divided by model probability at ), and add all of these up over the whole data space.”
Piece by piece:
- : a single number , the total “difference score.” The subscript says which penalty function is used.
- : add up over every possible data point.
- , outside: a weight. Points the model produces often count more.
- : a single number , the local mismatch at .
- : 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 : it maps (non-negative numbers in, any number out), it is convex, and it is lower-semicontinuous, with
In plain words: perfect agreement at a point costs nothing.
Key properties:
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
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 than model data does.
- : agreement.
- : the model under-produces here.
- : the model over-produces here.
- : the model puts mass where real data never goes.
Back to the main thread. only ever sees this one number, so it only ever judges “how mismatched are we here?”
Detour 2: Convexity
For any and any mixing weight between 0 and 1,
Read aloud: “a weighted average of two outputs of is at least as large as 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 , , , and (an equal mix):
- Average of the outputs: .
- Output at the average input: .
- ✓
Back to the main thread. Convexity is the property that forces (proof below).
Detour 3: Expectation as an integral
For a discrete random variable (a finite list of outcomes):
Read aloud: “the expected value of is the sum, over all outcomes, of each outcome’s probability times of that outcome.”
In plain words: a probability-weighted average.
Example: for a fair die, .
For a continuous variable, the sum becomes an integral:
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 and , so
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 and any random number :
Read aloud: “the average of of is at least of the average of .”
In plain words: for a bowl-shaped function, averaging after applying gives at least as much as applying after averaging. It is the convexity rule from Detour 2, extended from two points to any number of points.
Check with and equal to 0 or 2, each with probability ½:
- ✓
Back to the main thread. Here is the proof that . The lectures state this without proof; the proof is my addition.
Step 1, write as an expectation (Detour 3):
In plain words: the average penalty over model samples.
Step 2, apply Jensen (Detour 4), since is convex:
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 cancels:
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:
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
| Name | In plain words, this mostly punishes… | |
|---|---|---|
| Forward KL | large ratios: the model missing real data | |
| Reverse KL | ratios near 0: the model producing data that real data never has | |
| Jensen–Shannon (JS) | both directions, symmetrically, with a cap | |
| Total variation (TV) | any mismatch, in proportion to its size |
Substituting into the template:
Read aloud: “the f-divergence with 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 :
In plain words: the same idea in the opposite direction, averaged over model samples.
For TV, substituting gives
In plain words: half the total amount by which the two density curves differ.
Notes on the lecture slides:
- The reverse-KL is my addition; the slides name reverse KL but do not write its .
- The JS and TV formulas above are the standard forms satisfying .
- Week 2 uses for GANs; that is the same as JS up to scaling and a constant.
5. Tiny worked example
Two outcomes. Real: , a fair coin. Model: , a biased coin.
The ratios:
In plain words: the model over-produces outcome 1 (ratio below 1) and under-produces outcome 2 (ratio above 1).
The discrete template:
In plain words: for each outcome, model probability times penalty, then add.
| Divergence | Computation | Value |
|---|---|---|
| Forward KL | ≈ 0.223 | |
| Reverse KL | ≈ 0.193 | |
| JS | ≈ 0.051 | |
| TV | 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 and outcome 2 contributes . A negative piece, but a positive total: Jensen at work.
- If you set , every ratio is 1, so every row is .
The catch
The formula needs the values and at every point. But is unknown (we only have the dataset), and has no formula (we can only sample from ). So we cannot compute the ratio anywhere. What we can compute are averages over samples. Section 3b rewrites using only such averages.
6. Connect back
- Weeks 2–3: GANs use a JS-like .
- 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)
- Why does have to hold? What breaks if ?
- In the worked example, outcome 1 contributes a negative amount to forward KL. Why does that not break ?
- Why can I not plug the dataset into the formula and compute it directly?
Next: Section 3b: The convex conjugate and the variational bound.