Skip to content
AIAI Wranglers

3 Foundations of Generative Modelling

What does it mean for a machine to generate data? Consider the task of producing a new face photograph that looks realistic but does not depict any real person, or synthesising a plausible completion of a half-written paragraph. In each case, we want to draw a fresh sample from some complex, high-dimensional distribution (the distribution of natural face images, or of coherent English text) even though we only have access to a finite collection of examples from that distribution.

This chapter formalises the generative modelling problem and develops the core ideas that every later chapter builds on. We start from the simplest possible setting, sampling from a known one-dimensional distribution, and progressively introduce the key abstractions: push-forward maps, latent variables, and the reparameterisation trick. Along the way, we connect each idea to the concrete generative model families studied in Parts II–V of this book.

What Does “Generation” Mean?

Suppose we observe data x1,x2,,xn drawn independently from an unknown distribution pdata over a space 𝒳 (e.g. 𝒳=d for images with d pixels). The goal of generative modelling is to construct a mechanism that can produce new samples whose distribution closely approximates pdata.

At first glance the problem sounds daunting: pdata lives in high dimensions, is unknown, and may have a very complicated shape (multiple modes, thin ridges, etc.). Yet every approach we will study reduces the problem to two ingredients:

  1. a source of randomness zpZ that is easy to sample (e.g. a standard Gaussian), and

  2. a deterministic map gθ:𝒵𝒳 that transforms simple noise into complex data.

The universal recipe for generation: sample simple noise z, then apply a learned map gθ to obtain a data-like sample x.

The art lies in how we design gθ and how we train it. Each family of generative models makes a different choice, as summarised in Table 1.

ApproachHow gθ is trainedChapters
Autoregressive modelsMaximise logpθ(x) via chain rule3, 6
Normalising flowsMaximise logpθ(x) via change of variables14
VAEsMaximise ELBO (lower bound on logpθ(x))14
GANsAdversarial game (no explicit likelihood)9–13
Diffusion modelsDenoise score matching16
How different generative model families fit the zgθ(z)=x paradigm.

Remark 1 (Explicit vs. Implicit Models).

A model is called explicit (or prescribed) if it defines a density pθ(x) that can be evaluated, at least approximately. Autoregressive models, normalising flows, and VAEs are explicit. A model is implicit if it can produce samples but does not provide a tractable density. GANs are the prototypical implicit model.

Sampling from Known Distributions

Before tackling the full generative problem, we review how to sample from a known distribution. Every method rests on the same idea: transform uniform or Gaussian noise into the target distribution.

The Inverse-CDF Method

Let X be a continuous random variable with cumulative distribution function FX(x)=Pr(Xx). Because FX is monotonically non-decreasing and maps [0,1], we can (under mild regularity conditions) invert it.

Theorem 1 (Inverse-CDF Sampling).

Let UUniform(0,1). Define X=FX1(U), where FX1(u)=inf{x:FX(x)u} is the generalised inverse. Then XFX.

Proof.

For any t, Pr(Xt)=Pr(FX1(U)t)=Pr(UFX(t))=FX(t), where the second equality uses the monotonicity of FX and the third uses the fact that U is uniform on [0,1].

Inverse-CDF sampling: draw u uniformly on [0,1], then read off FX1(u) from the CDF curve.

Example 1 (Sampling from the Exponential Distribution).

Let XExp(λ), so FX(x)=1eλx for x0. Inverting gives FX1(u)=1λln(1u). Hence, if UUniform(0,1), the transformation X=1λln(1U) yields an exponential sample. In fact, since 1U is also Uniform(0,1), we may simplify to X=1λlnU.

Caution.

The inverse-CDF method requires that FX1 be available in closed form (or cheaply computable). For many distributions of interest, especially multivariate ones, this is not the case. That is precisely why we need more sophisticated methods.

The Box–Muller Transform

The Gaussian distribution 𝒩(0,1) does not have a closed-form inverse CDF. A classical workaround is the Box–Muller transform: given two independent uniform samples U1,U2Uniform(0,1), define (BOX Muller)Z1=2lnU1cos(2πU2),Z2=2lnU1sin(2πU2). Then Z1 and Z2 are independent 𝒩(0,1) random variables.

Insight.

The Box–Muller transform is our first glimpse of a recurring theme: design a deterministic map that converts simple randomness into the distribution you want. Normalising flows (Chapter 14) generalise this idea by learning the map from data.

Rejection Sampling

