Course contents 01 / 34

01 / 34 · Foundations

What generative modeling is

Learning to produce new data from examples of an unknown distribution, how a computer turns uniform random numbers into samples, and what six families of models compute: the likelihood exactly, a lower bound on it, or nothing.

Prerequisites

Probability · Calculus

On this page 7

A classifier answers a question about an object: is this image a cat? A generative model produces the object itself: a new image of a cat that was not in the training set. The two tasks differ in shape. A classifier has one right answer per input. A request for “a cat on a sofa” has endless right answers, some more plausible than others, and the model has to produce them with the right frequencies.

The problem: examples of an unknown distribution#

We treat a data set as independent draws from some distribution:

x(1),…,x(N)∼pdata(x).x^{(1)}, \dots, x^{(N)} \sim p_{\text{data}}(x).

For images, xx is a vector of pixel values; for text, a sequence of tokens; for audio, a waveform. Nobody can write down the density of “photographs of cats”, so pdatap_{\text{data}} is unknown. All we have is the sample.

The goal is a model pθ(x)p_\theta(x) with parameters θ\theta such that pθ≈pdatap_\theta \approx p_{\text{data}}, and from which we can draw new samples x∼pθx \sim p_\theta. If the approximation is good, the new samples look like the data without copying it.

Figure

Generative modeling: from examples to a model and back to examples

Data, a mixture model and new samplesThree panels. First: a cloud of points shaped as two interleaved half-moons, labelled p_data is unknown. An arrow labelled fit theta leads to the second: 16 pairs of concentric rings along the same half-moons, the components of a Gaussian mixture. An arrow labelled sample leads to the third: a new cloud of points with the same shape.datax⁽¹⁾, …, x⁽ᴺ⁾ ~ pdatamodelpθ(x) ≈ pdata(x)new samplesx ~ pθpdata is unknownfit θsampleData, a mixture model and new samplesThree panels. First: a cloud of points shaped as two interleaved half-moons, labelled p_data is unknown. An arrow labelled fit theta leads to the second: 16 pairs of concentric rings along the same half-moons, the components of a Gaussian mixture. An arrow labelled sample leads to the third: a new cloud of points with the same shape.datax⁽¹⁾, …, x⁽ᴺ⁾ ~ pdatamodelpθ(x) ≈ pdata(x)new samplesx ~ pθpdata is unknownfit θsample
Left: 300 points from a distribution whose formula we do not know (two noisy half-circles). Middle: an illustrative model pθ, an equal mixture of 16 Gaussians with σ = 0.12 along the data, with 1σ and 2σ rings. Right: 300 new samples from it; they resemble the data without repeating any point.

Why not simply store the data and replay it? A model that puts probability 1/N1/N on each training point is the best possible fit to the sample: on a finite set of outcomes the average log-likelihood of QQ is minus the cross-entropy from the empirical distribution to QQ, which Gibbs’ inequality maximizes at that distribution itself. Every one of its samples is a copy.

For continuous data it is worse. Put a Gaussian bump of width ss on each of the NN training points in dd dimensions, qs(x)=1N∑jN(x; x(j),s2I)q_s(x) = \frac1N \sum_j \mathcal N\big(x;\, x^{(j)}, s^2\mathbf I\big). At a training point the sum is at least its own term:

qs(x(i))≥1N N(x(i); x(i),s2I)=1N (2πs2)−d/2  → s→0   ∞,\begin{aligned} q_s\big(x^{(i)}\big) &\ge \tfrac1N\, \mathcal N\big(x^{(i)};\, x^{(i)}, s^2\mathbf I\big) \\ &= \tfrac1N\, (2\pi s^2)^{-d/2} \;\xrightarrow{\,s \to 0\,}\; \infty , \end{aligned}

so the likelihood of the sample grows without bound as the bumps shrink, the unbounded-likelihood failure of mixtures. A useful model is restricted to a family that cannot memorize, and its quality is judged on data it has not seen (Kaiming He, MIT 6.S978, lecture 1; Stanford CS236 notes).

The classical guarantees of maximum likelihood, consistency and asymptotic efficiency, need conditions: the model family contains the true distribution and is identifiable, the log-likelihood is smooth, and the sample grows while the number of parameters stays fixed. The Probability course states them with the Fisher information. A network with more parameters than training images is far from that regime (BMML lecture notes, 2026, lecture 1).

