Skip to content
DineshKumar Sarangapani
All writing

Section 4a: From the variational bound to a trainable min-max game

Turn Week 1's abstract sup over functions T into a neural-network critic T_w, creating the two-player saddle-point game J(theta, w).

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 3b: The convex conjugate and the variational bound.

Week 2 roadmap

Week 2 picks up right where Week 1 stopped. Here is how the four sections of Week 2 fit together:

SectionTopic
4a (this note)From the Week 1 lower bound to a trainable min–max game (generator vs critic)
4bThe critic’s output activation σf\sigma_f and dom⁡f∗\operatorname{dom} f^*
4cGAN as a special case: picking one particular ff gives the familiar GAN loss
4dTraining a GAN in practice: mini-batches and alternating updates

Prerequisites for this note (detours below): Week 1’s lower bound on DfD_f, “an average is approximately an expectation”, and what a saddle point is.

Symbol cheat-sheet

SymbolSay it asIn plain words
px(x)p_x(x)”p-x of x”how likely real data is to land near xx
pθ(x)p_\theta(x)“p-theta of x”how likely the generator model is to produce xx
gθ(z)g_\theta(z)“g-theta of z”the generator neural network with weights θ\theta, taking noise zz and outputting a fake sample x^\hat{x}
T(x)T(x)“T of x”a scoring function (critic) that takes one data point xx and returns one number
T\mathcal{T}”script T”the menu (family) of scoring functions we are allowed to pick from
Tw(x)T_w(x)”T-w of x”a neural-network critic with weights ww
ww“w”the critic network’s adjustable weights
f∗(t)f^*(t)”f-star of t”the convex conjugate of ff: a fixed penalty transform determined by which divergence we chose
J(θ,w)J(\theta, w)”J of theta and w”the single game score: how well critic ww separates real data from fakes made by generator θ\theta
max⁡T∈T\max_{T \in \mathcal{T}}“max over T in script-T”try every function TT in the family T\mathcal{T} and keep the one giving the biggest value
arg⁡min⁡θ\arg\min_\theta”arg-min over theta”the setting of θ\theta that makes the expression smallest
≈\approx”approximately equals”close to, but not exact (because we minimise a lower bound)

Section 4a: From the variational bound to a trainable min-max game

1. Intuition first

In Week 1 we wanted to make our model distribution pθp_\theta match the real data distribution pxp_x by shrinking a distance between them (the f-divergence DfD_f). The problem: we cannot compute that distance directly, because we do not know either density formula. We only have samples.

Week 1 ended with a workaround: a lower bound on DfD_f that only needs averages over samples, but it contains a “maximise over all functions TT” step. This section answers: how do you actually maximise over “all functions” on a computer? Answer: use a neural network for TT, and you get a two-player game.

2. Real-world analogy

Think of a fraud-detection team vs. a fraudster.

  • The fraudster (the generator, gθg_\theta) produces fake transactions.
  • The fraud team (the critic, TwT_w) builds a scoring model that gives real transactions high scores and fakes low scores.
  • The quality of the fraud team’s best scoring model is a measure of how different fakes still are from real ones. If even the best detector cannot separate them, the fakes are indistinguishable from real traffic.
  • The fraudster improves the fakes to make even the best detector useless.

That is the exact mathematical structure: the critic maximises a separation score, and the generator minimises that best-achievable score.

3. Visual

The critic only ever sees samples (real ones and fake ones), and the generator only ever sees noise. All the learning signal flows through the single game score J(θ,w)J(\theta, w).

Things to watch in the widget:

  • Move the critic slider ww: the blue curve shows J(θ,w)J(\theta, w) as a hill over ww. The coral dot is the current critic’s score J(θ,w)J(\theta, w), which always sits at or below the dashed green ceiling DKL(px ∥ pθ)D_{KL}(p_x \,\|\, p_\theta).
  • Click “Snap critic to w∗(θ)w^*(\theta)”: the critic climbs to the top of the hill, where the lower bound touches the true KL divergence.
  • Move the generator slider θ\theta toward 0.500.50: the entire hill sinks down until its peak hits 0 at the saddle point (θ∗=0.50,w∗=0)(\theta^* = 0.50, w^* = 0).

4. The formulas, decoded

Before we start, here is a quick recap of the Week 1 ingredient this all rests on.

Detour 1: The Week 1 lower bound (what f∗f^* and TT are)

In Week 1, the f-divergence was defined as

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 from pxp_x to pθp_\theta is: at every point xx, take the ratio of real density to model density, push it through the function ff, weight it by how often the model visits xx, and add up over all xx.”