When neither the inverse CDF nor a clever transform is available, we can fall back on rejection sampling. Suppose we want to sample from a target density p(x) but can only evaluate it (up to a normalising constant). Choose a proposal density q(x) that is easy to sample and satisfies p(x)Mq(x) for some constant M.

Algorithm 1 (Rejection Sampling).

  1. Sample xq(x) and uUniform(0,1).

  2. If up(x)Mq(x), accept x; otherwise reject and return to step 1.

The accepted samples are distributed exactly according to p.

Proposition 1 (Correctness of Rejection Sampling).

Each accepted sample has density p(x), and the acceptance probability is 1/M.

Proof.

The probability of accepting a proposal x is Pr(accept|x)=p(x)/(Mq(x)). The marginal density of an accepted sample is q(x)p(x)Mq(x)q(x)p(x)Mq(x)dx=p(x)/M1/M=p(x). The overall acceptance rate is q(x)p(x)Mq(x)dx=1/M.

Rejection sampling: points under p(x) are accepted (green); points between p(x) and Mq(x) are rejected (red).

Remark 2 (The Curse of Dimensionality in Rejection Sampling).

In high dimensions, the acceptance rate 1/M becomes astronomically small, and nearly all proposals are rejected. For instance, to sample a d-dimensional standard Gaussian using a uniform proposal over a hypercube, the acceptance rate decays exponentially in d. This is the fundamental reason why naive sampling methods fail for high-dimensional generative modelling, motivating the learned generators studied in later chapters.

Importance Sampling

Sometimes we do not need fresh samples but rather need to estimate an expectation 𝔼xp[f(x)] when p is hard to sample from. Importance sampling re-writes the expectation using a tractable proposal q: (Importance Sampling)𝔼xp[f(x)]=𝔼xq[p(x)q(x)f(x)]=𝔼xq[w(x)f(x)], where w(x)=p(x)/q(x) are the importance weights.

Key Idea.

Importance sampling is not merely a computational trick; it is a cornerstone of variational inference (Chapter 8). The evidence lower bound (ELBO) used to train VAEs (Chapter 15) can be derived as an importance-weighted estimate of the log-marginal likelihood.

Maximum Likelihood Estimation for Generation

The simplest route to a generative model is: choose a parametric family pθ, fit it to data by MLE, then sample from the fitted distribution.

Definition 1 (Maximum Likelihood Estimator).

Given i.i.d. observations x1,,xnpdata, the maximum likelihood estimator (MLE) selects the parameter θ^ that maximises the likelihood: (MLE)θ^=arg maxθi=1npθ(xi)=arg maxθi=1nlogpθ(xi).

Once we have θ^, generation is straightforward: sample xnewpθ^.

Example 2 (Balls in a Bag: Bernoulli MLE).

Suppose a bag contains green and blue balls in unknown proportions. We draw n=6 balls with replacement and observe the sequence G, G, B, G, B, G (encoding Green = 1, Blue = 0). Model each draw as XiBernoulli(θ), where θ=Pr(Green). The log-likelihood is (θ)=i=16[xilogθ+(1xi)log(1θ)]=4logθ+2log(1θ). Setting (θ)=0 gives θ^=4/6=2/3. To generate, we draw XnewBernoulli(2/3): each synthetic draw is Green with probability 2/3.

Example 3 (Fitting a Gaussian to Continuous Data).

Now consider continuous data: heights (in cm) of n=5 students: {168,172,175,170,180}. Model them as Xiiid𝒩(μ,σ2). The MLE gives μ^=173 and σ^2=15i(xi173)2=17.6. To generate a new “height”, sample Xnew𝒩(173,17.6), for example using the Box–Muller transform from The Box–Muller Transform.

Caution.

MLE with a simple parametric family (e.g. a single Gaussian) can only capture simple distributions. Natural images are not Gaussian; they live on a low-dimensional manifold within the high-dimensional pixel space. To model such distributions we need either (i) far richer parametric families (Gaussian mixtures, Chapter 7) or (ii) neural-network-based generators (Parts III–V).

Autoregressive Factorisation

For a d-dimensional vector 𝒙=(x1,,xd), the chain rule of probability gives an exact factorisation: (Chain RULE GEN)p(𝒙)=p(x1)p(x2|x1)p(x3|x1,x2)p(xd|x1,,xd1)=i=1dp(xi|x<i). If each conditional p(xi|x<i) is modelled by a parametric distribution (say a Gaussian or categorical) whose parameters are output by a neural network, we obtain an autoregressive model. Generation proceeds sequentially: sample x1, then x2 given x1, and so on.

