Diffusion Models: A Unified View of Learning to Sample
Table of contents
Acknowledgement: I am deeply grateful to my friends and colleagues who helped review this manuscript, namely Thijs van der Plas, Jiahao Zhang, Wenbo Jing, and Yingjie Shao.
Generative diffusion models reframe sample generation as an iterative denoising process: starting from pure Gaussian noise, they learn to reverse a carefully designed forward diffusion that gradually corrupts data.
This blog presents a unified derivation of two foundational approaches---Denoising Diffusion Probabilistic Models (DDPM) and Denoising Diffusion Implicit Models (DDIM)---tracing them from their variational autoencoder roots. We show how the training objective emerges naturally from the evidence lower bound (ELBO), reducing to a simple noise-prediction loss, and how the sampling procedure can be understood through its connection to score matching. Within this unified view, DDIM arises as a deterministic limit of DDPM, enabling accelerated sampling through a non-Markovian forward process while preserving the same marginal distributions and training objective.
Prefix

Generative models can be viewed as the processes that sample from an unknown data distribution. In image generation, the data distribution represents all possible images, while in language generation it is the distribution over possible sequences of text. In this blog, we adopt this perspective to re-derive the formalism of a class of image generation models—specifically diffusion models, including denoising diffusion probabilistic models (DDPM) and denoising diffusion implicit models (DDIM)—which form the basis of state-of-the-art systems such as Stable Diffusion and DALL·E.
Intuitively, one might consider a Monte Carlo approach in which samples are drawn from a simple, known distribution and only those consistent with the target data distribution are retained. However, such naive rejection sampling from a simple proposal distribution is infeasible in high dimensions, as the probability of accepting a sample becomes vanishingly small. Suppose we want to generate an RGB image of size ; the dimensionality of the space is , and each dimension takes values in . Since natural images lie on a highly structured, low-dimensional subset of this space, most samples from a simple distribution are very unlikely to correspond to realistic images, making such approaches extremely inefficient.
A more efficient approach is to learn a transformation from a known, easy-to-sample distribution to the target distribution, so that each sample corresponds to a generated data point. Many approaches have been proposed to learn such transformations. For example, in Generative Adversarial Networks (GANs), a generator is trained adversarially against a discriminator to produce samples that match the target distribution, effectively discouraging samples that deviate from the data distribution. In Variational Autoencoders (VAEs), an encoder–decoder architecture is defined within a Bayesian framework, and the model is trained by maximizing a lower bound on the data likelihood. In DDPM, we explicitly define a forward Markov process that gradually transforms data into Gaussian noise, and learn the reverse process to generate data from noise. Training can be interpreted as maximizing a variational lower bound on the data likelihood, similar in the spirit of VAEs.
The following sections introduce diffusion models and their related methods. We first review the ELBO derivation through VAEs (Section VAE), then derive DDPM (Section DDPM) and DDIM (Section DDIM). Finally, we discuss connections to score-based models (Section Score).
When writing this essay, I also read and learned a lot from several blog posts, including:
- Luo (2022): Understanding Diffusion Models: A Unified Perspective
- Lai (2025): Principles of Diffusion Models
- Weng (2021): Diffusion Models Blog
- Song et al. (2021): Score-Based Generative Modeling
Notations
For convenience of later derivations, we list the required notations below.
Data and distributions
- , : data point drawn from the target distribution
- : unknown target distribution we want to sample from
- : learned model distribution parameterized by
- : latent variable drawn from a simple prior
- : prior distribution, usually
- : joint distribution of and
- : conditional distribution of data given latent variable
Variational Inference (VAE)
- : encoder and decoder parameters
- : approximate posterior (encoder)
- : decoder / likelihood
- : encoder outputs
Distributions and metrics
- : Gaussian distribution
- : KL divergence
- : expectation under
DDPM & diffusion process
- : timestep in
- : latent variable at timestep
- : noise schedule coefficient
- : Gaussian noise
- : forward process
- : learned reverse process
- : noise prediction network
- : predicted reverse mean
DDIM
- : stochasticity parameter
- : number of sampling steps
- : subset of timesteps
- : DDIM sampling distribution
Score-based models
- : score function
- : learned score network
- : noise scale
- : noisy marginal distribution
- : weighting function
Langevin dynamics
- : step size
- : injected noise
- : timestep index for score updates
Variational AutoEncoders
In this section, we discuss the foundations of diffusion models, along with key aspects of their implementation, including the learning objective and how it is derived. Due to page limitations, some topics—such as text guidance—are not covered.
As discussed earlier, one common approach to generative modeling is to learn a transformation from a known, easy-to-sample distribution to an unknown data distribution , which describes real data.
In general, this transformation is not available in closed form. Instead, it can be learned by defining a trainable objective (i.e., a loss function) that measures the difference between the model distribution and the data distribution , and optimizing it using a neural network. The optimal parameters then define a model that approximates the target distribution. Ifgn this way, the problem of generating data is reduced to defining and solving an optimization problem.
Assumption 1
There exists a sufficiently regular transformation from to that can be approximated by a neural network.
Assumption 1 ensures that the model has enough expressive power to represent the target distribution. This assumption is nontrivial, since not every pair of distributions can be related through such a transformation in a well-behaved manner. For example, when the target distribution is singular (such as a Dirac delta distribution) while the model distribution is continuous (such as a Gaussian), the Kullback--Leibler divergence can become infinite, indicating a fundamental mismatch between the two distributions. In such cases, directly approximating the target distribution can be challenging.
We now turn to how to define the optimization problem. For simplicity, we use a Gaussian distribution as the base distribution , leveraging its convenient mathematical properties.
Evidence Lower Bound (ELBO)
To define the optimization problem, a natural approach is to use likelihood-based learning. Given a dataset , the objective is to maximize the likelihood of the data under the model, i.e., maximize for all samples. A naive approach might be to sample latent variables from a Gaussian distribution and arbitrarily associate them with data samples , then optimize the likelihood based on these random pairings. However, this approach is flawed because such random pairings do not establish a meaningful relationship between the latent variables and the data samples. As a result, the model cannot learn how changes in the latent space correspond to variations in the data. Instead, it may rely on overall statistics of the dataset and largely ignore the latent variables. Under common training objectives, this can lead to outputs that resemble an average of the data distribution, rather than distinct and realistic samples.
This issue suggests that, to learn a meaningful transformation, it is not sufficient to consider only the marginal likelihood . Instead, we should explicitly model the relationship between and . To achieve this, we introduce the conditional distribution , which describes how a data point is generated from a latent variable . This provides the necessary structure to guide the learning process and establish a meaningful mapping between the latent space and the data space.
We introduce a variational distribution to approximate the intractable posterior , where denotes the parameters to be optimized. This allows us to reformulate the objective in terms of the marginal likelihood and work with its logarithm, , which is more convenient for optimization.
Consider log likelihood:
The derivation shows that it can be expressed as the sum of a lower bound and a KL divergence term between the approximate posterior and the true posterior. Since the KL divergence is non-negative, the resulting expression provides a lower bound on , which is referred to as the Evidence Lower Bound (ELBO). The ELBO is used as the optimization objective in VAEs. This is motivated by two key reasons. First, it provides a tractable lower bound on the log-likelihood, and maximizing it indirectly improves the data likelihood. Second, it introduces a framework for learning a structured latent representation, where we explicitly model how data points are generated from latent variables. Although the exact KL term between the approximate posterior and the true posterior is intractable, maximizing the ELBO avoids computing it directly. Since does not depend on , maximizing the ELBO is equivalent to making the approximate posterior closer to the true posterior.
We now rewrite the ELBO in a more interpretable form.
The resulting expression consists of two terms. The first term corresponds to a reconstruction objective, which encourages accurate generation of data from latent variables. The second term is a KL divergence between the approximate posterior and the prior , which regularizes the latent representation to stay close to a simple distribution.
In practice, this objective is optimized using two neural networks: one parameterizing the encoder , and the other parameterizing the decoder .
Training and Reparameterization
We just introduced ELBO, following we explain how a VAE is trained to maximize it. In VAEs, the encoder is modeled as a Gaussian distribution parameterized by , while the prior distribution over the latent variable is chosen to be a standard Gaussian:
Therefore, the encoder is trained to predict the mean and variance of the latent variable given a data sample .
The reconstruction term in the ELBO can be approximated using Monte Carlo sampling with latent variables sampled from . However, the sampling operation itself is not differentiable, preventing gradients from being propagated through the network during backpropagation. To address this issue, we use the reparameterization trick. Instead of directly sampling from , we first sample
and then compute
where denotes element-wise multiplication.
After training, the VAE consists of an encoder and a decoder . To generate new data, we first sample a latent variable from the prior distribution and then pass it through the decoder . In the generation process, only the decoder is used to produce samples, while the encoder is introduced during training to establish the relationship between the data space and the latent space.
Denoising Diffusion Probabilistic Models (DDPM)
Previously, we saw that in variational autoencoders (VAEs), the transformation from a known prior distribution to the data distribution is learned implicitly through an encoder–decoder pair. A limitation of this approach is that the relationship between data and latent variables is only captured through an approximate posterior, which introduces additional modeling and inference complexity.
An alternative approach is to construct a stepwise transformation from the data distribution to a Gaussian distribution. Intuitively, this can be understood as forming a linear combination of the original data and Gaussian noise. Over time, the contribution of the noise gradually increases while the contribution of the data decreases, eventually resulting in pure Gaussian noise.
Equivalently, this process can be defined as a stepwise Markov chain. For convenience, we introduce the following notation:
- : original data sample
- : latent variable at the final timestep (standard Gaussian noise)
- : intermediate latent state at timestep
where .
Under this notation, the forward process is defined as
By recursively applying this relation, we can express in terms of the original data . For example,
Continuing this expansion yields a weighted sum of independent Gaussian noises. Since a linear combination of independent Gaussian random variables is still Gaussian, we can merge the noise terms into a single Gaussian random variable .
The merged variance is
Repeating this process until yields the closed-form expression
where
This formulation shows that the forward process gradually destroys the structure of the data, and after sufficiently many steps, the distribution of approaches a standard Gaussian distribution.
Formulation of the Diffusion Process
We now summarize the diffusion process more formally.
To make the stepwise noising process meaningful, we assume that the transformation from the data distribution to a Gaussian distribution can be approximated through a sequence of small stochastic transitions. This assumption differs from Assumption 1, since the forward diffusion process is explicitly defined rather than learned by a neural network.
Several important assumptions and design choices are made in this formulation:
Markov Property
The forward process is assumed to satisfy the Markov property, meaning each state depends only on the previous state. This allows the joint distribution of the trajectory to be written as
Gaussian Transitions
Each transition is modeled as a Gaussian distribution, which enables the forward process to admit the closed-form expression derived above.
Variance-Preserving Process
The coefficients are chosen such that the overall scale of the samples remains stable throughout the diffusion process, preventing the signal from growing or shrinking uncontrollably.
Gaussian Terminal Distribution
After sufficiently many diffusion steps, the final state approaches a standard Gaussian distribution.
If we could reverse this process and iteratively sample from the reverse conditional distribution , we would be able to generate samples from the data distribution starting from Gaussian noise.
However, this reverse conditional is generally intractable to compute directly. Therefore, diffusion models instead learn a parameterized model to approximate the reverse diffusion process.
ELBO in Diffusion Models
We now have all the necessary components to formulate the diffusion model objective.
Unlike VAEs, the forward process is no longer parameterized by , but is instead explicitly defined as a fixed stochastic process. As a result, the learning problem focuses only on the reverse process , which serves as the generative component of the model.
Similar to the VAE formulation, we can derive a variational lower bound (ELBO) for the log-likelihood objective. The detailed derivation and intermediate steps are provided in the appendix.
After a sequence of algebraic manipulations, the ELBO can be written as
The terms in the above expression can be interpreted as follows:
Reconstruction term
measures how well the model can recover the original data from a slightly noisy sample, analogous to the reconstruction objective in VAEs.
Prior matching term
measures how close the final distribution of the forward diffusion process is to a standard Gaussian distribution. By construction, and for sufficiently large , this term is effectively constant during training.
Denoising matching term
trains the reverse process to approximate the true reverse posterior.
The reverse posterior is Gaussian and can be written as
where
and
Training the Diffusion Model and Sampling
Recall that the KL divergence between two Gaussian distributions simplifies considerably when their covariance matrices are fixed to be equal.
In that case, minimizing
is equivalent to minimizing the squared error between their means:
Using the forward-process relation
we can solve for :
Substituting this into the posterior mean yields
This suggests that, instead of directly predicting the posterior mean, we can train a neural network to predict the noise added at timestep .
The resulting optimization objective becomes
Here denotes the weighting term induced by the KL divergence.
In the simplified DDPM objective, is commonly set to , which empirically leads to improved sample quality.
In practice, the reconstruction term is also absorbed into the simplified denoising objective. Consequently, training is dominated by the noise-prediction loss, which teaches the model to estimate the Gaussian noise added at each diffusion timestep.
Training and Sampling Algorithm