Most applications want a conditional model p(x∣c)p(x \mid c): an image for a text prompt cc, speech for a sentence, a translation for a source sentence, a high-resolution image for a small one. The unconditional case is the same problem with an empty condition, and the course treats it first.

What a generative model is for#

Sampling new objects is the most visible use. A model that can also evaluate pθ(x)p_\theta(x) gives a density estimate: one possible anomaly score, since low density can flag unusual objects. The score depends on the representation, because a density changes under a change of variables, and a high density at a point does not mean that the point looks typical; the anomaly detection note works through these limits. A good density model is also a good compressor. A prefix code can give each discrete xx a codeword of ⌈−log⁡2p(x)⌉\lceil -\log_2 p(x) \rceil bits, because these lengths satisfy the Kraft inequality, ∑x2−⌈−log⁡2p(x)⌉≤∑xp(x)=1\sum_x 2^{-\lceil -\log_2 p(x) \rceil} \le \sum_x p(x) = 1, which is exactly the condition for such a code to exist. Each xx then costs less than −log⁡2p(x)+1-\log_2 p(x) + 1 bits, and no prefix code has a shorter average length than the entropy (Cover and Thomas, 2006, chapter 5).

Models with a latent variable, such as the VAE, also learn a representation: they map each object to a low-dimensional code whose directions often carry meaning, without labels. Not every family offers all three uses, and the trade-offs between them shape the rest of the course (Stanford CS236 notes).

What “approximate” means: a divergence to minimize#

To train pθp_\theta we need a number that says how far it is from pdatap_{\text{data}}. The standard choice is the Kullback–Leibler divergence (Entropy, cross-entropy and KL divergence):

DKL(pdata ∥ pθ)=Ex∼pdata[log⁡pdata(x)]−Ex∼pdata[log⁡pθ(x)].\begin{aligned} D_{\mathrm{KL}}(p_{\text{data}} \,\|\, p_\theta) &= \mathbb{E}_{x \sim p_{\text{data}}}\big[\log p_{\text{data}}(x)\big] \\ &\quad - \mathbb{E}_{x \sim p_{\text{data}}}\big[\log p_\theta(x)\big]. \end{aligned}

The first term does not depend on θ\theta. Minimizing the divergence therefore means maximizing the expected log-likelihood of the data under the model. With the sample in place of the expectation this is maximum likelihood:

θ⋆=arg⁡max⁡θ1N∑i=1Nlog⁡pθ(x(i)).\theta^\star = \arg\max_\theta \frac{1}{N}\sum_{i=1}^{N} \log p_\theta\big(x^{(i)}\big).

The catch is that this needs pθ(x)p_\theta(x) as a number. For flexible models it is often impossible to compute, and the families in this course differ mainly in how they get around that: an exact formula, a lower bound, or a different objective altogether (Isachenko, lecture 1). The fitting note runs this objective on the simplest case, a Gaussian fitted by gradient descent.

How a computer samples at all#

A model is only generative if we can draw from it. Computers do not draw from arbitrary distributions directly. Everything starts from one primitive, uniform random numbers, and every later step transforms the samples of an easy distribution into samples of a harder one. The Probability course surveys these techniques; here we follow the chain to the point where neural networks take over.

Step 1: uniform numbers#

A processor is deterministic, so programs get their random numbers from a pseudo-random generator. It keeps an internal state ss, updates it by a fixed rule sn+1=T(sn)s_{n+1} = T(s_n) and returns a number computed from the state. The starting state is set from a seed. The simplest rule is the linear congruential generator (Knuth, 1997, §3.2.1). A toy version with 16 states is

sn+1=(5 sn+3) mod 16,un=sn/16.\begin{aligned} s_{n+1} &= (5\,s_n + 3) \bmod 16, \\ u_n &= s_n / 16 . \end{aligned}

From the seed s0=7s_0 = 7 it visits 7,6,1,8,11,10,5,12,15,14,9,0,3,2,13,47, 6, 1, 8, 11, 10, 5, 12, 15, 14, 9, 0, 3, 2, 13, 4 and returns to 7. Over one cycle each of the 16 values 0,116,…,15160, \tfrac1{16}, \dots, \tfrac{15}{16} appears exactly once, which is what “uniform” means for a generator.

Figure

Where the "random" numbers come from

