U1.0 What Is a Generative Model Learning?
Builds on5 notes
- L0.0How an Agent Prices a House: Learning Rules from Examples
- L5.0How should we score how well a model explains the data?
- L5.3Which number tells us whether a generative model is good?
- M5.1The Cost of a Wrong Probability Table: KL Divergence and Cross-Entropy
- L5.1What can we optimize when the required marginal is intractable?
A request that sounds simple
We have 50,000 32×32 images. We already know a familiar recipe from machine learning: define a loss and reduce it with gradient descent. Now someone asks for something different: can these tools produce an image the model has never seen, yet looks as though it belongs to the same dataset?

Figure: the problem we face. We have samples and know how to optimize a loss. What we want at the end is a new, plausible sample.
We will turn this intuitive request into a mathematical problem that we can implement. First we need to ask:
What does it mean to generate an image?
And what does it mean to look as though it came from the same dataset?
Generating an image from the same source
A 32×32 RGB image contains numbers. Treat each number as a coordinate: an image is one point in .
SupplementWhat is a point in 3072 dimensions?
A vector containing 3072 numbers. In the more familiar , a point is three numbers, such as . We simply replace three coordinates with 3072.
Every possible image corresponds to a point in this space. Real images are not spread uniformly through it: some are common, others rare, and many points represent noise or implausible images.
A distribution describes which regions are more or less likely to contain data. We call this real but unknown distribution .
In a continuous model, is a probability density: how densely data occurs near , rather than the probability of exactly . Higher density means nearby images are more common when we sample from this source.
SupplementWhy density rather than probability?
For a continuous density, a single point has probability zero, and a density value may exceed one. Multiplying by a small volume gives , approximately the probability of landing in that small region. Integrating over the whole space gives one.
Thus is a function over the entire space. If density were height, common regions would form hills and rare regions valleys. A local density peak is a mode.
Our 50,000 images are not the distribution itself; they are samples from it, or observed points in . They usually reveal some structure: many samples in some regions, few in others, and none in large parts of the space.