In plain words: it is a model-weighted measure of how far the ratio real/model\text{real}/\text{model} is from 1, on average.

The trouble is that this formula needs the density formulas for pxp_x and pθp_\theta, which we do not have. Week 1’s trick was the convex conjugate f∗f^*: every convex ff can be rewritten as “the best of many straight lines”,

f(u)=max⁡t [ t u−f∗(t) ]f(u) = \max_t \,\big[\,t\,u - f^*(t)\,\big]

Read aloud: ”ff of uu equals the maximum over tt of tt times uu minus ff-star of tt.”

In plain words: any bowl-shaped curve equals the upper envelope of its tangent lines. Plugging that into DfD_f and swapping the order gave an expression that only contains averages, which we can estimate from samples. The price: we have to choose a slope tt for every xx — a whole function T(x)T(x) — and we only get a lower bound unless we pick TT perfectly.

Back to the main thread. That brings us to the first equation of Week 2.

Formula 1: The lower bound

Df(px ∥ pθ)  ≥  max⁡T∈T  [ Ex∼px[T(x)]  −  Ex∼pθ[f∗(T(x))] ]D_f(p_x \,\|\, p_\theta) \;\ge\; \max_{T \in \mathcal{T}}\; \Big[\, \mathbb{E}_{x\sim p_x}[T(x)] \;-\; \mathbb{E}_{x\sim p_\theta}\big[f^*(T(x))\big] \,\Big]

Read aloud: “The divergence between the real distribution and the model is at least as big as the best value you can get, over all functions TT in some family T\mathcal{T}, of: the average of TT on real samples, minus the average of f∗f^*-of-TT on model samples.”

Piece by piece:

SymbolEveryday meaningType / shape
≥\ge”is at least”comparison
max⁡T∈T\max_{T\in\mathcal{T}}”try every function TT in the family T\mathcal{T} and keep the one giving the biggest value”optimisation over functions
T(x)T(x)a scoring function: takes one data point, returns one numberfunction from data space X\mathcal{X} (e.g. Rd\mathbb{R}^d) to R\mathbb{R}
T\mathcal{T}the “menu” of scoring functions we are allowed to pick froma set of functions
Ex∼px[⋅]\mathbb{E}_{x\sim p_x}[\cdot]”the average value you would get if you drew many xx‘s from the real data”a single number
Ex∼pθ[⋅]\mathbb{E}_{x\sim p_\theta}[\cdot]same, but drawing xx from the model (generated samples)a single number
f∗f^*the convex conjugate of ff: a fixed penalty transform that depends on which divergence you chosefunction from a number to a number

In plain words: reward TT for giving high scores to real data, penalise it (through f∗f^*) for giving high scores to fake data; the best possible net score is a guaranteed underestimate of how different the two distributions are.

Why it is a lower bound and not an equality: equality only holds if the menu T\mathcal{T} contains the perfect scorer, which Section 3b showed is

T∗(x)=f′ ⁣(px(x)pθ(x))T^*(x) = f'\!\left(\frac{p_x(x)}{p_\theta(x)}\right)

Read aloud: “T-star at xx equals ff-prime evaluated at the ratio of px(x)p_x(x) to pθ(x)p_\theta(x).”

In plain words: the ideal critic outputs the slope of ff at the true density ratio. A smaller menu T\mathcal{T} can only do worse, so the max can only be smaller.

Formula 2: The training objective

We want θ∗=arg⁡min⁡θDf(px ∥ pθ)\theta^* = \arg\min_\theta D_f(p_x \,\|\, p_\theta). Since we cannot compute DfD_f, we swap it for its lower bound:

θ∗  ≈  arg⁡min⁡θ  max⁡T∈T[ Epx[T(x)]−Epθ[f∗(T(x))] ]\theta^* \;\approx\; \arg\min_{\theta}\; \max_{T\in\mathcal{T}}\Big[\,\mathbb{E}_{p_x}[T(x)] - \mathbb{E}_{p_\theta}\big[f^*(T(x))\big]\,\Big]

Read aloud: “The best generator parameters θ∗\theta^* are approximately those that minimise, over θ\theta, the best score any critic TT in T\mathcal{T} can achieve.”

  • The outer min is over θ\theta, the weights of the generator gθ(z)g_\theta(z).
  • The inner max is over TT, a class of critic functions T(x)∈TT(x) \in \mathcal{T}.
  • The ≈\approx matters: we are minimising a lower bound, not the true divergence. If the critic family T\mathcal{T} is weak, the bound is loose, and the generator may appear to have low loss even when its samples are poor.

In plain words: pick the generator such that even the best possible critic can barely tell real from fake.