The cycle of a toy generator and pairs of consecutive numbersSixteen circles around a ring labelled with the states 7, 6, 1, 8, 11, 10, 5, 12, 15, 14, 9, 0, 3, 2, 13, 4, joined by arrows around the ring; circle 7 is highlighted. Two squares: the first holds 16 points on parallel slanted lines, the second 400 evenly scattered points.states of the toy generator7618111051215149032134seed 7toy: pairs (uₙ, uₙ₊₁)uₙ01a good generator: 400 pairsuₙ01The cycle of a toy generator and pairs of consecutive numbersSixteen circles around a ring labelled with the states 7, 6, 1, 8, 11, 10, 5, 12, 15, 14, 9, 0, 3, 2, 13, 4, joined by arrows around the ring; circle 7 is highlighted. Two squares: the first holds 16 points on parallel slanted lines, the second 400 evenly scattered points.states of the toy generator7618111051215149032134seed 7toy generatoruₙ01good: 400 pairsuₙ01
Left: the toy generator s ← (5s + 3) mod 16 from the seed 7 visits all 16 states and returns to 7, so each value u = s/16 occurs once per cycle. Right: consecutive pairs (uₙ, uₙ₊₁) lie on a few lines for the toy, while 400 pairs from a modern generator fill the square.

The toy has two defects. It repeats after 16 numbers. And consecutive pairs fall on a few lines: dividing the rule by 16 gives un+1=5un+316−ku_{n+1} = 5u_n + \tfrac3{16} - k, where the integer kk is what the  mod \bmod removes, and 0≤5sn+3≤780 \le 5s_n + 3 \le 78 leaves only k=0,…,4k = 0, \dots, 4. So every pair (un,un+1)(u_n, u_{n+1}) lies on one of five parallel lines of slope 5. Real generators keep the state-and-rule design and remove both. NumPy’s default, PCG64, has a 128-bit state and scrambles it before output (O’Neill, 2014); on GPUs, PyTorch uses Philox, which computes the nn-th number directly from nn and the seed so that thousands of threads can draw at once (Salmon et al., 2011).

The seed is what makes an experiment repeatable, and the guarantee is narrow. The same seed reproduces the same numbers only with the same generator, the same calls in the same order and the same library build. NumPy keeps its raw streams stable across versions, but samplers built on them, such as normal, may change between releases (NumPy documentation).

Step 2: any one-dimensional distribution, by inverting the CDF#

Suppose we need an outcome aa, bb or cc with probabilities 0.20.2, 0.50.5 and 0.30.3. Cut [0,1)[0, 1) into pieces of exactly these lengths, [0,0.2)[0, 0.2), [0.2,0.7)[0.2, 0.7) and [0.7,1)[0.7, 1), draw uu and return the outcome whose piece contains it. A uniform number falls into a piece with probability equal to its length. The cut points are the running totals of the probabilities, the cumulative distribution function FF, so sampling is a lookup: the first outcome whose running total exceeds uu.

Figure

Sampling a discrete distribution: cut the interval [0, 1)

The interval [0, 1) cut into three pieces, the staircase CDF and the shares of outcomesA vertical strip from 0 to 1 split into pieces of length 0.2, 0.5 and 0.3 for outcomes a, b and c. A staircase CDF with steps at 0.2, 0.7 and 1. A dashed line from u = 0.637 runs right to the b step and down to outcome b. Bars of the shares in 10,000 draws, near 0.2, 0.5 and 0.3.abc00.20.71uabcF (running total)u = 0.63710,000 draws: shares0.200a0.503b0.297cThe interval [0, 1) cut into three pieces, the staircase CDF and the shares of outcomesA vertical strip from 0 to 1 split into pieces of length 0.2, 0.5 and 0.3 for outcomes a, b and c. A staircase CDF with steps at 0.2, 0.7 and 1. A dashed line from u = 0.637 runs right to the b step and down to outcome b. Bars of the shares in 10,000 draws, near 0.2, 0.5 and 0.3.abc00.20.71uabcF (running total)u = 0.63710,000 draws: shares0.200a0.503b0.297c
Outcomes a, b and c have probabilities 0.2, 0.5 and 0.3. The cuts at 0.2 and 0.7 are the running totals, the CDF F (the staircase in the middle). The uniform number u = 0.637 falls into the second piece, so the result is b. Right: the shares in 10,000 draws, 0.200, 0.503, 0.297; ticks mark 0.2, 0.5 and 0.3.

For a continuous, strictly increasing CDF FF the same lookup asks which xx has accumulated exactly uu of the probability. With U∼U(0,1)U \sim \mathcal U(0, 1),

 X=F−1(U)∼F \boxed{\,X = F^{-1}(U) \sim F\,}