Insight.

Autoregressive models are the basis of modern large language models (GPT, LLaMA, etc.). Each token is generated by sampling from pθ(tokent|token<t), which is exactly the chain-rule factorisation applied to sequences. The probabilistic graphical model view of this factorisation is studied in Chapter 6.

The Push-Forward Distribution

We now formalise the idea of “transforming noise into data.”

Definition 2 (Push-Forward Distribution).

Let Z be a random variable with distribution pZ on 𝒵, and let g:𝒵𝒳 be a measurable function. The push-forward of pZ through g, denoted g#pZ, is the distribution of X=g(Z): (g#pZ)(A)=pZ(g1(A))for every measurable set A𝒳. Equivalently, for any test function f, 𝔼Xg#pZ[f(X)]=𝔼ZpZ[f(g(Z))].

The push-forward viewpoint unifies all generative models:

Key Idea.

The generative modelling problem is to find a map gθ such that gθ#pZpdata. Different model families differ in how they parameterise gθ (e.g. invertible network, decoder network, denoiser) and how they measure the approximation quality (e.g. KL divergence, Wasserstein distance, adversarial loss).

Connection to Change of Variables

When g is a diffeomorphism (smooth, invertible, with smooth inverse), the push-forward density can be computed explicitly via the change-of-variables formula from Chapter 2: (PUSH Forward Density)pX(x)=pZ(g1(x))|detg1x|. This equation is the backbone of normalising flows: the generator gθ is an invertible neural network, and the log-likelihood (NF Loglik)logpX(x)=logpZ(gθ1(x))+log|detgθ1x| can be maximised exactly, with no approximations needed.

Remark 3 (When g is Not Invertible).

If gθ is not invertible (as in GANs and VAEs), we cannot use (PUSH Forward Density). Instead, the push-forward distribution is only defined implicitly through samples. For GANs this is fine, since the discriminator never needs to evaluate pX(x). For VAEs, the marginal likelihood pθ(x)=pθ(x|z)p(z)dz is intractable, and we resort to variational bounds (Chapter 15).

The push-forward map gθ transforms a simple unimodal latent distribution (left) into a complex multimodal data distribution (right).

The Generative Modelling Problem

We now state the central problem of this book precisely.

Definition 3 (The Generative Modelling Problem).

Let x1,,xniidpdata be observed data. Find a generator gθ:𝒵𝒳 and a latent distribution pZ on 𝒵 such that gθ#pZpdata, where the notion of “” is made precise by a discrepancy measure 𝒟 (e.g. KL divergence, Wasserstein distance, or an adversarial criterion): (GEN Problem)θ^=arg minθ𝒟(pdata,gθ#pZ). Common choices for pZ include 𝒩(0,Id) (Gaussian) and Uniform([0,1]d).

Remark 4 (Why the Discrepancy 𝒟 Matters).

The choice of 𝒟 profoundly affects the behaviour of the resulting model:

  • KL divergence 𝖣KL(pdatapθ) leads to maximum likelihood training (Chapter 4).

  • Adversarial divergence yields GANs (Chapters 9–10).

  • Wasserstein distance gives Wasserstein GANs with better training stability (Chapter 10).

  • Score matching leads to diffusion models (Chapter 16).

Latent Variable Models

In practice, the data we observe (images, text, speech) lives in a very high-dimensional space, yet its intrinsic structure is often much lower-dimensional. Latent variable models make this explicit by introducing a hidden (latent) variable z that “explains” the observations.

Definition 4 (Latent Variable Model).

A latent variable model specifies:

  1. a prior p(z) over latent codes z𝒵, and

  2. a likelihood (decoder) pθ(x|z) that generates an observation given a latent code.

The marginal (observed) distribution is obtained by integrating out z: (Marginal Likelihood)pθ(x)=pθ(x|z)p(z)dz.

Graphical model for a latent variable model. The shaded circle indicates the observed variable x; the open circle is the latent z. The plate denotes n i.i.d. observations.

The Intractability Problem

The marginal likelihood involves an integral over the entire latent space, which is typically intractable: it cannot be computed in closed form when pθ(x|z) is parameterised by a neural network.

This intractability has two consequences:

  1. We cannot evaluate pθ(x) exactly, so standard MLE is not directly applicable.

  2. The posterior pθ(z|x)=pθ(x|z)p(z)/pθ(x) is also intractable, since it requires knowing pθ(x).