Formula 3: Making “max over all functions” computable

“Max over a set of functions” is not something a computer can execute directly. The fix: let the menu T\mathcal{T} be every function a particular neural network architecture can represent. A network Tw(x)T_w(x) with weights ww is a function; changing ww changes the function. So “search over functions” becomes “search over weights ww”, which gradient ascent can do:

θ∗, w∗  =  arg⁡min⁡θ  max⁡w  [ Ex∼px[Tw(x)]  −  Ex∼pθ[f∗(Tw(x))] ]\theta^*,\, w^* \;=\; \arg\min_{\theta}\;\max_{w}\;\Big[\,\mathbb{E}_{x\sim p_x}[T_w(x)] \;-\; \mathbb{E}_{x\sim p_\theta}\big[f^*(T_w(x))\big]\,\Big]

Read aloud: “Find the generator weights θ∗\theta^* and critic weights w∗w^* such that θ\theta minimises, and ww maximises, the critic’s net score.”

In plain words: replace the abstract function search with a neural network TwT_w, so both players are just tuning network weights.

The lectures name the bracket J(θ,w)J(\theta, w):

J(θ,w)=Ex∼px[Tw(x)]−Ex∼pθ[f∗(Tw(x))]J(\theta, w) = \mathbb{E}_{x\sim p_x}[T_w(x)] - \mathbb{E}_{x\sim p_\theta}\big[f^*(T_w(x))\big]

Read aloud: “J of theta and w equals the expected value of Tw(x)T_w(x) under pxp_x minus the expected value of f∗(Tw(x))f^*(T_w(x)) under pθp_\theta.”

In plain words: J(θ,w)J(\theta, w) is one single number measuring how well critic ww separates real data from fake data produced by generator θ\theta.

One step the slides leave implicit — and the reason gradients can reach θ\theta — is how θ\theta enters the second average. Because fake samples are made as x^=gθ(z)\hat{x} = g_\theta(z) with z∼N(0,I)z \sim \mathcal{N}(0, I), we rewrite the second term as:

Ex∼pθ[f∗(Tw(x))]=Ez∼N(0,I)[f∗(Tw(gθ(z)))]\mathbb{E}_{x\sim p_\theta}\big[f^*(T_w(x))\big] = \mathbb{E}_{z\sim\mathcal{N}(0,I)}\big[f^*\big(T_w(g_\theta(z))\big)\big]

Read aloud: “the expected value of f∗(Tw(x))f^*(T_w(x)) for xx drawn from pθp_\theta equals the expected value of f∗(Tw(gθ(z)))f^*(T_w(g_\theta(z))) for noise zz drawn from a standard normal.”

In plain words: averaging over model samples is the exact same thing as drawing random noise vectors zz, running each one through the generator gθ(z)g_\theta(z), and averaging the critic’s penalty on the resulting fakes. We never need a density formula for pθp_\theta.

Saddle point and adversarial training

Detour 2: What is a saddle point?

Picture a horse saddle (or a Pringle crisp). Walking front-to-back, the centre is the lowest point; walking side-to-side, it is the highest point. A point that is a minimum in one direction and a maximum in another is a saddle point:

J(θ∗,w)  ≤  J(θ∗,w∗)  ≤  J(θ,w∗)J(\theta^*, w) \;\le\; J(\theta^*, w^*) \;\le\; J(\theta, w^*)

Read aloud: ”JJ at (θ∗,w)(\theta^*, w) is at most JJ at (θ∗,w∗)(\theta^*, w^*), which in turn is at most JJ at (θ,w∗)(\theta, w^*).”

In plain words: at the saddle point (θ∗,w∗)(\theta^*, w^*), if the critic changes ww, the score JJ cannot go up (the critic is already doing its best), and if the generator changes θ\theta, the score JJ cannot go down (the generator is already doing its best). Neither player wants to move.

Back to the main thread. That is why the lectures call this saddle-point optimisation and an adversarial problem: two players pulling one single number J(θ,w)J(\theta, w) in opposite directions.

FeatureOrdinary model trainingVDM / GAN training
PlayersOne networkTwo networks (generator θ\theta, critic ww)
GoalMinimise a single lossmin⁡θmax⁡wJ(θ,w)\min_\theta \max_w J(\theta, w)
Solution geometryBottom of a bowl (minimum)Saddle point (min in θ\theta, max in ww)
Training stabilityUsually steady descentCan oscillate because each player’s target moves

5. Tiny worked example

Use the forward KL divergence from Week 1: f(u)=uln⁡uf(u) = u\ln u, whose conjugate is f∗(t)=et−1f^*(t) = e^{t-1}. Data lives on two points, aa and bb:

Outcomepxp_x (real)pθp_\theta (model)
aa0.50.50.80.8
bb0.50.50.20.2

First, the true KL divergence:

DKL(px ∥ pθ)=0.5ln⁡ ⁣(0.50.8)+0.5ln⁡ ⁣(0.50.2)=0.5(−0.470)+0.5(0.916)≈0.223D_{KL}(p_x \,\|\, p_\theta) = 0.5\ln\!\left(\frac{0.5}{0.8}\right) + 0.5\ln\!\left(\frac{0.5}{0.2}\right) = 0.5(-0.470) + 0.5(0.916) \approx 0.223

Read aloud: “0.5 times natural log of 0.5 over 0.8, plus 0.5 times natural log of 0.5 over 0.2, equals approximately 0.223.”

In plain words: the true KL gap between these two coins is 0.2230.223.

Now let three different critics try. For each critic,

J(θ,w)=(0.5 T(a)+0.5 T(b))−(0.8 eT(a)−1+0.2 eT(b)−1)J(\theta, w) = \Big(0.5\,T(a) + 0.5\,T(b)\Big) - \Big(0.8\,e^{T(a)-1} + 0.2\,e^{T(b)-1}\Big)

Read aloud: ”JJ equals the real average of TT minus the model average of eT−1e^{T-1}.”

In plain words: average score on real coin flips minus average exponential penalty on model coin flips.

CriticT(a)T(a)T(b)T(b)Avg TT on realAvg f∗(T)f^*(T) on modelJ(θ,w)J(\theta, w)
Lazy (constant)1.01.01.01.01.0001.0000.8(1)+0.2(1)=1.0000.8(1) + 0.2(1) = 1.0000.0000.000
Decent0.80.81.51.51.1501.1500.8(0.819)+0.2(1.649)=0.9850.8(0.819) + 0.2(1.649) = 0.9850.1650.165
Perfect: T∗=1+ln⁡(px/pθ)T^* = 1 + \ln(p_x/p_\theta)0.5300.5301.9161.9161.2231.2230.8(0.625)+0.2(2.5)=1.0000.8(0.625) + 0.2(2.5) = 1.0000.2230.223

What the table shows:

  • Every critic gives J≤0.223J \le 0.223 (matching the "≥\ge" in Formula 1), and only the optimal critic hits 0.2230.223.
  • The good critic scores bb higher, because bb is where real data is under-produced by the model (0.50.5 vs 0.20.2). That is the critic pointing at what the generator gets wrong.
  • Now the generator moves: if θ\theta changes so pθ=(0.5,0.5)p_\theta = (0.5, 0.5), the true KL drops to 00, and the best any critic can achieve is J=0J = 0. The generator wins when no critic can score above zero.

In practice, those averages come from mini-batch samples: for instance, four real draws {a,a,b,b}\{a, a, b, b\} give sample average (0.530+0.530+1.916+1.916)/4=1.223(0.530 + 0.530 + 1.916 + 1.916)/4 = 1.223, matching the exact expectation by the Law of Large Numbers.

6. Connect back

  • Week 1 (Section 3b) gave the variational lower bound with an abstract sup⁡T\sup_T.
  • This note (Section 4a) makes it concrete: TT becomes a neural network TwT_w, and training becomes the two-player saddle-point game min⁡θmax⁡wJ(θ,w)\min_\theta \max_w J(\theta, w). This general recipe is called f-GAN or Variational Divergence Minimisation (VDM).
  • Next (Section 4b): TwT_w must output numbers that lie inside dom⁡f∗\operatorname{dom} f^* (since some f∗f^* only accept restricted ranges), which we enforce with an output activation σf\sigma_f.
  • Section 4c: picking one specific ff turns J(θ,w)J(\theta, w) into the classic GAN loss E[log⁡D(x)]+E[log⁡(1−D(gθ(z)))]\mathbb{E}[\log D(x)] + \mathbb{E}[\log(1 - D(g_\theta(z)))].
  • Week 6 (WGAN): replaces f-divergences with the Wasserstein distance while keeping this exact generator-vs-critic game shape.

Quick check (optional)

  1. Why can minimising J(θ,w)J(\theta, w) over θ\theta fool you into thinking the generator is good, even when it is not?
  2. In J(θ,w)J(\theta, w), which of the two averages actually depends on θ\theta, and how does θ\theta get in there?
  3. If the critic network were tiny (say, it could only output constants Tw(x)=cT_w(x) = c), what would max⁡wJ(θ,w)\max_w J(\theta, w) always equal, and what would that mean for training?