This is inverse transform sampling (Devroye, 1986, §II.2). It works because F−1(U)≤xF^{-1}(U) \le x exactly when U≤F(x)U \le F(x), which has probability F(x)F(x); the functions of random variables note gives the proof and the version for CDFs with jumps. For the exponential distribution with rate λ\lambda, solving 1−e−λx=u1 - e^{-\lambda x} = u gives

x=−ln⁡(1−u)λ.x = -\frac{\ln(1 - u)}{\lambda} .

With λ=1\lambda = 1, the uniform numbers 0.10.1, 0.50.5, 0.90.9 and 0.990.99 become 0.1050.105, 0.6930.693, 2.3032.303 and 4.6054.605. Half of all samples land in the short stretch below 0.6930.693, and the last 1% spread over everything beyond 4.64.6: the long tail appears by itself.

For the standard normal, FF is Φ\Phi, whose inverse has no formula in elementary functions. Libraries evaluate Φ−1\Phi^{-1} with rational approximations accurate to about 16 digits (Wichura, 1988). The figure shows why the recipe produces a bell.

Figure

From uniform to normal through the inverse CDF

Inverse transform sampling: u from U(0, 1) becomes x = Φ⁻¹(u)Top plot: the S-shaped curve Φ(x) rising from 0 to 1 over x from −3.5 to 3.5. Six values of u are marked on the vertical axis: 0.06, 0.24, 0.42, 0.58, 0.8 and 0.95. From each a dashed line runs right to the curve and then down to the x axis. Bottom plot: a histogram of 4000 values of Φ⁻¹(u) with the bell-shaped N(0, 1) density drawn over it.00.51Φ(x)u ~ U(0, 1)−3−2−10123x4000 values of Φ⁻¹(u)N(0, 1) densityInverse transform sampling: u from U(0, 1) becomes x = Φ⁻¹(u)Top plot: the S-shaped curve Φ(x) rising from 0 to 1 over x from −3.5 to 3.5. Six values of u are marked on the vertical axis: 0.06, 0.24, 0.42, 0.58, 0.8 and 0.95. From each a dashed line runs right to the curve and then down to the x axis. Bottom plot: a histogram of 4000 values of Φ⁻¹(u) with the bell-shaped N(0, 1) density drawn over it.00.51Φ(x)u ~ U(0, 1)−3−2−10123x4000 values of Φ⁻¹(u)N(0, 1) density
Top: the standard normal CDF Φ; six uniform numbers u go across to the curve and down to x = Φ⁻¹(u). Equal stretches of u map to short stretches of x where Φ is steep, so the values pile up near zero. Bottom: a histogram of 4000 such values (bin width 0.25) under the N(0, 1) density.

Step 3: the standard normal, by Box–Muller#

Box and Muller found an exact shortcut that turns two independent uniforms into two independent standard normals (Box and Muller, 1958):

R=−2ln⁡U1,Θ=2πU2,Z1=Rcos⁡Θ,Z2=Rsin⁡Θ.\begin{aligned} R &= \sqrt{-2\ln U_1}, \\ \Theta &= 2\pi U_2, \\ Z_1 &= R\cos\Theta, \\ Z_2 &= R\sin\Theta . \end{aligned}

It is step 2 in polar coordinates. Take two independent standard normals, with joint density 12πe−(z12+z22)/2\frac{1}{2\pi}e^{-(z_1^2 + z_2^2)/2}, and write them in polar coordinates (R,Θ)(R, \Theta). The map from (r,θ)(r, \theta) to (z1,z2)(z_1, z_2) has Jacobian determinant rr, so by the densities under a transform section of the Mathematics course

pR,Θ(r,θ)=12π⋅r e−r2/2.p_{R,\Theta}(r, \theta) = \frac{1}{2\pi} \cdot r\,e^{-r^2/2} .

The density is a function of θ\theta alone times a function of rr alone, so Θ\Theta is uniform on [0,2π)[0, 2\pi) and independent of RR. The squared radius has the distribution function

Pr⁡(R2≤t)=∫0tr e−r2/2 dr=[−e−r2/2]0t=1−e−t/2,\begin{aligned} \Pr(R^2 \le t) &= \int_0^{\sqrt t} r\,e^{-r^2/2}\, dr \\ &= \Big[-e^{-r^2/2}\Big]_0^{\sqrt t} \\ &= 1 - e^{-t/2}, \end{aligned}