Different model families deal with this in different ways:

ModelStrategy for intractability
Normalising flowsMake gθ invertible so the integral is not needed
VAEsIntroduce an approximate posterior qϕ(z|x) and optimise a lower bound (ELBO)
GANsAvoid computing pθ(x) entirely; use an adversarial discriminator instead
Diffusion modelsDecompose generation into many small denoising steps, each tractable
How each model family sidesteps the intractability of pθ(x)=pθ(x|z)p(z)dz.

Insight.

Understanding why the marginal likelihood is intractable is the single most important conceptual step in generative modelling. Almost every architectural and algorithmic innovation you will encounter in Parts II–V is a creative response to this intractability.

The Reparameterisation Trick

Suppose we want to optimise an objective that involves an expectation over a parameterised distribution. For instance, in a VAE the ELBO contains 𝔼zqϕ(z|x)[logpθ(x|z)]. To perform gradient descent on ϕ, we need ϕ𝔼zqϕ[]. But the sampling operation zqϕ is not differentiable: how can we back-propagate through a random draw?

Definition 5 (Reparameterisation Trick).

Replace the parameterised sample zqϕ(z|x) with a deterministic transformation of parameter-free noise: (Reparam)z=μϕ(x)+σϕ(x)ϵ,ϵ𝒩(0,I), where μϕ and σϕ are neural networks outputting the mean and standard deviation.

Now z is a deterministic, differentiable function of ϕ (and ϵ), so the gradient ϕ𝔼ϵ𝒩(0,I)[logpθ(x|μϕ(x)+σϕ(x)ϵ)] can be estimated by Monte Carlo and back-propagated through the network as usual.

The reparameterisation trick moves the stochasticity out of the computational graph, enabling gradient-based optimisation. Left: without reparameterisation, gradients cannot flow through the stochastic sampling node. Right: with reparameterisation, the noise ϵ is an external input and gradients flow through the deterministic path.

Key Idea.

The reparameterisation trick is the key enabler of VAEs (Chapter 15) and also appears in the training of diffusion models (Chapter 16), where the noise ϵ added at each diffusion step is exactly the reparameterisation noise.

Evaluating Generative Models

How do we know whether a generative model produces “good” samples? This is surprisingly subtle; we need to assess both quality (do individual samples look realistic?) and diversity (does the model cover the full data distribution, or does it suffer from mode collapse?).

Definition 6 (Log-Likelihood).

For explicit models, the average test log-likelihood 1ntesti=1ntestlogpθ(xitest) directly measures how well the model explains held-out data. Higher is better. This metric is not available for implicit models like GANs.

Definition 7 (Fréchet Inception Distance (FID)).

The FID [1] compares the statistics of real and generated images in the feature space of a pre-trained Inception network. Let (μr,Σr) and (μg,Σg) be the mean and covariance of Inception features for real and generated images, respectively. Then (FID)FID=μrμg2+trace(Σr+Σg2(ΣrΣg)1/2). Lower FID indicates higher quality and diversity.

Remark 5 (Limitations of FID).

FID assumes that Inception features are Gaussian, a strong assumption. It also requires thousands of samples for a stable estimate. Nevertheless, FID remains the most widely used metric for image generation as of 2025.

Tractability and the Road Ahead

Let us take stock of the fundamental tension in generative modelling. We want a model that is simultaneously:

  1. Expressive: capable of modelling complex, multimodal distributions in high dimensions.

  2. Tractable: allowing efficient training and sampling.

  3. Likelihood-evaluable: permitting computation of pθ(x) for evaluation and comparison.

No single model achieves all three perfectly. fig:triangle illustrates the trade-off.

The “generative modelling triangle”: no single model class perfectly achieves expressiveness, tractability, and exact likelihood evaluation simultaneously.

The remainder of this book explores each major model family in depth:

  • Part II: Probabilistic Generative Models. Graphical models and Gaussian mixtures provide the classical foundation: exact inference is possible in small models, but approximate inference (EM, variational methods) is needed for larger ones.

  • Part III: Generative Adversarial Networks. GANs side-step likelihood computation entirely, using an adversarial game to train the generator. They excel at sample quality but can suffer from mode collapse and training instability.

  • Part IV: Variational and Transport Methods. VAEs and normalising flows provide principled likelihood-based training. VAEs use amortised variational inference; flows use invertible architectures for exact likelihood.

  • Part V: Diffusion Models. Diffusion models decompose the generation process into many small denoising steps, achieving state-of-the-art quality with stable training.

