Skip to content
DineshKumar Sarangapani
All writing

Section 2: The general principle of generative models

Pick a parametric family, define a divergence, and minimise it using only samples.

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 1: The dataset and the problem.

Symbol cheat-sheet

SymbolSay it asIn plain words
zz”z”a random input vector (noise seed) fed into the generator
Z\mathcal{Z}”script Z”the space of possible noise seeds (usually Rk\mathbb{R}^k)
X\mathcal{X}“script X”the space of possible data points (usually Rd\mathbb{R}^d)
k≪dk \ll d“k much smaller than d”the noise seed has far fewer numbers than a full image
N(μ,σ2)\mathcal{N}(\mu, \sigma^2)”normal with mean mu and variance sigma-squared”a 1-D bell curve centred at μ\mu with spread σ\sigma
N(0,I)\mathcal{N}(0, I)“standard normal”a bell curve centred at zero with spread 1 in every coordinate, and no correlation between coordinates
II”capital I (identity matrix)“a grid of 1s on the diagonal and 0s elsewhere
gθ:Z→Xg_\theta : \mathcal{Z} \to \mathcal{X}”g-theta maps script-Z to script-X”a neural network with weights θ\theta that takes noise zz and outputs a synthetic data point xx
θ\theta“theta”the adjustable knobs (weights) of the generator network
θ∗\theta^*”theta-star”the best knob setting found after training
PθP_\theta”P-theta”the distribution of outputs produced when random noise is pushed through gθg_\theta
:=:=“is defined as”this symbol on the left is shorthand for the thing on the right
D(Px ∥ Pθ)\mathcal{D}(P_x \,\|\, P_\theta)”divergence of P-x from P-theta”a non-negative score measuring how different the real distribution is from the model distribution
min⁡θ\min_\theta”minimum over theta”the smallest score achievable by tuning θ\theta
arg⁡min⁡θ\arg\min_\theta“arg-min over theta”the knob setting θ\theta that achieves that smallest score
  ⟺  \iff”if and only if”each side guarantees the other

Section 2: The general principle of generative models

Section 1 left me with a vague goal: estimate PxP_x and learn to sample from it. This section turns that goal into a three-step recipe. Every later model in the lectures is this recipe with different choices plugged in.

The statement I am carrying:

(i) Assume a parametric family on PxP_x, denoted PθP_\theta, represented using deep neural networks. (ii) Define and estimate a divergence between PxP_x and PθP_\theta. (iii) Solve an optimisation problem over the parameters of PθP_\theta to minimise that divergence.

The concrete version: draw z∼N(0,I)z \sim \mathcal{N}(0, I), pass it through a network gθ:Z→Xg_\theta : \mathcal{Z} \to \mathcal{X}, and call the distribution of x=gθ(z)x = g_\theta(z) by the name PθP_\theta. Then solve

θ∗=arg⁡min⁡θ  D(Px ∥ Pθ)\theta^* = \arg\min_\theta \; \mathcal{D}(P_x \,\|\, P_\theta)

Read aloud: “theta-star equals the arg-min over theta of the divergence between PxP_x and PθP_\theta.”

In plain words: find the network weights θ∗\theta^* that make the gap between the real data distribution and the model’s output distribution as small as possible.

After that, sampling z∼N(0,I)z \sim \mathcal{N}(0, I) and computing gθ∗(z)g_{\theta^*}(z) gives samples from Pθ∗P_{\theta^*}, which is as close to PxP_x as this family could get.

1. Intuition first

I cannot see PxP_x. So I build a machine with knobs, θ\theta, that turns plain random noise into data-shaped outputs. I keep turning the knobs until the machine’s output distribution looks as close as possible to the real data. The “how close” score is a divergence. The knob-turning is optimisation.

2. Real-world analogy

The traffic generator from Section 1 takes random seeds and outputs synthetic requests. It has configuration knobs: endpoint weights, payload-size parameters, and so on.

Load-testing analogyNotationIn plain words
Random seed fed into the generatorz∼N(0,I)z \sim \mathcal{N}(0, I)a fresh roll of standard random numbers
The generator codegθg_\thetathe neural network that turns seeds into outputs
Its config knobsθ\thetathe network weights we can tune
The distribution of traffic it producesPθP_\thetawhat the generator’s outputs look like overall
Score of how different synthetic traffic is from real logsD(Px ∥ Pθ)\mathcal{D}(P_x \,\|\, P_\theta)the mismatch score (0 means identical)
Tuning knobs until that score is near zeroθ∗=arg⁡min⁡θD\theta^* = \arg\min_\theta \mathcal{D}training the model

