Skip to content
BIASED

What generative modeling is

Data as samples from an unknown distribution, the two jobs of a model — sample and evaluate likelihood — how uniform noise becomes any distribution, and the map of model families in this course.

Data as samples from an unknown distribution

One question drives this course: how can a model learn to produce new data that looks like its training data? A classifier answers a question about an image: is this a cat? A generative model produces the image itself, a new cat that was not in the training set.

To make the question precise, we treat a data set as independent draws from one distribution:

x1,…,xN∼P.x_1, \dots, x_N \sim P .

For images, x∈RDx \in \mathbb R^D is a vector of DD pixel values; for text it is a sequence of tokens, for audio a waveform. The distribution PP (also written pdatap_{\text{data}}) is unknown. Nobody can write down the density of "photographs of cats". All we have is the sample.

We look for a model QθQ_\theta with parameters θ\theta such that

Qθ≈P,Q_\theta \approx P ,

and from which we can draw new points x∼Qθx \sim Q_\theta. Generative modeling is learning to sample from a distribution we do not know, by approximating it from examples.

"New" matters. Replaying a random training example is also a way to sample: its distribution is the empirical distribution P^\hat P, with mass 1/N1/N on each xix_i. But the target is PP, the process that produced the photographs, not the finite list of photographs itself.

Generative versus discriminative

Most of machine learning is discriminative: given an input xx, predict a label yy. A classifier models the conditional distribution p(y∣x)p(y \mid x), one distribution over labels for each input. The input is always given, so the model never has to decide which inputs are likely.

A generative model describes the inputs themselves: P(x)P(x), or P(x∣c)P(x \mid c) for a condition cc such as a class label or a text prompt. The second case is conditional generation; it is the same problem with one distribution per condition.

The generative object is the harder one. A 64×6464 \times 64 colour image has 64⋅64⋅3=12 28864 \cdot 64 \cdot 3 = 12\,288 coordinates, and PP has to say how probable every combination of them is, while a cat-or-dog classifier only splits probability between two labels.

The two views are linked by Bayes' rule. A generative model of each class, p(x∣y)p(x \mid y), together with the class frequencies p(y)p(y), already contains a classifier:

p(y∣x)=p(x∣y) p(y)p(x).p(y \mid x) = \frac{p(x \mid y)\, p(y)}{p(x)} .

Two jobs: sample and evaluate

What do we want from QθQ_\theta? Two jobs.

  1. Sample: produce new points x∼Qθx \sim Q_\theta. This is what makes a model generative.
  2. Evaluate: for a given xx, compute Qθ(x)Q_\theta(x), a probability for discrete data or a density for continuous data. Its logarithm, log⁡Qθ(x)\log Q_\theta(x), is the log-likelihood of xx under the model.

Every model in this course can sample. Not every one can evaluate, and that turns out to be the main difference between them.

Evaluation is useful in its own right: it measures how plausible the model finds a given object, and it lets us compare models on data they were not trained on. It is also a training signal. Take a toy model on three outcomes, Qθ(a)=0.2Q_\theta(a) = 0.2, Qθ(b)=0.5Q_\theta(b) = 0.5, Qθ(c)=0.3Q_\theta(c) = 0.3. For independent points the probability of a data set is a product, so its logarithm is a sum, and we usually report the average:

1N∑i=1Nlog⁡Qθ(xi).\frac1N \sum_{i=1}^N \log Q_\theta(x_i) .

A model that fits the data gives it a high average log-likelihood. The topic Entropy, cross-entropy and KL explains why maximizing it is the right goal.

One caution: a high average log-likelihood on the training points proves little. On a finite set of outcomes the highest possible value belongs to P^\hat P, the memorizer from the first block; the topic Entropy, cross-entropy and KL proves it. For continuous data it is worse. Put a narrow bump of width ss on each training point: its height is of order s−Ds^{-D}, so the value grows without bound as s→0s \to 0. A useful model therefore comes from a family that cannot memorize the sample, and it is judged on held-out data it was not trained on.

From uniform numbers to any discrete distribution

A model is generative only if we can draw from it. But a processor is deterministic: the same program on the same input does the same thing every time.

