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 the formula in my own words.
Previously: Section 2: The general principle of generative models.
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, not written yet, is the convex conjugate: the rewrite that estimates from samples, which is the bridge into GANs.
The statement I am carrying:
Given two distributions with densities and ,
where is convex and lower-semicontinuous, with . Then , and if and only if .
Intuition
At each point I compare how much probability the data puts there with how much the model puts there. The ratio is 1 where they agree and something else where they do not. turns that local mismatch into a penalty. The integral averages the penalties. A different punishes a different kind of mismatch, so one template gives a family.
The load test, by endpoint
I compare synthetic traffic with the real access log, endpoint by endpoint.
A ratio of 1 means the generator gets that endpoint right. A ratio of 3 means real users hit it three times as often as the generator does. A ratio of 0.2 means the generator hammers an endpoint real users barely touch.
The single score depends on which failure I care about.
- Missing real traffic patterns. The penalty grows when the ratio is large. That is forward KL.
- Fake traffic that real users would never send. The penalty grows when the ratio is near 0. That is reverse KL.
- A balanced score that cannot run off to infinity. That is Jensen–Shannon or total variation.
is the penalty. The integral is the average over endpoints.
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 I want in front of me while I move :
- At every divergence is 0. Matching distributions means zero penalty, for every with .
- With forward KL, the coral curve dips below zero in places. A single point can contribute a negative amount. The total never does. The proof below is why.
- Jensen–Shannon and total variation level off as grows. Both are bounded. The two KLs keep climbing. That saturation is the later reason a GAN, which aims at Jensen–Shannon, can stop learning when the data and the model do not overlap.
Detours
Density ratio. asks, at , how many times more likely real data is here than model data.
- : agreement at this point.
- : the model under-produces here.
- : the model over-produces here.
- : the model puts mass where the data never goes.
If everywhere, then everywhere. only ever sees this ratio.
Convexity. A function is convex when it is bowl-shaped: the straight line between any two points on its graph lies on or above the curve. For ,
Averaging the outputs is at least as big as the output of the average. Check with , , , . The average of the outputs is . The output of the average is . And .
Convexity is what forces .
Expectation as an integral. For a continuous density , the expected value of is a probability-weighted average:
Jensen’s inequality. For convex and a random variable ,
This is the two-point definition extended to any average. On a bowl, the average of points on the surface lands above the value at the average, never below.
The formula
Read aloud: the average penalty, over points drawn from the model, of how mismatched the two densities are at each point.
| Symbol | What it is | Type / shape | Role |
|---|---|---|---|
| true density at | scalar | the data’s weight here | |
| model density at | scalar | the model’s weight, and the weight in the average | |
| density ratio | scalar | local mismatch | |
| generator of the divergence | convex, | turns mismatch into a penalty | |
| integral over | operation | adds the penalties | |
| the divergence | scalar | the total score |
Each condition on earns its place:
- . If the distributions match, everywhere, so .
- Convex. This is the proof below.
- Lower-semicontinuous. No sudden downward jumps. Section 3b needs it so the conjugate is well behaved.
The proof that
The lectures state the property. This is the short proof.
The first line is the expectation form. The second is Jensen. The third writes the expectation as an integral. cancels. A density integrates to 1, and .
In words: the average ratio, weighted by the model, is exactly 1. A bowl-shaped penalty averaged around 1 cannot fall below its value at 1, which is 0. That is why the coral curve can go negative locally while the total stays non-negative.
One template, four choices of
| Name | Resulting divergence | Behaviour | |
|---|---|---|---|
| Forward KL | Punishes the model for missing real data. Mode-covering. This is the blurry blob in Section 2, and it is maximum likelihood. | ||
| Reverse KL | Punishes the model for generating where the data is not. Mode-seeking. It sits on one hump. | ||
| Jensen–Shannon | symmetric, bounded by | What the original GAN minimises. | |
| Total variation | Symmetric, bounded by 1. |
Forward KL is the template with , and the cancels:
Reverse KL is the same template with . Substituting it recovers . The Jensen–Shannon above satisfies . A GAN writeup often uses , which differs from this one by a constant shift and is why that loss is described as similar to Jensen–Shannon rather than identical. Total variation is , which is the choice that turns the template into .
A worked example small enough to do by hand
Two outcomes. , a fair coin. , a biased model.
The ratios are (over-produced) and (under-produced). The template on a finite set is .
| Divergence | Computation | Value |
|---|---|---|
| Forward KL | ||
| Reverse KL | ||
| Jensen–Shannon | ||
| Total variation |
All four are positive, and they disagree on the size of the gap. Forward KL is not reverse KL, so the score is not symmetric. In the forward-KL row, outcome 1 contributes about and outcome 2 about . Negative pieces, positive total. That is Jensen. If , every ratio is 1 and every row is 0.
The catch, which is Section 3b
Computing needs the values and .
- is unknown. Section 1 only gave me samples.
- is implicit. Section 2 lets me sample , not evaluate a density.
So I cannot form the ratio . What I can form are averages over samples, by the law of large numbers. Section 3b has to rewrite as expectations under and under , with no density values in the formula. The convex conjugate is the tool. The result is the GAN objective.
Where this sits in the lectures
- This note answers “which divergence?” with a menu, not a single choice.
- A GAN picks the Jensen–Shannon-like .
- VAEs, diffusion, and autoregressive models minimise forward KL, which is maximum likelihood.
- Bounded scores such as Jensen–Shannon saturate when and do not overlap. Wasserstein, which is not an f-divergence, is the later answer to that.
- The KL term that keeps an aligned model close to a reference model, , is this same family.
Questions I want to be able to answer:
- 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?