an exponential with rate 12\tfrac12. Step 2 samples it as −2ln⁡(1−U1)-2\ln(1 - U_1), and since 1−U11 - U_1 is uniform too, −2ln⁡U1-2\ln U_1 works as well. The constructed (R,Θ)(R, \Theta) then has the joint distribution of the polar coordinates of a normal pair, and so (Rcos⁡Θ,Rsin⁡Θ)(R\cos\Theta, R\sin\Theta) has the distribution of the pair itself.

For u1=0.3u_1 = 0.3 and u2=0.8u_2 = 0.8 the distance is R=−2ln⁡0.3=1.552R = \sqrt{-2\ln 0.3} = 1.552, the angle is 288∘288^\circ, and the point is z=(0.480,−1.476)z = (0.480, -1.476).

Figure

Box–Muller step by step: two uniform numbers → a point of the normal cloud

The Box–Muller transform in three panelsPanel one: 600 points filling the unit square evenly, with the point (0.3, 0.8) highlighted. Panel two: a ray from the origin at angle 288° and length 1.552, with an arc marking the angle. Panel three: a cloud of 600 normal points around zero with circles of radius 1 and 2 and the point (0.480, −1.476) highlighted.1. two uniform numbers(0.3, 0.8)u₁u₂12–3. angle and distanceangle 2π·0.8 = 288°2distance √(−2 ln 0.3) = 1.55234. a point in the planez = (0.480, −1.476)600 such points4The Box–Muller transform in three panelsPanel one: 600 points filling the unit square evenly, with the point (0.3, 0.8) highlighted. Panel two: a ray from the origin at angle 288° and length 1.552, with an arc marking the angle. Panel three: a cloud of 600 normal points around zero with circles of radius 1 and 2 and the point (0.480, −1.476) highlighted.1. two uniform numbers(0.3, 0.8)u₁u₂12–3. angle and distanceangle 2π·0.8 = 288°2distance √(−2 ln 0.3) = 1.55234. a point in the planez = (0.480, −1.476)600 such points4
Left: uniform pairs (u₁, u₂), with the example (0.3, 0.8) highlighted. Middle: u₂ sets the angle 2πu₂ and u₁ the distance √(−2 ln u₁). Right: the resulting points z, a cloud centred at zero, with circles of radius 1 and 2. A small u₁ gives one of the rare far points.

A floating-point generator on [0,1)[0, 1) can return exactly 0, where ln⁡U1=−∞\ln U_1 = -\infty, so code feeds the logarithm 1−U11 - U_1 instead. Libraries use faster variants of the same idea; NumPy’s normal sampler uses the ziggurat method.

Step 4: any Gaussian, by an affine map#

Steps 1–3 give standard normal noise Z∼N(0,I)Z \sim \mathcal N(\mathbf 0, \mathbf I), a round cloud centred at zero. A shift and a linear map turn it into any Gaussian: for any matrix A\mathbf A with AA⊤=Σ\mathbf A\mathbf A^{\top} = \boldsymbol\Sigma,

 X=μ+AZ∼N(μ,Σ) \boxed{\,X = \boldsymbol\mu + \mathbf A Z \sim \mathcal N(\boldsymbol\mu, \boldsymbol\Sigma)\,}

The next note chooses A\mathbf A and turns this map into a model with trainable parameters. The first four steps fit in a few lines:

import numpy as np

rng = np.random.default_rng(0)               # the seed fixes the whole stream
u = rng.random((100_000, 3))                 # step 1: U(0, 1), three columns
print(u[0].round(4))                         # [0.637  0.2698 0.041 ]

x = -np.log1p(-u[:, 0])                      # step 2: exponential, x = -ln(1 - u)
print(x.mean().round(3), np.median(x).round(3))          # theory: 1 and ln 2 = 0.693

r = np.sqrt(-2 * np.log1p(-u[:, 1]))         # step 3: Box-Muller, 1 - u is never 0
phi = 2 * np.pi * u[:, 2]
z = np.stack([r * np.cos(phi), r * np.sin(phi)], axis=1)
print(z.std(axis=0).round(3), np.corrcoef(z.T)[0, 1].round(3))

mu, Sigma = np.array([1.0, 2.0]), np.array([[2.0, 0.8], [0.8, 1.0]])
L = np.linalg.cholesky(Sigma)                # step 4: any A with A A^T = Sigma
xg = mu + z @ L.T
print(xg.mean(axis=0).round(2), np.cov(xg.T).round(2).ravel())
[0.637  0.2698 0.041 ]
1.005 0.694
[0.997 0.994] -0.001
[0.99 2.  ] [1.99 0.79 0.79 0.99]

