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:
For images, is a vector of pixel values; for text it is a sequence of tokens, for audio a waveform. The distribution (also written ) is unknown. Nobody can write down the density of "photographs of cats". All we have is the sample.
We look for a model with parameters such that
and from which we can draw new points . 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 , with mass on each . But the target is , 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 , predict a label . A classifier models the conditional distribution , 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: , or for a condition 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 colour image has coordinates, and 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, , together with the class frequencies , already contains a classifier:
Two jobs: sample and evaluate
What do we want from ? Two jobs.
- Sample: produce new points . This is what makes a model generative.
- Evaluate: for a given , compute , a probability for discrete data or a density for continuous data. Its logarithm, , is the log-likelihood of 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, , , . For independent points the probability of a data set is a product, so its logarithm is a sum, and we usually report the average:
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 , 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 on each training point: its height is of order , so the value grows without bound as . 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 updates a large internal state, and each state is turned into a number . 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 , , for , , . Cut into pieces of exactly these lengths:
Draw and return the outcome whose piece contains it; for that is . 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 , , are the running totals of the probabilities, that is, the values of the cumulative distribution function (CDF) .
Inverse transform sampling
The continuous case is the same idea with infinitely many pieces. The CDF says how much probability lies to the left of . Assume it is continuous and strictly increasing, so every level is reached at exactly one point. Draw a uniform and return the point that has accumulated exactly that much probability:
This is inverse transform sampling. Why it works takes three lines:
The middle step holds because is increasing; the last because a uniform number is at most with probability . So has exactly the CDF we wanted.
Worked example: the exponential distribution. Waiting times with rate have for . Solve for :
With :
Half of all uniform numbers land in the short stretch , and the last 1% are spread over everything beyond . The shape of the exponential, many short waits and a long tail, appears by itself.
For the standard normal the CDF has no inverse in elementary functions. Libraries approximate 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:
It is inverse transform sampling in polar coordinates. The joint density of two independent standard normals, , depends only on the distance from the origin. In polar coordinates the area element is , so the distance and the angle have the joint density : a constant in times a function of alone. Hence the angle is uniform on and independent of the distance, and the squared distance has
an exponential with rate , which the previous block samples as ; and is uniform too. For , : , the angle is , and .
Any Gaussian. Shift and scale: if , then . In dimensions a matrix takes the place of .
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 :
The distribution of is the pushforward of the noise distribution through , and training chooses so that it matches .
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 .
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 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 becomes a density only after dividing by , an integral over the whole space that is intractable for a neural network. Without it, 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 |
|---|---|---|
| Latent-variable models (VAE) | , then a decoder | a lower bound, the ELBO |
| GAN | in one pass; trained against a discriminator | nothing: samples only |
| Normalizing flow | an invertible map applied to | 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 and draw samples with a Markov chain guided by its gradient, so is known only up to a constant. Autoregressive models, such as language models, draw one coordinate or token at a time from . 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 and a model , 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 in many dimensions, and the shift-and-scale step 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 we need a distance between distributions: one number that says how far is from . Since 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.
Answers are saved on the server and tied to this browser until you sign in.