Libraries use a pseudo-random number generator: a fixed rule sn+1=T(sn)s_{n+1} = T(s_n) updates a large internal state, and each state is turned into a number u∈[0,1)u \in [0, 1). The starting state comes from a seed. The rule is chosen so that the outputs pass large batteries of statistical tests for independent uniform numbers, and the same seed gives the same numbers, which makes experiments repeatable.

Every other distribution is built from this one primitive by the same idea:

start with a distribution that is easy to sample, and transform its samples into samples of a harder one.

Take the toy model from the previous block, with probabilities 0.20.2, 0.50.5, 0.30.3 for aa, bb, cc. Cut [0,1)[0, 1) into pieces of exactly these lengths:

a:[0,0.2),b:[0.2,0.7),c:[0.7,1).a: [0, 0.2), \qquad b: [0.2, 0.7), \qquad c: [0.7, 1) .

Draw uu and return the outcome whose piece contains it; for u=0.637u = 0.637 that is bb. A uniform number falls into a piece with probability equal to the piece's length, so each outcome comes out exactly as often as the model says. The cut points 0.20.2, 0.70.7, 11 are the running totals of the probabilities, that is, the values of the cumulative distribution function (CDF) FF.

Inverse transform sampling

The continuous case is the same idea with infinitely many pieces. The CDF F(x)=Pr⁡(X≤x)F(x) = \Pr(X \le x) says how much probability lies to the left of xx. Assume it is continuous and strictly increasing, so every level u∈(0,1)u \in (0, 1) is reached at exactly one point. Draw a uniform uu and return the point that has accumulated exactly that much probability:

X=F−1(U),U∼U(0,1).X = F^{-1}(U), \qquad U \sim \mathcal U(0, 1) .

This is inverse transform sampling. Why it works takes three lines:

Pr⁡(X≤x)=Pr⁡(F−1(U)≤x)=Pr⁡(U≤F(x))=F(x).\Pr(X \le x) = \Pr\big(F^{-1}(U) \le x\big) = \Pr\big(U \le F(x)\big) = F(x) .

The middle step holds because FF is increasing; the last because a uniform number is at most F(x)F(x) with probability F(x)F(x). So XX has exactly the CDF we wanted.

Worked example: the exponential distribution. Waiting times with rate λ\lambda have F(x)=1−e−λxF(x) = 1 - e^{-\lambda x} for x≥0x \ge 0. Solve F(x)=uF(x) = u for xx:

e−λx=1−u⟹x=−ln⁡(1−u)λ.e^{-\lambda x} = 1 - u \quad\Longrightarrow\quad x = -\frac{\ln(1 - u)}{\lambda} .

With λ=1\lambda = 1:

uu 0.10.1 0.50.5 0.90.9 0.990.99
x=−ln⁡(1−u)x = -\ln(1 - u) 0.1050.105 0.6930.693 2.3032.303 4.6054.605

Half of all uniform numbers land in the short stretch [0,0.693)[0, 0.693), and the last 1% are spread over everything beyond 4.64.6. The shape of the exponential, many short waits and a long tail, appears by itself.

For the standard normal the CDF Φ\Phi has no inverse in elementary functions. Libraries approximate Φ−1\Phi^{-1} numerically, or use a different transformation, which comes next.

From noise to data: Box–Muller, Gaussians, a network

Standard normal noise. Box and Muller (1958) turn two independent uniforms into two independent standard normals:

R=−2ln⁡U1,φ=2πU2,Z1=Rcos⁡φ,Z2=Rsin⁡φ.R = \sqrt{-2\ln U_1}, \qquad \varphi = 2\pi U_2, \qquad Z_1 = R\cos\varphi, \qquad Z_2 = R\sin\varphi .

It is inverse transform sampling in polar coordinates. The joint density of two independent standard normals, 12πe−(z12+z22)/2\frac{1}{2\pi}e^{-(z_1^2 + z_2^2)/2}, depends only on the distance rr from the origin. In polar coordinates the area element is r dr dφr\,dr\,d\varphi, so the distance and the angle have the joint density 12π⋅r e−r2/2\frac{1}{2\pi} \cdot r\,e^{-r^2/2}: a constant in φ\varphi times a function of rr alone. Hence the angle is uniform on [0,2π)[0, 2\pi) and independent of the distance, and the squared distance has