Figure: from samples to a distribution. The left panel shows similar images clustered together. Density has a value at every location, including locations without an observed sample. The right panel uses density as height, with modes at the peaks. This low-dimensional picture illustrates data that actually lives in .
We can now explain generation and resemblance together.
Generation means drawing a new point from . Selecting one of the existing images would be retrieval; we want a previously unseen sample.
Why should it resemble the dataset? It comes from the same distribution. High-density regions are sampled more often, so most samples fall in plausible regions.
There is no rule that forbids a sample from entering a low-density region. It is simply less likely to do so.
More precisely, resembling the original data means:
The new point also looks like a draw from the same .
Here is the difficulty: we cannot observe the distribution that generates the data. We see 50,000 points, but want to learn the landscape behind them.
Could a neural network represent the density directly?
A natural approach is to represent with a network, fit it to , and then sample from it.
Before choosing a loss, its output must be a valid density, satisfying both:
- Nonnegativity: everywhere.
- Normalization: .
An ordinary network can output any real number, so neither condition holds automatically. We need to enforce each mathematically.
Let the network output and exponentiate its negative:
This enforces positivity. Lower energy gives higher unnormalized density, but the total need not be one. Compute the total and divide by it:
Provided is finite, this enforces normalization. A model that assigns energies and converts them into a distribution this way is an energy-based model [1].
SupplementWhy call it energy?
The name comes from statistical physics, where the probability of a state often takes the form
Lower makes larger and the state more likely. An energy-based model uses the same mathematical form, assigning lower energy to states it considers more plausible.
This need not be physical energy. It is a numerical measure of compatibility with the data; exponentiation and normalization still turn it into a density.
With a density in hand, we can maximize likelihood, assigning higher density where data occurs. Expanding the negative log-likelihood gives
The first term lowers energy near training data. The second is difficult: integrates over the full 3072-dimensional space and cannot be computed from our images alone.
Training actually needs the gradient rather than the value of . Differentiating gives
We can estimate the first expectation using training data. The second averages over the model’s own distribution, requiring draws from the current . We wanted to train a distribution we could sample from, yet its training gradient already requires model samples.
An expression that we can write but cannot compute directly is often called intractable. Energy-based models can be trained with approximate sampling; the issue is that direct density learning couples normalization to sampling.
Could we learn a function that produces samples instead?
Do we already know a distribution we can sample from? Yes: the standard Gaussian .
NotationWhy is Gaussian sampling easy?
is the standard Gaussian on . Established algorithms directly generate Gaussian random numbers, without training another model or taking many iterative steps through the space.
In dimensions, the coordinates are independent:
A -dimensional draw is just independent one-dimensional draws. This is what torch.randn(d) does.
Gaussian samples are readily available raw material. We want images from , so the question becomes:
Can a processing machine turn Gaussian raw material into the data we want?
Write this machine as a function : draw and compute . If the outputs collectively follow , we have solved the generation problem:
Generation now means draw , then transform it with . The resulting transformation of a distribution is its pushforward. A function that sends one distribution to another is a transport map.
DefinitionPushforward
For a distribution and measurable function , the distribution of transformed samples is . For any measurable set ,
The chance that lands in equals the chance that lies in its preimage. Equivalently, for an integrable function ,
Our goal is therefore find such that .
How do we learn ? We have real samples and generated samples; we need a loss differentiable with respect to .
DiscussionQ1
Why not use ordinary prediction? Draw , compute , and compare it with a training image using :
What happens if we train this way?
Show the reasoning
requires pairs, but nobody has told us which noise should produce which image. Here and are sampled independently.
We can calculate the optimum. Fix ; independence gives
Every input produces the same mean training image: a blurry, gray result with low but no diversity. This is regression to the mean when a prediction has multiple possible targets.
The loss asks the wrong question. We need the distribution of to match , while independent-pair asks whether a particular output matches a particular image. Two sets can follow the same distribution without independently drawn pairs being close. This objective therefore learns the mean.
There are two natural directions:
- Use a distribution-level objective to compare the generated and real distributions.
- Construct the pairing ourselves. If a noise input has an assigned endpoint, perhaps even a whole path, samplewise regression can make sense again.
Let us examine the first direction before returning to constructed pairs.
If is invertible, can we recover its density?
We need a distribution-level objective. Since produces samples, perhaps we can calculate their density and train by likelihood.
Knowing how to compute does not tell us the density at . We need to recover its source and account for local stretching or compression of space.
If is invertible and sufficiently smooth, the inverse recovers and the Jacobian determinant measures volume change. Change of variables gives
The first term evaluates the Gaussian density at the recovered source. The second corrects for local volume change. Models that compose invertible transformations to turn simple distributions into complex ones are normalizing flows [2, 3].
If the inverse and log-determinant are tractable, we can evaluate and train by maximum likelihood:
SupplementWhy does maximum likelihood help?
Think of as distributing a fixed supply of betting chips over the data space. The fixed total corresponds to
It can put more chips in some regions and fewer in others.
For each observed image , we ask how much density it assigned there. Its contribution to log-likelihood is .
Normalization prevents assigning high density everywhere. Mass wasted in regions without data leaves less for data-rich regions. Improving likelihood therefore encourages the model to place mass where data occurs.
For independent samples,
Maximum likelihood maximizes average log density at the observations.
It is a distribution-level objective because
The first term is independent of . Maximizing likelihood therefore minimizes , which is zero exactly when the distributions agree; see M5.1.
This supplies a distribution-level loss, but every training image requires an inverse and a Jacobian determinant. An arbitrary network has no cheap inverse, and a generic determinant costs , expensive at .
Normalizing flows consequently restrict their layer structures to make both calculations tractable. Their exchange for exact likelihood is:
The transformation must be invertible, with a tractable Jacobian determinant.
The same destination can be reached in many ways
Many different maps can produce the same final distribution:
Imagine moving boxes between warehouses. Success means the correct final number of boxes at every location. It does not specify which box goes where or which route it follows.
Transport is therefore underdetermined. Likelihood evaluates the final distribution without selecting a unique pairing or path; normalizing flows restrict the map to calculate that likelihood.
Interactive demo: the same endpoints, different transport. Change the pairing. The Gaussian at and target distribution at stay fixed; pointwise assignments and intermediate paths change.
Could we construct the missing pairs ourselves?
Each approach has a difficulty: direct density learning requires normalization, independent-pair regression learns a mean, and exact flow likelihood restricts the transformation.
But itself was not the problem; arbitrary pairing was. Regression may become useful if we construct meaningful training pairs.
Instead of guessing which image a Gaussian input should become, reverse the direction. Training provides clean data . A rule that gradually adds noise constructs ourselves.
This resembles recording a video of boxes being scattered. We know how each frame was created. Learning to reverse it can begin with a local question: given this frame, how should we move toward the preceding one?
This noise-adding rule is the forward process. It does not automatically solve the reverse direction, but supplies noisy states we can sample and separates generation into learning problems at different noise levels.
Diffusion begins by designing a forward process that constructs training pairs from data.
How should we add noise so that any is easy to generate and approaches a familiar distribution we can sample?
Check your understanding
References
- LeCun, Y., Chopra, S., Hadsell, R., Ranzato, M., Huang, F. A Tutorial on Energy-Based Learning. 2006.
- Papamakarios, G., Nalisnick, E., Rezende, D. J., Mohamed, S., Lakshminarayanan, B. Normalizing Flows for Probabilistic Modeling and Inference. JMLR 2021.
- Rezende, D. J., Mohamed, S. Variational Inference with Normalizing Flows. ICML 2015.