The exponential sample has mean 1.0051.005 and median 0.6940.694 against the exact 11 and ln⁡2\ln 2. The normal pair has unit spread and no correlation, and the affine map lands on the requested mean and covariance up to sampling noise.

Step 5: anything else, by a learned nonlinear map#

Gaussians are ellipsoids. Images, speech and text have distributions of far more complicated shape, and to reach them we push the noise through a nonlinear map:

 Z∼N(0,I)  ⟶  X=gθ(Z) \boxed{\,Z \sim \mathcal N(\mathbf 0, \mathbf I) \;\longrightarrow\; X = g_\theta(Z)\,}

The distribution of XX is the pushforward of the noise distribution through gθg_\theta. In a generative model gθg_\theta is a neural network, and training chooses θ\theta so that the pushforward matches the data.

Figure

Sampling as a chain of transformations of noise

Four point clouds: uniform noise, standard normal, a Gaussian, a bent distributionFour square panels joined by arrows. First: points filling the unit square evenly. An arrow labelled Box–Muller leads to the second: a round cloud centred at zero. An arrow labelled mu plus A z leads to the third: the same cloud stretched horizontally and squeezed vertically. An arrow labelled g leads to the fourth: the cloud bent into an arch. One highlighted point is marked in all four panels.uniform noiseu ~ U(0, 1)²standard normalz ~ N(0, I)any Gaussianμ + Az ~ N(μ, AAᵀ)complex shapex = g(μ + Az)Box–Mullerμ + A zg(·)Four point clouds: uniform noise, standard normal, a Gaussian, a bent distributionFour square panels joined by arrows. First: points filling the unit square evenly. An arrow labelled Box–Muller leads to the second: a round cloud centred at zero. An arrow labelled mu plus A z leads to the third: the same cloud stretched horizontally and squeezed vertically. An arrow labelled g leads to the fourth: the cloud bent into an arch. One highlighted point is marked in all four panels.uniform noiseu ~ U(0, 1)²standard normalz ~ N(0, I)any Gaussianμ + Az ~ N(μ, AAᵀ)complex shapex = g(μ + Az)Box–Mullerμ + A zg(·)
The same 320 uniform pairs pass through every step; the amber point is one pair followed to the end. Box–Muller makes a round normal cloud, the affine map μ + Az with A = diag(1.4, 0.45) an ellipse, and g(y) = (y₁, y₂ − 0.45 y₁² + 0.9) bends it into an arch. In a model the last map is a trained network gθ.

This is the central picture of the course: a generative model is simple noise plus a learned transformation. Some of the transformation is designed by hand, such as the order in which an autoregressive model generates pixels or the noising schedule of a diffusion model, and the rest is learned (Kaiming He, MIT 6.S978, lecture 1). For a one-pass generator, sampling is easy by construction. The hard part is choosing θ\theta, which needs some way to compare the pushforward with the data.

When gg is invertible and smooth, the density of XX has a formula, the change of variables:

pX(x)=pZ(g−1(x)) ∣det⁡∂g−1(x)∂x∣.p_X(x) = p_Z\big(g^{-1}(x)\big)\,\left|\det \frac{\partial g^{-1}(x)}{\partial x}\right| .

It is derived in Multiple integrals and is the starting point of normalizing flows. For a general network there is no such formula, and that is where the families diverge.

Why this is hard#

Inverting a CDF solves sampling in one dimension, and a Gaussian has a formula in any dimension. Real data has neither. A 256×256256 \times 256 colour image has 196,608196{,}608 coordinates. Even a binary 28×2828 \times 28 image has 27842^{784} possible values, so a table of their probabilities needs 2784−12^{784} - 1 numbers (Isachenko, lecture 1). A joint CDF exists, but sampling coordinate by coordinate would need every conditional CDF, which is exactly what we do not know.

Natural images also fill a tiny part of that space. An array of independent random pixels looks like noise, because neighbouring pixels of a photograph are strongly correlated and these are not. Measurements agree: nearest-neighbour estimates put the intrinsic dimension of ImageNet, whose 224×224224 \times 224 colour images have 150,528150{,}528 coordinates, between 26 and 43 (Pope et al., 2021, Table 1). Real images lie close to a much lower-dimensional surface, and the model has to put nearly all its probability there.