The hard step is computing the score: I cannot see the true user-behaviour formula, only the access log. That difficulty drives the rest of Week 1.

3. Visual

One dimension is enough to see the recipe. The noise is z∼N(0,1)z \sim \mathcal{N}(0, 1). The network is the simplest one I can write, gθ(z)=az+bg_\theta(z) = a z + b, so θ=(a,b)\theta = (a, b). The green curve is the target PxP_x. The blue curve is PθP_\theta, what this generator can produce.

Three things to watch while moving the sliders:

  1. Single bump: a=0.6a = 0.6 and b=2b = 2 drive the divergence to 0. The target is a bell centred at 2 with spread 0.6, and x=0.6z+2x = 0.6z + 2 produces that exact bell.
  2. Two bumps: the divergence never reaches 0 for any (a,b)(a, b). The closest blue curve is one wide bell stretched over both humps. A linear gθg_\theta can only shift and stretch a single bell; it can never split it into two. That is why we need a deep, non-linear neural network.
  3. Why this widget is cheating: it draws the green curve and computes the divergence directly because the target formula was hardcoded into the demo. In real life we only get samples.

4. The formula, decoded

θ∗=arg⁡min⁡θ  D(Px ∥ Pθ),Pθ:=distribution of gθ(z),  z∼N(0,I)\theta^* = \arg\min_\theta \; \mathcal{D}\big(P_x \,\|\, P_\theta\big), \qquad P_\theta := \text{distribution of } g_\theta(z),\; z \sim \mathcal{N}(0, I)

Read aloud: “theta-star equals the arg-min over theta of the divergence of PxP_x from PθP_\theta, where PθP_\theta is defined as the distribution of gθ(z)g_\theta(z) when zz is drawn from a standard normal.”

In plain words: draw standard random noise zz, push it through the network gθg_\theta, and adjust the weights θ\theta until the resulting distribution PθP_\theta is as close as possible to the real data distribution PxP_x.

Piece by piece:

SymbolWhat it isType / shapeIn plain words
zzinput noisevector in Rk\mathbb{R}^k, usually k≪dk \ll dthe random seed
N(0,I)\mathcal{N}(0, I)standard Gaussiandistribution on Rk\mathbb{R}^kan easy-to-sample source of randomness
gθg_\thetageneratorfunction Rk→Rd\mathbb{R}^k \to \mathbb{R}^d (neural net)reshapes noise into data
θ\thetagenerator parametersvector of weightsthe knobs we tune during training
PθP_\thetamodel distributiondistribution on Rd\mathbb{R}^d, implicitwhat the generator’s outputs look like as a whole
PxP_xtrue distributiondistribution on Rd\mathbb{R}^d, unknownthe target we want to match
D(⋅ ∥ ⋅)\mathcal{D}(\cdot \,\|\, \cdot)divergencemaps two distributions to a number ≥0\ge 0the “how far apart” score
θ∗\theta^*optimal parameterssame shape as θ\thetathe trained generator weights

Detour 1: The standard Gaussian N(0,I)\mathcal{N}(0, I)

In one dimension, N(μ,σ2)\mathcal{N}(\mu, \sigma^2) is the familiar bell curve centred at μ\mu with spread σ\sigma. “Standard” means centred at zero with unit spread:

z∼N(0,1)z \sim \mathcal{N}(0, 1)

Read aloud: ”zz is drawn from a normal distribution with mean 0 and variance 1.”

In plain words: zz is a single random number that usually lands near 0 (mostly between −2-2 and +2+2).

In kk dimensions, N(0,I)\mathcal{N}(0, I) stacks kk of those independent standard bells into a vector:

z=(z1,z2,…,zk)∼N(0,I)z = (z_1, z_2, \dots, z_k) \sim \mathcal{N}(0, I)

Read aloud: “the vector zz is drawn from a kk-dimensional standard normal distribution.”

In plain words: roll kk independent standard bell-curve numbers and pack them into a list. We use it as input noise because every software library can sample it instantly, and it has no built-in image structure — any structure in gθ(z)g_\theta(z) had to be learned by gθg_\theta.

Back to the main thread. z∼N(0,I)z \sim \mathcal{N}(0, I) is our raw clay; gθg_\theta is the sculptor.

Detour 2: Pushing a random variable through a function

If zz is random and you compute x=g(z)x = g(z), then xx is random too, with a new distribution shaped by gg.