In reverse sampling, is a tunable hyperparameter that controls how much random noise is injected at each timestep.
The overall training procedure of diffusion models can now be summarized as follows.
During training, we first sample a real data point from the dataset and randomly choose a timestep . Gaussian noise is then added according to
The noisy sample , together with the timestep , is passed into the neural network , which is trained to predict the noise that was added to the original data.
Because the timestep is sampled uniformly during training, the model learns to denoise samples across a wide range of noise levels.
After training, generation proceeds through the reverse diffusion process. We first sample , which corresponds to pure Gaussian noise.
The learned model then iteratively predicts and removes noise from the sample:
At each timestep, the model predicts the noise component in the current sample and uses it to estimate a less noisy sample from the previous timestep. After sufficiently many reverse steps, the final sample approximately follows the data distribution, producing a generated image.
Denoising Diffusion Implicit Models (DDIM)
The derivation of diffusion models follows a progressive sequence of steps:
Although the derivation is constructed through a stepwise Markov process, the final formulation exhibits two important characteristics:
Loss Function Independence:
The training objective depends only on the marginal distribution .Sampling Independence:
The sampling procedure depends only on the learned reverse process .
This observation suggests that the explicit Markovian structure of the forward process is not essential for defining the training objective. As long as the marginal distributions remain unchanged, different forward processes can lead to the same optimization objective. DDIM extends this idea by introducing a more general non-Markovian diffusion process.
DDIM Sampling and Non-Markovian Diffusion
In DDPM, the marginal distribution of conditioned on is Gaussian:
DDIM considers a non-Markovian forward process while preserving the same marginal distribution . The goal remains to generate starting from Gaussian noise . Unlike DDPM, where each reverse step involves sampling from a probability distribution, DDIM introduces an implicit deterministic mapping
From Eq. (29), we have the expression for . After training a DDPM model, a neural network can be used to approximate the noise component with . Therefore, both the original and estimated forms of can be written as
Applying the diffusion relation again at timestep gives
DDIM further decomposes the noise term into a deterministic component and a stochastic component:
where .
Substituting Eq. (31) into Eq. (33) gives
Eq. (34) defines the DDIM sampling process:
where the mean term in Eq. (35) is
We can treat as a hyperparameter that controls the stochasticity of the sampling process in Eq. (35). When , the sampling process becomes fully deterministic. When
the sampling process in Eq. (35) reduces to the original DDPM formulation.
During generation, it is not necessary to follow the entire sequence of timesteps . Instead, we may sample using only a subset of timesteps. Let denote two timesteps in this trajectory. The DDIM reverse update step is then given by Eq. (38),
This formulation gives DDIM two important advantages over DDPM:
By controlling the parameter , the sampling process can interpolate between stochastic and deterministic generation.
Since sampling does not need to follow the full sequence of timesteps, generation can be performed using only a subset (Eq. (39))
which significantly reduces the computational cost while maintaining comparable sample quality.
Connection to Score-based Models
Having introduced the basics of diffusion models, we now show the connection between diffusion models and score-based generative models.
Directly modeling the likelihood function is often intractable, and estimating it using methods such as Markov chain Monte Carlo (MCMC) or variational inference can be computationally expensive. Score-based models address this difficulty by modeling the score function instead of the probability density itself. The score function of a distribution is defined as
Rather than learning the density function directly, score-based models learn the gradient of the log-density. Similar to other likelihood-based generative models, such as VAEs, the objective is still related to maximizing likelihood, but the optimization is performed through score matching. The corresponding objective minimizes the Fisher divergence:
where denotes the learned score function.
An important advantage of score-based models is that they do not require a tractable normalizing constant for the probability density. Instead, the model directly learns a vector field describing the direction of increasing probability density.
However, score estimation in low-density regions is often inaccurate. Expanding the Fisher divergence gives
Since the weighting term is small in low-density regions, errors in these regions contribute little to the optimization objective. As a result, the learned score function is often inaccurate far from the data manifold.
Score-based Modeling with Multiple Noise Perturbations
To address this issue, score-based models perturb the data with Gaussian noise and train the score function on the perturbed distributions instead. Large noise levels improve coverage of low-density regions, while small noise levels preserve the structure of the original data distribution.
To balance these effects, multiple noise scales are introduced. Let denote a sequence of noise levels. Given a data sample , we generate a perturbed sample
The resulting perturbed distribution is
A noise-conditional score network (NCSN) is then trained to estimate the score function at different noise levels. The training objective becomes
Equivalence to Diffusion Models
The connection between score-based models and diffusion models comes from the fact that predicting the noise added to a sample is equivalent to estimating the score function.
In diffusion models, the forward conditional from Eq. (29) is . Differentiating the log-density with respect to yields
With the usual reparameterization from Eq. (22), , this becomes
Substituting into the score-matching objective and comparing with the DDPM noise-prediction loss in Eq. (25), we consider
These differ only by a scaling factor; in particular,
Therefore, training a diffusion model to predict noise is equivalent to training a score-based model.
Once the score is learned, one may sample using Langevin dynamics, where denotes the outer iterate index:
Replacing by the diffusion noise predictor at diffusion timestep gives
This highlights the close relationship between score-based sampling and diffusion-model denoising.
In the continuous-time limit, diffusion can be written as a stochastic differential equation (SDE), and the reverse process (together with a deterministic probability-flow ODE) links DDPM- and DDIM-style sampling to score-based dynamics.