Finally, a flexible function fθ(x)≥0f_\theta(x) \ge 0 becomes a density only after division by ∫fθ(x) dx\int f_\theta(x)\,dx, an integral over the whole space. For a neural network it has no closed form, and numerical integration does not rescue it. A grid with only 10 points per coordinate has 1078410^{784} nodes for a 28×2828 \times 28 image (why grids fail), and uniform random points almost never land on the thin region where fθf_\theta is large. Without the integral the likelihood cannot be evaluated.

The two basic rules of probability show where the difficulty sits (BMML lecture notes, 2026, lecture 1). The product rule factorizes any joint distribution into one-dimensional conditionals,

p(x1,…,xn)=p(x1)∏k=2np(xk∣x1,…,xk−1),p(x_1, \dots, x_n) = p(x_1) \prod_{k=2}^{n} p(x_k \mid x_1, \dots, x_{k-1}),

which is exact and is the whole idea of autoregressive models; the price is nn sequential steps per sample. The sum rule gives the density of a model with a latent code zz as an integral,

pθ(x)=∫pθ(x∣z) p(z) dz,p_\theta(x) = \int p_\theta(x \mid z)\, p(z)\, dz ,

which has no closed form once the decoder pθ(x∣z)p_\theta(x \mid z) is a nonlinear network. With a linear decoder and Gaussian noise it does: that model is probabilistic PCA, the starting point of the VAE note. The VAE, diffusion models and GANs are three ways of living with this integral.

Only the latent code is integrated out. In the VAE and in diffusion models the network weights θ\theta are point estimates, one value found by optimizing an objective; the latent variables zz, or the noisy images of a diffusion chain, are what is integrated over or sampled.

Six ways around it#

Each family keeps the “noise plus transformation” picture and differs in what it gives up to make training possible. Two groups stand out: likelihood-based models, which compute log⁡pθ(x)\log p_\theta(x) exactly or bound it, and likelihood-free models such as GANs, which compare samples instead. The cost of a sample is a second axis. A VAE or a GAN needs one network pass; an autoregressive model needs one pass per element, and energy-based and diffusion models need many steps of a chain (Murphy, 2023, Table 20.1).

Figure

Six families: noise becomes data; what differs is what can be computed

Diagram of six families of generative modelsSix rows: VAE, GAN, flow, energy-based model, diffusion, autoregressive model. Each has a noise circle on the left, an arrow into a blue box with the learned transformation and an arrow to the sample; on the right is what the model lets us compute. Dashed elements under the rows show the helpers: the encoder of the VAE, the discriminator of the GAN, the inverse map of the flow, the unknown normalizer of the EBM, the fixed noising of diffusion and the one-value-at-a-time order of the autoregressive model.simple noiselearned transformationsamplewhat it computesVAEzdecoder pθ(x | z)xlower bound on log p(x)encoder qφ(z | x)GANzgenerator Gθ(z)xno density, samples onlydiscriminator: real?Flowzinvertible fθxexact log p(x)fθ⁻¹ backEBMx0K Langevin steps on −∇EθxKlog p(x) up to log ZZ unknown; MCMC in training tooDiffusionxTT denoising stepsx₀lower bound on log p(x)noise added by a fixed ruleAutoregr.upθ(xi | x<i), i = 1…nxexact log p(x)one value at a time, n stepsDiagram of six families of generative modelsSix blocks stacked: VAE, GAN, flow, energy-based model, diffusion, autoregressive model. Each shows a noise circle, a blue box with the learned transformation and a sample circle; below are the helper part and what the model lets us compute.VAEzdecoder pθ(x | z)xencoder qφ(z | x)computes: lower bound on log p(x)GANzgenerator Gθ(z)xdiscriminator: real?computes: no density, samples onlyFlowzinvertible fθxfθ⁻¹ backcomputes: exact log p(x)EBMx0K Langevin steps on −∇EθxKZ unknown; MCMC in training toocomputes: log p(x) up to log ZDiffusionxTT denoising stepsx₀noise added by a fixed rulecomputes: lower bound on log p(x)Autoregr.upθ(xi | x<i), i = 1…nxone value at a time, n stepscomputes: exact log p(x)
Each family turns simple noise into a sample; what is learned is drawn in blue, helpers are dashed. The right column says what each model can compute: a lower bound (VAE, diffusion), nothing (GAN), the exact likelihood (flow, autoregressive), or a density up to an unknown normalizer Z (EBM, sampled by a noisy Markov chain).
FamilyHow a sample is drawnWhat can be computedNotes
Variational autoencoder (VAE)z∼N(0,I)z \sim \mathcal N(\mathbf 0, \mathbf I), then a learned decodera lower bound on log⁡pθ(x)\log p_\theta(x), the ELBOVAE, variants
Generative adversarial network (GAN)z∼p(z)z \sim p(z), then x=Gθ(z)x = G_\theta(z) in one passno density, only samplesGAN, WGAN and six branch notes
Normalizing flowz∼N(0,I)z \sim \mathcal N(\mathbf 0, \mathbf I), then a chain of invertible mapsthe exact log⁡pθ(x)\log p_\theta(x)Flows, architectures
Energy-based model (EBM)a noisy Markov chain started from noise, many stepslog⁡pθ(x)\log p_\theta(x) up to the unknown log⁡Zθ\log Z_\thetaMCMC samplers, EBMs
Diffusion modelstart from noise xTx_T, denoise step by step to x0x_0a lower bound on log⁡pθ(x)\log p_\theta(x)Forward process, objective, score matching, continuous time, latent diffusion
Autoregressive modelone value or token at a time, each given the previous onesthe exact log⁡pθ(x)\log p_\theta(x)PixelRNN and PixelCNN, transformers