In the 1-D linear case:

z∼N(0,1)    ⟹    x=az+b∼N(b,a2)z \sim \mathcal{N}(0, 1) \;\;\Longrightarrow\;\; x = a z + b \sim \mathcal{N}(b, a^2)

Read aloud: “if zz is standard normal, then x=az+bx = a z + b follows a normal distribution with mean bb and variance a2a^2.”

In plain words: multiplying by aa stretches the bell’s width by ∣a∣\lvert a \rvert, and adding bb slides its centre to bb. That is what the two sliders in the widget do.

A deep, non-linear network gθg_\theta can bend and fold the bell into multiple humps or into the thin space of human faces. We call that resulting output distribution PθP_\theta. For a deep network we almost never have a closed-form formula for Pθ(x)P_\theta(x) — we only know how to sample from it (draw zz, run gθ(z)g_\theta(z)).

Back to the main thread. Because PθP_\theta has no formula we can evaluate, we call it an implicit distribution.

Detour 3: Divergence (weaker than a distance)

A divergence D(P ∥ Q)\mathcal{D}(P \,\|\, Q) scores how different two distributions are. It must obey two rules:

D(Px ∥ Pθ)≥0,D(Px ∥ Pθ)=0  ⟺  Px=Pθ\mathcal{D}(P_x \,\|\, P_\theta) \ge 0, \qquad \mathcal{D}(P_x \,\|\, P_\theta) = 0 \iff P_x = P_\theta

Read aloud: “the divergence of PxP_x from PθP_\theta is at least zero, and it equals zero if and only if PxP_x equals PθP_\theta.”

In plain words: the score can never be negative, and it hits zero only when the model matches reality everywhere.

Unlike a ruler distance, a divergence does not have to be symmetric:

D(P ∥ Q)≠D(Q ∥ P)in general\mathcal{D}(P \,\|\, Q) \ne \mathcal{D}(Q \,\|\, P) \quad \text{in general}

Read aloud: “the divergence of PP from QQ is not necessarily equal to the divergence of QQ from PP.”

In plain words: order matters, which is why we write a double bar ∥\| instead of a comma.

Back to the main thread. Those two rules make “minimise D(Px ∥ Pθ)\mathcal{D}(P_x \,\|\, P_\theta)” a clean training objective: the floor is 0, and hitting the floor means we solved the problem.

Detour 4: min⁡\min vs arg⁡min⁡\arg\min

For a bowl-shaped function like f(θ)=(θ−3)2+1f(\theta) = (\theta - 3)^2 + 1:

min⁡θ[(θ−3)2+1]=1,arg⁡min⁡θ[(θ−3)2+1]=3\min_\theta \big[(\theta - 3)^2 + 1\big] = 1, \qquad \arg\min_\theta \big[(\theta - 3)^2 + 1\big] = 3

Read aloud: “the minimum over theta of (θ−3)2+1(\theta - 3)^2 + 1 is 1, while the arg-min over theta of (θ−3)2+1(\theta - 3)^2 + 1 is 3.”

In plain words: min⁡\min asks “how low does the curve go?” (answer: height 1), whereas arg⁡min⁡\arg\min asks “what knob setting θ\theta puts me at that lowest point?” (answer: θ=3\theta = 3).

Back to the main thread. At the end of training we want to keep the trained weights θ∗\theta^*, so the principle writes θ∗=arg⁡min⁡θ\theta^* = \arg\min_\theta.

5. Tiny worked example

First, see how gθg_\theta pushes noise into outputs. Take k=d=1k = d = 1 and gθ(z)=2z+3g_\theta(z) = 2z + 3, so θ=(a,b)=(2,3)\theta = (a, b) = (2, 3):

z∈{−1,0,1}    ⟹    gθ(z)∈{1,3,5}z \in \{-1, 0, 1\} \;\;\Longrightarrow\;\; g_\theta(z) \in \{1, 3, 5\}

Read aloud: “feeding inputs −1,0,1-1, 0, 1 into gθg_\theta produces outputs 1,3,51, 3, 5.”

In plain words: the network stretches the spacing by 2 and shifts the centre to 3, so standard normal draws become draws from N(3,22)\mathcal{N}(3, 2^2).

Second, see how a divergence scores different models on a two-outcome coin world. Let the real coin be fair, Px=(0.5,0.5)P_x = (0.5, 0.5), and score candidate models Pθ=(p1,p2)P_\theta = (p_1, p_2) with the KL divergence (derived in Section 3a):