Pr⁡(R2≤t)=∫0tr e−r2/2 dr=1−e−t/2,\Pr(R^2 \le t) = \int_0^{\sqrt t} r\,e^{-r^2/2}\,dr = 1 - e^{-t/2} ,

an exponential with rate 12\tfrac12, which the previous block samples as −2ln⁡(1−U1)-2\ln(1 - U_1); and 1−U11 - U_1 is uniform too. For u1=0.3u_1 = 0.3, u2=0.8u_2 = 0.8: R=1.552R = 1.552, the angle is 288∘288^\circ, and z=(0.480,−1.476)z = (0.480, -1.476).

Any Gaussian. Shift and scale: if ε∼N(0,1)\varepsilon \sim \mathcal N(0, 1), then x=μ+σε∼N(μ,σ2)x = \mu + \sigma\varepsilon \sim \mathcal N(\mu, \sigma^2). In DD dimensions a matrix takes the place of σ\sigma.

Anything else. Gaussians are ellipsoids; images and speech are not. To reach the shape of real data, push the noise through a nonlinear map, a neural network gθg_\theta:

z∼N(0,I),x=gθ(z).z \sim \mathcal N(0, I), \qquad x = g_\theta(z) .

The distribution of xx is the pushforward of the noise distribution through gθg_\theta, and training chooses θ\theta so that it matches PP.

This is the central picture of the course: a generative model is simple noise plus a learned transformation. Sampling is easy by construction. The hard part is choosing θ\theta.

The map of model families

Why it is hard. In one dimension we invert a CDF; a Gaussian has a formula in any dimension. Real data has neither.

  • Dimension. The 12 28812\,288 coordinates of a small image have a joint CDF, but no single inverse. Sampling coordinate by coordinate would need every conditional distribution, which is exactly what we do not know.
  • Normalization. A flexible function fθ(x)≥0f_\theta(x) \ge 0 becomes a density only after dividing by ∫fθ(x) dx\int f_\theta(x)\,dx, an integral over the whole space that is intractable for a neural network. Without it, Qθ(x)Q_\theta(x) cannot be evaluated.

The model families of this course all keep the picture "noise plus learned transformation". They differ in what they give up to make training possible:

Family How a sample is drawn What we know about log⁡Qθ(x)\log Q_\theta(x)
Latent-variable models (VAE) z∼N(0,I)z \sim \mathcal N(0, I), then a decoder a lower bound, the ELBO
GAN x=Gθ(z)x = G_\theta(z) in one pass; trained against a discriminator nothing: samples only
Normalizing flow an invertible map applied to zz the exact value
Diffusion start from pure noise, remove it in many small steps a lower bound

Two more families complete the course. Energy-based models never divide by the normalizing integral: they keep the flexible fθ(x)f_\theta(x) and draw samples with a Markov chain guided by its gradient, so log⁡Qθ(x)\log Q_\theta(x) is known only up to a constant. Autoregressive models, such as language models, draw one coordinate or token at a time from pθ(xi∣x<i)p_\theta(x_i \mid x_{<i}). Their likelihood is exact, a product of these conditionals, but sampling takes as many steps as there are coordinates.

Since some models give no likelihood at all, the course ends with evaluation: metrics computed from samples, such as FID, and finally human judgement.

What is still missing

We have the setting, samples from an unknown PP and a model QθQ_\theta, and the recipe for sampling: noise plus a transformation. Two pieces are missing before any family can be trained.

The noise. Nearly every model starts from ε∼N(0,I)\varepsilon \sim \mathcal N(0, I) in many dimensions, and the shift-and-scale step μ+σε\mu + \sigma\varepsilon becomes a matrix map. How a covariance matrix shapes a Gaussian, and how to sample from one, is the next topic, Multivariate Gaussians.

The target. To choose θ\theta we need a distance between distributions: one number that says how far QθQ_\theta is from PP. Since PP is known only through samples, that number has to be estimable from samples alone. Building it is the job of the topic after next, Entropy, cross-entropy and KL.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.