Each approach represents a different resolution of the expressiveness–tractability–likelihood triangle. The mathematical tools needed to understand these trade-offs (divergence measures, optimisation theory, and deep learning) are developed in the next two chapters.

Exercises

Exercise 1 (Inverse CDF for the Laplace Distribution).

The Laplace distribution with location μ and scale b has CDF F(x)=12+12sign(xμ)(1e|xμ|/b). Derive F1(u) and describe how to generate Laplace samples from uniform random numbers.

Exercise 2 (Box–Muller Verification).

Starting from U1,U2iidUniform(0,1), use the change-of-variables formula (Chapter 2) to prove that Z1 and Z2 in (BOX Muller) are independent 𝒩(0,1). Hint: first change to polar coordinates (R,Θ) with R=2lnU1 and Θ=2πU2.

Exercise 3 (Rejection Sampling Efficiency).

Suppose the target is p(x)=2x on [0,1] and the proposal is q(x)=Uniform(0,1). Find the smallest valid constant M, describe the rejection criterion, and compute the acceptance rate.

Exercise 4 (Importance Sampling Variance).

Let p(x)=𝒩(0,1) and f(x)=𝟏[x>4] (indicator of a rare event). Compare the variance of estimating 𝔼p[f(x)] using (a) naïve Monte Carlo and (b) importance sampling with proposal q(x)=𝒩(4,1). Which is more efficient and why?

Exercise 5 (MLE for a Gaussian Mixture).

Suppose the data is {2.1,1.8,2.3,3.1,2.9,3.2}. Explain why a single Gaussian is a poor fit. What would be a better model? Write down the log-likelihood for a two-component Gaussian mixture model p(x)=π1𝒩(x;μ1,σ12)+π2𝒩(x;μ2,σ22) and explain why direct maximisation is difficult. (We will solve this properly in Chapter 7 using the EM algorithm.)

Exercise 6 (Push-Forward of a Linear Map).

Let Z𝒩(0,Id) and g(z)=Az+b for Ad×d invertible and bd. Use (PUSH Forward Density) to show that X=g(Z)𝒩(b,AA).

Exercise 7 (Can a Linear Generator Model a Mixture?).

Suppose pdata(x)=12𝒩(3,1)+12𝒩(3,1) and Z𝒩(0,1). Prove that no affine map g(z)=az+b can produce a push-forward g#pZ=pdata. What does this imply about the expressiveness requirement for the generator?

Exercise 8 (Reparameterisation for the Exponential).

Show that if UUniform(0,1) and X=1λlnU, then XExp(λ). Write X in the form X=h(λ,U) for a deterministic function h. Is h differentiable with respect to λ? Explain how this enables gradient-based optimisation of λ.

Exercise 9 (Intractable Marginal Likelihood).

Consider a latent variable model with z𝒩(0,1) and x|z𝒩(tanh(wz+b),σ2) for learnable parameters w,b,σ. Write down the marginal likelihood p(x)=p(x|z)p(z)dz. Explain why this integral has no closed-form solution. Suggest two strategies for training this model nonetheless.

Exercise 10 (Connecting the Three Approaches).

Explain how (a) inverse-CDF sampling, (b) MLE with a parametric family followed by sampling, and (c) the push-forward framework are related. In what sense are (a) and (b) special cases of the push-forward viewpoint? Give a concrete example where all three coincide.

Exercise 11 (FID in One Dimension (Computational)).

Suppose the “real” features are {1,2,3,4,5} and the “generated” features are {1.5,2.5,3.5,4.5,5.5}. Compute the FID using (FID) in one dimension (where the matrix square root reduces to the ordinary square root). What happens to the FID if the generated features are {1,2,3,4,5} exactly?

References

  1. Gans trained by a two time-scale update rule converge to a local nash equilibrium

    Martin Heusel, Hubertus Ramsauer, Thomas Unterthiner, Bernhard Nessler, Sepp Hochreiter

    Advances in Neural Information Processing Systems, pp. 6626-6637 · 2017

    BibTeX
    @inproceedings{heusel2017gans,
      title={Gans trained by a two time-scale update rule converge to a local nash equilibrium},
      author={Heusel, Martin and Ramsauer, Hubertus and Unterthiner, Thomas and Nessler, Bernhard and Hochreiter, Sepp},
      booktitle={Advances in Neural Information Processing Systems},
      pages={6626--6637},
      year={2017}
    }

    Conference paper