DKL(Px ∥ Pθ)=0.5ln⁡0.5p1+0.5ln⁡0.5p2D_{KL}(P_x \,\|\, P_\theta) = 0.5 \ln\frac{0.5}{p_1} + 0.5 \ln\frac{0.5}{p_2}

Read aloud: “the KL divergence is 0.5 times the natural log of 0.5 over p1p_1, plus 0.5 times the natural log of 0.5 over p2p_2.”

In plain words: average the log mismatch across the two coin faces.

Candidate model PθP_\thetaCalculationDivergence scoreIn plain words
(0.9,0.1)(0.9, 0.1)0.5ln⁡(0.5/0.9)+0.5ln⁡(0.5/0.1)=0.5(−0.588)+0.5(1.609)0.5\ln(0.5/0.9) + 0.5\ln(0.5/0.1) = 0.5(-0.588) + 0.5(1.609)≈0.511\approx 0.511heavily biased coin; high mismatch score
(0.6,0.4)(0.6, 0.4)0.5ln⁡(0.5/0.6)+0.5ln⁡(0.5/0.4)=0.5(−0.182)+0.5(0.223)0.5\ln(0.5/0.6) + 0.5\ln(0.5/0.4) = 0.5(-0.182) + 0.5(0.223)≈0.020\approx 0.020nearly fair coin; score drops close to zero
(0.5,0.5)(0.5, 0.5)0.5ln⁡(1)+0.5ln⁡(1)=0+00.5\ln(1) + 0.5\ln(1) = 0 + 000exact match; score hits its floor of zero

Optimisation is nudging θ\theta so the model moves down this table toward 0.

Four open questions this recipe leaves behind

Open questionWhy it is hardWhere the lectures answer it
(i) How do I compute D(Px ∥ Pθ)\mathcal{D}(P_x \,\|\, P_\theta) without formulas for either distribution?I only have real samples DD and generated samples gθ(z)g_\theta(z)Section 3b: rewrite the divergence as expectations and estimate with sample averages
(ii) Which divergence D\mathcal{D} should I pick?Different divergences punish different mistakes (e.g. one wide bell vs one hump)Section 3a (f-divergences) and Week 6 (Wasserstein)
(iii) How do I choose the architecture for gθg_\theta / PθP_\theta?Must be expressive enough for images/text, yet still trainableGANs, VAEs, diffusion, transformers
(iv) How do I solve arg⁡min⁡θ\arg\min_\theta?Millions of weights, often inside a min-max gameAlternating gradient descent (Week 2 onward)

6. Connect back

Every major model family is a row in this table:

ModelChoice of PθP_\thetaDivergence D\mathcal{D}Optimisation
GANgθ(z)g_\theta(z), implicitJensen–Shannon (an f-divergence)min-max game with a discriminator
WGANgθ(z)g_\theta(z), implicitWasserstein distancemin-max game with a 1-Lipschitz critic
VAE∫Pθ(x∣z) p(z) dz\int P_\theta(x \mid z)\, p(z)\, dzForward KL (via the ELBO lower bound)gradient ascent on the ELBO
DDPMreverse chain of Gaussian stepsForward KL (via the ELBO lower bound)regression on the added noise
Autoregressive / Transformer∏tPθ(xt∣x<t)\prod_t P_\theta(x_t \mid x_{<t})Forward KL (maximum likelihood)cross-entropy loss

One identity links the bottom three rows:

arg⁡min⁡θ  DKL(Px ∥ Pθ)  =  arg⁡max⁡θ  Ex∼Px[log⁡Pθ(x)]\arg\min_\theta \; D_{KL}(P_x \,\|\, P_\theta) \;=\; \arg\max_\theta \; \mathbb{E}_{x \sim P_x}\big[\log P_\theta(x)\big]

Read aloud: “the theta that minimises the KL divergence from PxP_x to PθP_\theta is the exact same theta that maximises the expected log probability of real data under the model.”

In plain words: shrinking forward KL is identical to maximum likelihood — making real data look as probable as possible under your model.

Quick check (optional)

  1. Why can no setting of (a,b)(a, b) in gθ(z)=az+bg_\theta(z) = az + b match the two-bump target, and what has to change about gθg_\theta?
  2. After training finishes and gives you θ∗\theta^*, how do you produce 10 new samples?
  3. If D(Px ∥ Pθ)=0\mathcal{D}(P_x \,\|\, P_\theta) = 0, what follows? What if the divergence is small but not zero?

Next: Section 3a: f-divergences.