Not every model gives a likelihood, and a likelihood does not always reflect how good samples look. Mix a good model pp with a model qq of white noise and sample from 0.01 p+0.99 q0.01\,p + 0.99\,q: 99% of the samples are noise. Since q(x)≥0q(x) \ge 0 and the logarithm is increasing,

log⁡(0.01 p(x)+0.99 q(x))≥log⁡(0.01 p(x))=log⁡p(x)−ln⁡100.\begin{aligned} \log\big(0.01\,p(x) + 0.99\,q(x)\big) &\ge \log\big(0.01\,p(x)\big) \\ &= \log p(x) - \ln 100 . \end{aligned}

The log-likelihood drops by at most ln⁡100≈4.61\ln 100 \approx 4.61 nats per image, while the log-likelihoods of different models of 32×3232 \times 32 images can differ by thousands of nats (Theis, van den Oord and Bethge, 2016, §3.2). The course therefore also measures models from their samples alone, before it needs those measurements to compare GANs.

The plan of the course#

The sections build on each other in this order; flows, energy-based models, diffusion and autoregressive models are parallel routes from the same foundations.

SectionNotesWhat it adds
FoundationsGaussians, entropy and KL, fitting with KL, training background, autoencodersthe maximum-likelihood objective and the network blocks
Variational autoencodersVAE, β-VAE and VQ-VAEa latent code, trained through a lower bound
EvaluationInception Score, FID, LPIPS, precision and recall, human evaluationjudging samples when there is no likelihood
GANsGAN, WGAN; branches on scaling, StyleGAN, α-GAN, CycleGAN, StarGAN and beyondlearning by comparing samples with a discriminator
Normalizing flowschange of variables, flow architecturesexact likelihood through invertible maps
Energy-based modelsMCMC samplers, energy-based modelsunnormalized densities and their samplers
DiffusionDDPM, score matching, continuous-time diffusion and flow matching, latent diffusion, drag editinglearning to reverse a gradual noising process
Autoregressive modelsPixelRNN and PixelCNN, transformers over image tokensexact likelihood by the chain rule
Audiotext-to-speech and musicthe same tools on sound

What you should remember#

  • Data is a sample from an unknown pdatap_{\text{data}}; a generative model is a pθp_\theta we can sample from, fitted so that pθ≈pdatap_\theta \approx p_{\text{data}}.
  • Minimizing DKL(pdata ∥ pθ)D_{\mathrm{KL}}(p_{\text{data}} \,\|\, p_\theta) is maximum likelihood, which needs pθ(x)p_\theta(x) as a number.
  • A family that can memorize the sample wins the likelihood and generalizes nothing; judge on held-out data.
  • Sampling ladder: uniform numbers, inverse CDF, Box–Muller, an affine map, a learned nonlinear map.
  • The families differ in what they can compute: a bound (VAE, diffusion), nothing (GAN), the likelihood up to a constant (EBM) or exactly (flows, autoregressive).

Next#

The first rung of the ladder that later models stand on is the Gaussian: how a matrix square root turns standard noise into any Gaussian, and how to make its parameters trainable. That is Multivariate Gaussian distributions.

References#