Skip to content
AIAI Wranglers

19 Diffusion Models

Variational autoencoders, as developed in ch:vae,ch:vae2, learn a latent representation of data in a single encoding step. But what if we replaced that single step with a long chain of small, incremental corruptions, gradually transforming data into pure noise? This idea, rooted in non-equilibrium statistical physics, was first formalised by Sohl-Dickstein et al. [1] and later connected to variational inference by Ho et al. [2]. The result is a family of diffusion models that achieve remarkable sample quality by learning to reverse a carefully designed noise process. In this chapter we derive diffusion models as the natural limit of hierarchical VAEs, following the exposition of [5].

From Hierarchical VAEs to Diffusion Models

The standard VAE compresses an observation x0 into a single latent code 𝒛 and reconstructs from that code. A natural question is whether we can build a richer generative model by stacking multiple latent layers on top of one another, so that each layer captures progressively more abstract features. This is the idea behind hierarchical VAEs, and it turns out that diffusion models emerge as a special case when we push the hierarchy to its logical extreme.

Key Idea.

From VAEs to Hierarchical VAEs In ch:vae,ch:vae2 we built VAEs with a single latent layer: an encoder qϕ(𝒛|𝒙) maps data to a latent code, and a decoder pθ(𝒙|𝒛) maps back. Hierarchical VAEs extend this by introducing a chain of latent variables z1,z2,,zT, where each layer conditions on all previous layers. The encoder builds up a sequence of increasingly abstract representations, and the decoder reconstructs data by traversing the hierarchy in reverse.

Hierarchical VAE

Consider stacking multiple VAE layers into a fully connected directed acyclic graph (DAG), where each latent variable zi is conditioned on all preceding variables. The resulting graphical model has edges between every earlier node and every later node, as shown in fig:diff:hvae-dag.

Directed acyclic graph for a hierarchical VAE with fully connected dependencies. Each node xi is conditioned on all preceding nodes.
A hierarchical VAE with both forward (black) and backward (red) processes. The observed variable x1 is drawn larger to distinguish it from the latent variables z2,,z5.

Let pa(xi) denote the parents of xi in the DAG.\@ The joint distribution factorises according to the graph structure: q(x1,,xD)=i=1Dq(xi|pa(xi))(forward process only)

Applying the product rule explicitly, the forward joint distribution q(x1,,z5) becomes q(x1,,z5)=q(x1)q(z2|x1)q(z3|x1,z2)q(z4|x1,z2,z3)q(z5|x1,z2,z3,z4), where each latent variable zi is conditioned on all preceding variables. Similarly, the backward (generative) joint distribution factorises as p(x1,,z5)=p(z5)p(z4|z5)p(z3|z4,z5)p(z2|z3,z4,z5)p(x1|z2,z3,z4,z5), where each variable is now conditioned on all subsequent variables.

This fully connected hierarchy captures all possible dependencies, but it raises a practical concern: with T latent layers, each conditional depends on up to T1 variables. Can we simplify the model without losing too much expressive power?

The Markov Assumption

The fully connected hierarchical VAE requires each latent variable to condition on the entire history of preceding variables, making both inference and generation computationally expensive. A powerful simplification is to impose a Markov assumption: each variable depends only on its immediate predecessor. This reduces the graphical model from a fully connected DAG to a simple chain.

Graphical model for the Markovian forward process. Each node depends only on its immediate predecessor.
Graphical model for the Markovian backward (generative) process. Each node depends only on its immediate successor.
The Markovian VAE with both forward (top) and backward (bottom) transitions. A forward step from zt1 to zt is governed by qϕ(zt|zt1), and the corresponding backward step is pθ(zt1|zt).

Under the Markov assumption, the joint distribution simplifies dramatically: p(x,z1:T)=p(zT)pθ(x0|z1)t=2Tpθ(zt1|zt). Note that this generative model proceeds from zT to x0, with each transition depending only on the immediately preceding latent variable. The posterior distribution (denoting x0 as generic x) factorises as qϕ(z1:T|x)=qϕ(z1|x)t=2Tqϕ(zt|zt1).

The ELBO for this Markovian hierarchical VAE follows the same derivation as in 15: logp(x)=logp(x,z1:T)dz1:T(Obtain likelihood; integrate out z)=logp(x,z1:T)qϕ(z1:T|x)qϕ(z1:T|x)dz1:T(multiply and divide by qϕ)=log𝔼qϕ(z1:T|x)[p(x,z1:T)qϕ(z1:T|x)](write as expectation of qϕ)𝔼qϕ(z1:T|x)[logp(x,z1:T)qϕ(z1:T|x)](Jensen inequality)

Inserting the Markovian joint and posterior into the ELBO yields 𝔼qϕ(z1:T|x)[logp(x,z1:T)qϕ(z1:T|x)]=𝔼qϕ(z1:T|x)[logp(zT)pθ(x0|z1)t=2Tpθ(zt1|zt)qϕ(z1|x)t=2Tqϕ(zt|zt1)].

Historical Note.

The Thermodynamic Origins of Diffusion Sohl-Dickstein et al. [1] first proposed using diffusion processes from non-equilibrium thermodynamics as a foundation for generative modelling. The key insight was that if one can define a tractable process that gradually destroys structure in data, one can train a model to reverse this destruction. Five years later, Ho et al. [2] showed that this framework, when combined with a simplified training objective and careful noise scheduling, produces image samples rivalling those of GANs.

The Variational Diffusion Model

The Markovian VAE of the previous section still uses learned encoders qϕ(zt|zt1) with latent dimensions potentially smaller than the data dimension. The variational diffusion model (VDM) makes three key simplifications that transform this architecture into a diffusion model.

Key Idea.

Three Simplifications from MVAE to VDM

  1. The latent dimension equals the data dimension: dim(xt)=dim(x0) for all t.

  2. The encoder q(xt|xt1) is not learned; it is a fixed linear Gaussian.

  3. The Gaussian parameters are chosen so that q(xT|x0)𝒩(0,I).

The variational diffusion model. Compared to the Markovian VAE in fig:diff:mvae-full, all latent variables zt have been replaced by xt (same dimensionality as x0), and the forward encoder q is no longer parameterised.

Notice in fig:diff:vdm that every latent variable is now denoted xt rather than zt, reflecting the fact that latent and data spaces share the same dimensionality. The forward encoder q has no trainable parameters; only the backward decoder pθ is learned. With these changes, the posterior distribution becomes q(x1:T|x0)=t=1Tq(xt|xt1).

The Forward Process

Since the encoder is not learned, we must specify its form explicitly. The idea is to add a small amount of Gaussian noise at each step, with the noise level controlled by a variance schedule {αt}t=1T.

Definition 1 (Forward Process).

The forward process defines each transition as a linear Gaussian: (Forward)q(xt|xt1)=𝒩(xt;αtxt1,(1αt)I), where αt(0,1) for each t=1,,T. The full forward posterior factorises as q(x1:T|x0)=t=1Tq(xt|xt1). At each step, the mean shrinks the previous sample by αt and adds Gaussian noise with variance (1αt). By choosing the schedule {αt} appropriately, the distribution q(xT|x0) converges to 𝒩(0,I) for large T.

ELBO for the Variational Diffusion Model

We now derive the evidence lower bound for the VDM.\@ The derivation follows the same Jensen's inequality argument as in 15, but applied to the T-step Markov chain.

logp(x)=logp(x0:T)dx1:T=logp(x0:T)q(x1:T|x0)q(x1:T|x0)dx1:T[multiply and divide by q]=log𝔼q(x1:T|x0)[p(x0:T)q(x1:T|x0)]𝔼q(x1:T|x0)[logp(x0:T)q(x1:T|x0)][Jensen’s inequality]=𝔼q(x1:T|x0)[logp(xT)t=1Tpθ(xt1|xt)t=1Tq(xt|xt1)][definition of joint]=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)t=2Tpθ(xt1|xt)q(xT|xT1)t=1T1q(xt|xt1)][separate terms]=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(xT|xT1)t=1T1pθ(xt|xt+1)q(xt|xt1)][combine products] Continuing further, we split the logarithm and simplify the expectations: logp(x)𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(xT|xT1)]+t=1T1𝔼q(x1:T|x0)[logpθ(xt|xt+1)q(xt|xt1)][split log]=𝔼q(x1:T|x0)[logpθ(x0|x1)]+𝔼q(x1:T|x0)[logp(xT)q(xT|xT1)]+t=1T1𝔼q(x1:T|x0)[logpθ(xt|xt+1)q(xt|xt1)]= [Above, we split first log term in view of writing KL and reconstruction terms!]=𝔼q(x1|x0)[logpθ(x0|x1)]+𝔼q(xT1,xT|x0)[logp(xT)q(xT|xT1)]=+t=1T1𝔼q(xt1,xt,xt+1|x0)[logpθ(xt|xt+1)q(xt|xt1)] In the last equality, we replaced the joint q(x1:T|x0) by the marginals that each term actually depends on.

Theorem 1 (ELBO for VDM (First Form)).

logp(x)𝔼q(x1|x0)[logpθ(x0|x1)]𝔼q(xT1|x0)[DKL(q(xT|xT1)||p(xT))]t=1T1𝔼q(xt1,xt+1|x0)[DKL(q(xt|xt1)||pθ(xt|xt+1))]

Remark 1 (Interpretation of the Three ELBO Terms).

The three terms in Theorem 1 each have a clear interpretation:

  1. Reconstruction term: 𝔼q(x1|x0)[logpθ(x0|x1)] measures how well the decoder reconstructs x0 from the first latent step x1. This term is analogous to the reconstruction term in the standard VAE ELBO.

  2. Prior matching term: 𝔼q(xT1|x0)[DKL(q(xT|xT1)p(xT))] penalises deviations of the final noisy distribution from the Gaussian prior. This term has no trainable parameters and vanishes for sufficiently large T, since q(xT|x0) converges to 𝒩(0,I) by design.

  3. Consistency term: 𝔼q(xt1,xt+1|x0)[DKL(q(xt|xt1)pθ(xt|xt+1))] enforces that the learned backward transition pθ at each step is consistent with the forward transition q.

Applying Bayes' rule yields the true posterior q(xt1|xt,x0) (red), which runs parallel to the learned denoiser pθ(xt1|xt) (black). Matching these two distributions resolves the computational difficulty of the consistency term.

Remark 2 (Issues with the Consistency Term).

The consistency term 𝔼q(xt1,xt+1|x0)[DKL(q(xt|xt1)pθ(xt|xt+1))] requires computing an expectation over the joint q(xt1,xt+1|x0), which involves two correlated random variables. This is difficult to estimate efficiently. The remedy is to apply Bayes' rule: q(xt|xt1,x0)=q(xt1|xt,x0)q(xt|x0)q(xt1|x0). Note that we added x0 as a condition, which is valid since q(xt|xt1,x0)=q(xt|xt1) by the Markov property. As illustrated in fig:diff:bayes-consistency, this yields the true posterior q(xt1|xt,x0), which runs parallel to pθ(xt1|xt). Matching these two distributions will let us rewrite the ELBO in a more tractable form.

Revised ELBO via Bayes' Rule

We now rederive the ELBO, this time applying Bayes' rule to replace q(xt|xt1) with the tractable posterior q(xt1|xt,x0). Starting again from Jensen's inequality: (NEW ELBO LAST)logp(x)𝔼q(x1:T|x0)[logp(x0:T)q(x1:T|x0)]=𝔼q(x1:T|x0)[logp(xT)t=1Tpθ(xt1|xt)t=1Tq(xt|xt1)]=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)t=2Tpθ(xt1|xt)q(x1|x0)t=2Tq(xt|xt1)]=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(x1|x0)t=2Tpθ(xt1|xt)q(xt|xt1,x0)]=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(x1|x0)+logt=2Tpθ(xt1|xt)q(xt|xt1,x0)]=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(x1|x0)+logt=2Tpθ(xt1|xt)q(xt1|xt,x0)q(xt|x0)q(xt1|x0)] We now examine the product inside the last term. Separating it into two factors: t=2Tpθ(xt1|xt)q(xt1|xt,x0)q(xt|x0)q(xt1|x0)=t=2Tpθ(xt1|xt)q(xt1|xt,x0)×t=2Tq(xt1|x0)q(xt|x0)=[Inner terms (Nr. and Dr.) of second product gets canceled]=t=2Tpθ(xt1|xt)q(xt1|xt,x0)q(x1|x0)q(xT|x0) The second product telescopes, leaving only the boundary terms. Substituting back into : logp(x)𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(x1|x0)+logq(x1|x0)q(xT|x0)+logt=2Tpθ(xt1|xt)q(xt1|xt,x0)](cancel red)=𝔼q(x1:T|x0)[logp(xT)pθ(x0|x1)q(xT|x0)+t=2Tlogpθ(xt1|xt)q(xt1|xt,x0)]=𝔼q(x1:T|x0)[logpθ(x0|x1)]+𝔼q(x1:T|x0)[logp(xT)q(xT|x0)]+t=2T𝔼q(x1:T|x0)[logpθ(xt1|xt)q(xt1|xt,x0)]=𝔼q(x1|x0)[logpθ(x0|x1)]+𝔼q(xT|x0)[logp(xT)q(xT|x0)]+t=2T𝔼q(xt,xt1|x0)[logpθ(xt1|xt)q(xt1|xt,x0)]=𝔼q(x1|x0)[logpθ(x0|x1)]DKL(q(xT|x0)||p(xT))xt=2T𝔼q(xt|x0)[DKL(q(xt1|xt,x0)||pθ(xt1|xt))]

Theorem 2 (ELBO for VDM (Revised Form)).

(ELBO Revised)logp(x)𝔼q(x1|x0)[logpθ(x0|x1)]DKL(q(xT|x0)||p(xT))xt=2T𝔼q(xt|x0)[DKL(q(xt1|xt,x0)||pθ(xt1|xt))] The three terms have the following roles:

  1. 𝔼q(x1|x0)[logpθ(x0|x1)] is the reconstruction term, as before.

  2. DKL(q(xT|x0)p(xT)) is the prior matching term: it compares the final noisy distribution to the standard Gaussian prior.

  3. 𝔼q(xt|x0)[DKL(q(xt1|xt,x0)pθ(xt1|xt))] is the denoising matching term: it measures how well the learned backward step pθ matches the true posterior q(xt1|xt,x0).

Marginal Distribution q(xt|x0)

A crucial property of the linear Gaussian forward process is that we can compute the distribution of xt given x0 in closed form, without iterating through all intermediate steps.

Theorem 3 (Marginal Distribution).

(Marginal)q(xt|x0)=𝒩(xt;αtx0,(1αt)I),where αt=i=1tαi.

Proof.

xt=αtxt1+1αtϵt1,(where ϵt1𝒩(0,I))=αt(αt1xt2+1αt1ϵt2)+1αtϵt1(substitute xt1)=αtαt1xt2+αtαtαt1ϵt2+1αtϵt1=αtαt1xt2+αtαtαt12+1αt2ϵt2(used γϵi+ζϵjN(0,γ2+ζ2))=αtαt1xt2+αtαtαt1+1αtϵt2=αtαt1xt2+1αtαt1ϵt2(cancel αt)=(continue recursion, simplification)=(i=1tαi)x0+(1i=1tαi)ϵ0(after recursion)=αtx0+1αtϵ0(since α=i=1tαi)𝒩(xt;αtx0,(1αt)I)(x0 constantvar(x0)=0,E[x0]=x0).

True Posterior q(xt1|xt,x0)

The revised ELBO in Theorem 2 requires the true denoising posterior q(xt1|xt,x0). We obtain it by applying Bayes' rule and completing the square in the exponent of the resulting Gaussian.

Theorem 4 (True Denoising Posterior).

q(xt1|xt,x0)𝒩(xt1;αt(1αt1)xt+αt1(1αt)x01αt,(1αt)(1αt1)1αtI)𝒩(xt1;μq(xt,x0),Σq(t))

Proof.

We begin by applying Bayes' rule and substituting the known Gaussian forms from Definition 1 and Theorem 3: q(xt1|xt,x0)=q(xt|xt1,x0)q(xt1|x0)q(xt|x0)=𝒩(xt;αtxt1,(1αt)I)𝒩(xt1;αt1x0,(1αt1)I)𝒩(xt;αtx0,(1αt)I)exp{[(xtαtxt1)22(1αt)+(xt1αt1x0)22(1αt1)(xtαtx0)22(1αt)]}=exp{12[(xtαtxt1)21αt+(xt1αt1x0)21αt1(xtαtx0)21αt]}=exp{12[(2αtxtxt1+αtxt12)1αt+(xt122αt1xt1x0)1αt1+C(xt,x0))]}exp{12[2αtxtxt11αt+αtxt121αt+xt121αt12αt1xt1x01αt1]}=exp{12[(αt1αt+11αt1)xt122(αtxt1αt+αt1x01αt1)xt1]}

Remark 3 (The Constant C(xt,x0)).

The constant term dropped in the proof above is (THE Constant)C(xt,x0)=xt21αt+αt1x021αt1(xtαtx0)21αt. Since this term depends only on x0 and xt, both of which are given as conditions, it is treated as a constant with respect to xt1. We will reintroduce C when completing the square below.

Continuing the proof by simplifying the coefficient of xt12 and completing the square: q(xt1|xt,x0)=exp{12[αt(1αt1)+1αt(1αt)(1αt1)xt122(αtxt1αt+αt1x01αt1)xt1]}=exp{12[αtαt+1αt(1αt)(1αt1)xt122(αtxt1αt+αt1x01αt1)xt1]}=exp{12[1αt(1αt)(1αt1)xt122(αtxt1αt+αt1x01αt1)xt1]}=exp{12(1αt(1αt)(1αt1))[xt122(αtxt1αt+αt1x01αt1)1αt(1αt)(1αt1)xt1]}=exp{121αt(1αt)(1αt1)[xt122(αtxt1αt+αt1x01αt1)(1αt)(1αt1)1αtxt1]}=exp{12(1(1αt)(1αt1)1αt)[xt122αt(1αt1)xt+αt1(1αt)x01αtxt1]}𝒩(xt1;αt(1αt1)xt+αt1(1αt)x01αt,(1αt)(1αt1)1αtI)𝒩(xt1;μq(xt,x0),Σq(t)). This completes the proof. The constant C(xt,x0) that we dropped earlier is precisely the term needed to complete the square.

Remark 4 (Completing the Square).

To verify the final step, observe that (Complete Square)q(xt1|xt,x0)=exp{121αt(1αt)(1αt1)×[xt122αt(1αt1)xt+αt1(1αt)x01αtxt1]}𝒩(xt1;αt(1αt1)xt+αt1(1αt)x01αt,(1αt)(1αt1)1αtI)𝒩(xt1;μq(xt,x0),Σq(t)). To prove , use the constant C(xt,x0) from to complete the square in : (xt1αt(1αt1)xt+αt1(1αt)x01αt)2=xt122αt(1αt1)xt+αt1(1αt)x01αtxt1+(1αt(1αt)(1αt1))1C(xt,x0).

Remark 5 (Predetermined Variance).

Theorem 4 shows that at each step, xt1 given xt and x0 follows a Gaussian with mean μq(xt,x0) and variance Σq(t) that is predetermined by the schedule {αt}. The variance depends only on t, not on any trainable parameters. It can be written as Σq(t)=σq2(t)I, where (Sigma Q)σq2(t)=(1αt)(1αt1)1αt. The goal of training is therefore to align the approximate denoiser pθ(xt1|xt) with the ground-truth posterior q(xt1|xt,x0). Since both distributions are Gaussian with the same variance, matching them reduces to matching their means.

The Denoising Objective

Now that both q(xt1|xt,x0) and pθ(xt1|xt) are Gaussian with identical variance σq2(t)I, the KL divergence between them simplifies to a function of their means alone.

Theorem 5 (KL Divergence between Gaussians).

The Kullback-Leibler divergence between two Gaussian distributions is DKL(𝒩(x;μx,Σx)𝒩(y;μy,Σy))=12[log|Σy||Σx|d+tr(Σy1Σx)+(μyμx)Σy1(μyμx)].

Theorem 6 (Denoising Matching Objective).

When q and pθ share the same variance σq2(t)I, the denoising matching term reduces to argminθDKL(q(xt1|xt,x0)pθ(xt1|xt))=argminθ12σq2(t)μθμq22

Proof.

argminθDKL(q(xt1|xt,x0)pθ(xt1|xt))=argminθDKL(𝒩(xt1;μq,Σq(t))𝒩(xt1;μθ,Σq(t)))=argminθ12[log|Σq(t)||Σq(t)|d+tr(Σq(t)1Σq(t))+(μθμq)Σq(t)1(μθμq)]=argminθ12[log1d+d+(μθμq)Σq(t)1(μθμq)]=argminθ12[(μθμq)Σq(t)1(μθμq)]=argminθ12[(μθμq)(σq2(t)I)1(μθμq)]=argminθ121σq2(t)μθμq22.

Here μq is shorthand for μq(xt,x0) and μθ for μθ(xt,t). The denoiser pθ must therefore learn to predict the mean of the true posterior q.

The true mean is given by (Theorem 4):

Definition 2 (True Denoiser Mean).

μq(xt,x0)=αt(1αt1)xt+αt1(1αt)x01αt.

The approximate denoiser mirrors this formula but replaces the unknown x0 with a learned estimate:

Definition 3 (Approximate Denoiser Mean).

The approximate denoiser uses the mean (Approx MEAN)μθ(xt,t)=αt(1αt1)xt+αt1(1αt)x^θ(xt,t)1αt, where x^θ(xt,t) is a neural network that attempts to predict x0 from the noisy observation xt at timestep t.

Theorem 7 (x0-Prediction Loss).

Given the true denoiser mean μq(xt,x0) and the approximate denoiser mean μθ(xt,t) as defined above, the denoising matching objective simplifies to argminθDKL(q(xt1|xt,x0)pθ(xt1|xt))=argminθ12σq2(t)(αt1(1αt)1αt)2(x^θ(xt,t)x0)22.

Proof.

argminθDKL(q(xt1|xt,x0)pθ(xt1|xt))=argminθDKL(𝒩(xt1;μq,Σq(t))𝒩(xt1;μθ,Σq(t)))=argminθ12σq2(t)αt(1αt1)xt+αt1(1αt)x^θ(xt,t)1αtαt(1αt1)xt+αt1(1αt)x01αt22=argminθ12σq2(t)αt1(1αt)1αtx^θ(xt,t)αt1(1αt)1αtx022=argminθ12σq2(t)αt1(1αt)1αt(x^θ(xt,t)x0)22=argminθ12σq2(t)(αt1(1αt)1αt)2(x^θ(xt,t)x0)22.

Remark 6 (Interpretation of the x0-Prediction Loss).

Theorem 7 reveals that optimising the denoising matching term in the ELBO is equivalent to training a neural network x^θ(xt,t) to predict the clean data x0 from its noisy version xt. The time-dependent prefactor weights the loss differently at each noise level, but the core task is always the same: denoise. The model parameters θ are learned by supervised regression against ground-truth clean samples.

DDPM: Training and Inference

The mathematical machinery of the previous section (the ELBO decomposition, the closed-form posterior, and the x0-prediction loss) remained largely theoretical until Ho et al. [2] crystallised them into a practical algorithm called the Denoising Diffusion Probabilistic Model (DDPM). Their key contribution was not a new derivation but a concrete recipe: a simple training loop, a matching sampling loop, and a set of design choices (noise schedule, network architecture, loss weighting) that together produce high-quality images rivalling GANs.

The DDPM Loss Function

Combining the per-timestep KL terms from Theorem 2 into a single objective yields the full DDPM loss. Because every term reduces to an x0-prediction mean-squared error (as shown in the previous section), the summation takes a clean and interpretable form.

Definition 4 (DDPM Loss).

The full DDPM loss sums over all timesteps. It is the negation of the denoising-matching contribution to the ELBO, so minimising it maximises the bound: L(θ)=t=1T𝔼q(xt|x0)[12σq2(t)(αt1(1αt)2(1αt)2)x^θ(xt)x02]=t=1T[12σq2(t)(αt1(1αt)2(1αt)2)𝔼q(xt|x0)x^θ(xt)x02].

Remark 7 (Interpretation of the Loss).

This formulation of the ELBO highlights several critical aspects:

  • Reconstruction error. Each term within the summation directly relates to the reconstruction error between the predicted data x^θ(xt) and the original data x0. This error is scaled inversely by the noise variance σq2(t), which adjusts the impact of each timestep based on the model's noise schedule.

  • Noise schedule dependency. The noise schedule is explicitly integrated into the ELBO through the parameters αt and αt1. The terms involving these parameters adjust how the model weights the importance of errors at different timesteps, effectively shaping the learning trajectory.

  • Linearity of expectation. The expectation operator is linear, allowing the external factors like the variance scaling and noise parameters to be handled outside of the expectation. This property simplifies the computation and interpretation of the ELBO, making it computationally feasible to handle within the stochastic gradient descent framework typically used to train such models.

In practice, the summation over all timesteps is replaced by an expectation over a uniformly sampled timestep tUniform{1,,T}, yielding a stochastic estimate of L(θ) suitable for mini-batch gradient descent: L(θ)=T𝔼tU{1,T}[12σq2(t)(αt1(1αt)2(1αt)2)𝔼q(xt|x0)x^θ(xt)x02].

Training Algorithm

The training procedure is pleasingly simple: sample a data point, pick a random timestep, corrupt the data with the corresponding noise level, and take a gradient step to make the network's prediction closer to the original clean data.

Algorithm 1 (DDPM Training).

Algorithm 1: DDPM Training [2].

[H]

  1. repeat
  2. Sample x0pdata
  3. Sample tUniform{1,,T}
  4. Sample ϵ𝒩(0,I)
  5. xtαtx0+1αtϵ
  6. Take gradient step on θx^θ(xt,t)x02
  7. until converged

An important detail is the choice of αt, the cumulative product of the variance reduction factors up to timestep t. The selection of αt affects the trajectory of the noise addition and must be carefully calibrated to balance learning efficiency and model stability. We return to this question in Learned Noise Schedules.

Sampling and Inference

After training, the model generates new data by reversing the diffusion process. Starting from pure Gaussian noise xT𝒩(0,I), we iteratively apply the learned reverse transition xt1pθ(xt1|xt)=𝒩(xt1|μθ(xt),σq2(t)I)realised asxt1=μθ(xt)+σq(t)z,z𝒩(0,I), where the mean is given by μθ(xt)=(1αt1)αt1αtxt+(1αt)αt11αtx^θ(xt). At each step the network predicts a clean image x^θ(xt), which is blended with the current noisy state xt to produce the mean of the reverse step. Stochasticity enters through the noise term σq(t)z. At the final step (t=1) the noise is omitted, producing the deterministic output x0.

Algorithm 2 (DDPM Sampling).

Algorithm 2: DDPM Sampling [2].

[H]

  1. Sample xT𝒩(0,I)
  2. for t=T,T1,,1
  3. z𝒩(0,I) if t>1, else z=0
  4. xt1(1αt1)αt1αtxt+(1αt)αt11αtx^θ(xt,t)+σq(t)z
  5. return x0

Signal-to-Noise Ratio

A unifying lens through which to view the loss, the noise schedule, and the weighting of different timesteps is the signal-to-noise ratio (SNR). Recall that the forward process satisfies q(xt|x0)=𝒩(xt;αtx0,(1αt)I). The mean carries the “signal” αtx0 and the variance carries the “noise” (1αt).

Definition 5 (Signal-to-Noise Ratio).

For a Gaussian 𝒩(μ,σ2I), the signal-to-noise ratio is SNR=μ2/σ2. Applied to the forward process q(xt|x0), whose mean is αtx0, this gives αtx02/(1αt); for unit-scaled data (x0=1) this is (SNR)SNR(t)=αt1αt.

The SNR starts high at t=0 (mostly signal, little noise) and decreases monotonically toward zero as tT (mostly noise, little signal). A good noise schedule ensures that SNR(T)0, so that xT is nearly pure Gaussian noise, and that the decrease is smooth enough for the network to learn at every noise level.

Theorem 8 (Loss in Terms of SNR).

12σq2(t)αt1(1αt)2(1αt)2(x^θ(xt,t)x0)22=12(αt11αt1αt1αt)(x^θ(xt,t)x0)22.

Proof.

12σq2(t)αt1(1αt)2(1αt)2(x^θ(xt,t)x0)22=12(1αt)(1αt1)1αtαt1(1αt)2(1αt)2x^θ(xt,t)x022=121αt(1αt)(1αt1)αt1(1αt)2(1αt)2(x^θ(xt,t)x0)22=12αt1(1αt)(1αt1)(1αt)(x^θ(xt,t)x0)22=12αt1αt+αt1αtαt1αt(1αt1)(1αt)x^θ(xt,t)x022=12αt1(1αt)αt(1αt1)(1αt1)(1αt)x^θ(xt,t)x022=12(αt1(1αt)(1αt1)(1αt)αt(1αt1)(1αt1)(1αt))(x^θ(xt,t)x0)22=12(αt11αt1αt1αt)(x^θ(xt,t)x0)22.

Theorem 8 tells us that the per-timestep loss can be written purely in terms of the SNR drop between consecutive timesteps. In particular, the differential SNR between two consecutive timesteps gives the per-step loss a transparent interpretation: (SNR TIME)Lt(x)=12(αt11αt1αt1αt)x^θ(xt,t)x022=12(SNR(t1)SNR(t))x^θ(xt,t)x022. Each term is the product of an SNR drop and the network's prediction error at that noise level. Timesteps where the SNR changes rapidly contribute more to the loss, while timesteps where the SNR is nearly constant contribute little. For the standard cosine and linear-β schedules the drop is largest at small t, so this weighting emphasises the low-noise end of the chain, which is one reason Ho et al. [2] found that discarding the weight altogether improves perceptual sample quality. Minimising Lt(x) is thus synonymous with denoising: the model learns to reverse the noise added during the forward process, reconstructing x0 from xt with increasing accuracy as training progresses.

Learned Noise Schedules

The noise schedule {αt}t=1T (equivalently, the SNR schedule) is a design choice that profoundly affects sample quality. Two classical families are:

  • Variance preserving (VP) [1]: the forward process keeps the total variance of xt constant at every step; in the parameterisation of Definition 1 this means αt=1σt2, where σt2 is the variance added at step t.

  • Variance exploding (VE) [3]: the signal coefficient on x0 is held at 1 and only the noise variance grows over time, so xt=x0+σtϵ. This does not fit the (αt,1αt) parameterisation of Definition 1 directly; the two are related by the rescaling xtVE=xtVP/αt, which leaves the signal-to-noise ratio unchanged.

Rather than hand-crafting the schedule, Kingma et al. [4] proposed learning it end-to-end by parameterising the SNR directly with a monotonic neural network ωη(t): (Learned SNR)SNR(t)=exp(ωη(t)), where ωη is constrained to be monotonically increasing so that the SNR decreases with t. From this parameterisation the cumulative signal coefficient follows immediately: (Alpha Sigmoid)αt1αt=exp(ωη(t))αt=exp(ωη(t))1+exp(ωη(t))=σ(ωη(t)), where σ() denotes the sigmoid function. Equivalently, αt=1σ(ωη(t)). Because ωη is a neural network trained jointly with the denoiser x^θ, the model can adapt its noise schedule to the data, yielding tighter variational bounds with no manual tuning.

The Continuous-Time Limit

A natural question is whether using more timesteps always helps. The answer is yes, and the argument is elegant. From (SNR TIME), the loss for T evenly-spaced timesteps is LT(x)=12𝔼ϵ𝒩(0,I)[i=1T(SNR(s(i))SNR(t(i)))xx^θ(xt(i),t(i))22], with s(i)=(i1)/T and t(i)=i/T. Introducing the midpoint t=t0.5/T and adding and subtracting SNR(t), we can write (SNR LT)LT(x)=12𝔼ϵ𝒩(0,I)[i=1T(SNR(s)SNR(t)+SNR(t)SNR(t))xx^θ(xt,t)22]. On the other hand, the loss for 2T timesteps splits into two sums: (SNR L2T)L2T(x)=12𝔼ϵ𝒩(0,I)i=1T(SNR(s)SNR(t))xx^θ(xt;t)22+i=1T(SNR(t)SNR(t))xx^θ(xt,t)22.

Subtracting (SNR LT) from (SNR L2T) immediately gives L2T(x)LT(x)=12𝔼ϵ𝒩(0,I)[i=1T(SNR(s)SNR(t))(xx^θ(xt;t)22xx^θ(xt,t)22)].

Since t>s and SNR() is monotonically decreasing, SNR(s)SNR(t) is positive. Moreover, because t<t, the sample xt is a less noisy version of x compared to xt. A reasonable denoiser should therefore predict x more accurately from xt than from xt, implying xx^θ(xt;t)22xx^θ(xt;t)22<0. Multiplying a positive factor by a negative factor gives L2T(x)LT(x)<0: doubling the number of timesteps always decreases the variational bound.

Historical Note.

Kingma et al. [4] formalised this argument and used it to advocate for the continuous-time variational diffusion model (VDM), corresponding to the limit T. In the continuous-time limit the discrete sum becomes an integral over t[0,1], and the loss takes the form L(x)=1201SNR(t)xx^θ(xt,t)22dt (the minus sign is required because SNR is decreasing, so SNR(t)<0), which can be estimated with a single uniformly sampled t per training step, exactly as in the discrete case, but without any discretisation error in the bound.

Three Parameterisations of the Diffusion Loss

The denoising objective derived in the preceding sections can be equivalently expressed in terms of predicting the original data x0, the noise ϵ0, the score xtlogq(xt), or an energy function Eθ(xt). All four viewpoints lead to the same optimum, but they emphasise different aspects of the problem and have motivated distinct lines of research.

Predicting the Original Image (x0-Prediction)

The most direct parameterisation, analysed in Theorem 7, trains a network x^θ(xt,t) to predict the clean data x0 from its noisy observation xt. The per-timestep loss is arg minθ12σq2(t)(αt1(1αt)1αt)2(x^θ(xt,t)x0)22. This formulation is intuitive: the network literally learns to “see through” the noise and reconstruct the original image. However, the weighting factor in front of the squared error can vary over several orders of magnitude across timesteps, which may complicate optimisation.

Predicting the Noise (ϵ-Prediction)

Rather than predicting x0 directly, we can reparameterise the network to predict the noise ϵ0 that was added to produce xt. Since xt=αtx0+1αtϵ0, knowing ϵ0 is equivalent to knowing x0, but the loss landscape may differ. We first express the true posterior mean in terms of ϵ0.

Theorem 9 (True Mean in Terms of Noise).

With xt=αtx0+1αtϵ0, which implies x0=xt1αtϵ0αt, the true posterior mean satisfies μq(xt,x0)=1αtxt1αt1αtαtϵ0.

Proof.

μq(xt,x0)=αt(1αt1)xt+αt1(1αt)x01αt=αt(1αt1)xt+αt1(1αt)(xt1αtϵ0αt)1αt=αt(1αt1)xt+(1αt)(xt1αtϵ0αt)1αt=αt(1αt1)xt1αt+(1αt)xt(1αt)αt(1αt)1αtϵ0(1αt)αt=(αt(1αt1)1αt+1αt(1αt)αt)xt(1αt)1αt(1αt)αtϵ0=(αt(1αt1)(1αt)αt+1αt(1αt)αt)xt1αt1αtαtϵ0=αtαt+1αt(1αt)αtxt1αt1αtαtϵ0=1αt(1αt)αtxt1αt1αtαtϵ0=1αtxt1αt1αtαtϵ0.

In view of Theorem 9, we define the mean of the approximate reverse process by replacing the unknown true noise ϵ0 with a learned prediction ϵ^θ(xt,t): μθ(xt,t)=1αtxt1αt1αtαtϵ^θ(xt,t). Substituting this into the KL divergence between the true and approximate posteriors yields the noise-prediction loss.

Theorem 10 (Noise Prediction Loss).

arg minθDKL(𝒩(xt1;μq,Σq(t))𝒩(xt1;μθ,Σq(t)))=arg minθ12σq2(t)[1αt1αtαt(ϵ0ϵ^θ(xt,t))22].

Proof.

arg minθDKL(q(xt1|xt,x0)pθ(xt1|xt))=arg minθDKL(𝒩(xt1;μq,Σq(t))𝒩(xt1;μθ,Σq(t)))=arg minθ12σq2(t)[1αtxt1αt1αtαtϵ^θ(xt,t)1αtxt+1αt1αtαtϵ022]=arg minθ12σq2(t)[1αt1αtαtϵ01αt1αtαtϵ^θ(xt,t)22]=arg minθ12σq2(t)[1αt1αtαt(ϵ0ϵ^θ(xt,t))22]=arg minθ12σq2(t)(1αt)2(1αt)αt[ϵ0ϵ^θ(xt,t)22].

The final line shows that, up to a timestep-dependent weight, the ϵ-prediction loss is simply the mean-squared error between the true noise ϵ0 and the network's estimate ϵ^θ(xt,t). This is the loss used in the original DDPM paper [2], where Ho et al. [2] further observed that dropping the weighting factor and training with the unweighted objective ϵ0ϵ^θ(xt,t)2 gives better sample quality in practice, albeit at the cost of no longer optimising a proper variational bound.

Predicting the Score (s-Prediction)

A third viewpoint replaces the noise network with a score network sθ(xt,t) that estimates the gradient of the log marginal density xtlogq(xt). The corresponding loss is arg minθ12σq2(t)(1αt)2αt[sθ(xt,t)logp(xt)22]. We defer the full derivation and the connection to score matching to Score Functions and Diffusion, but note here that it is closely related to ϵ-prediction. For the conditional density the identity is exact, xtlogq(xt|x0)=ϵ0/1αt, while for the marginal it holds only in conditional expectation, xtlogq(xt)=𝔼[ϵ0|xt]/1αt, a consequence of Tweedie's formula (Theorem 11). Consequently, a score network and a noise network carry the same information.

Predicting the Energy

Finally, one may parameterise the model as an energy function Eθ(xt) whose negative gradient approximates the score. The loss becomes arg minθ12σq2(t)(1αt)2αt[xtEθ(xt)xtE(xt)22]. This viewpoint connects diffusion models to the broader family of energy-based models (EBMs). Because the score is s(xt)=xtE(xt), this is mathematically identical to score prediction, but it opens the door to architectures and training tricks developed in the EBM literature.

Summary of Parameterisations

Insight.

All four parameterisations (x0-prediction, ϵ-prediction, score prediction, and energy prediction) are mathematically equivalent: they share the same global optimum and differ only in how the network output is interpreted. The relationships are:

  • x0-prediction: the network x^θ(xt,t) directly estimates the clean data.

  • ϵ-prediction: the network ϵ^θ(xt,t) estimates the noise that was added. Used by DDPM [2].

  • Score prediction: the network sθ(xt,t) estimates the gradient xtlogq(xt). Central to score-based generative modelling [3].

  • Energy prediction: the network Eθ(xt) defines an energy whose negative gradient gives the score.

They are linked by the identities ϵ^θ=xtαtx^θ1αt,sθ=ϵ^θ1αt,x^θ=xt1αtϵ^θαt. In practice the choice is largely one of convenience and training stability: ϵ-prediction tends to give uniform loss magnitude across timesteps, while x0-prediction can be more interpretable and is preferred in some recent architectures [4].

Score Functions and Diffusion

So far we have derived the diffusion loss from a purely variational perspective, treating the reverse process as a learned Gaussian and optimising the ELBO. A complementary viewpoint starts from unnormalised probability models and asks: can we train a model without ever computing the intractable normalisation constant? The answer is yes, and the key object is the score function.

Unnormalised Probability Models

Let fθ(𝒙) be a real-valued function defined over the vector space 𝒙D, where D denotes the dimensionality of the input space. This function fθ(𝒙) is parameterised by θ, which represents a set of parameters that govern the behaviour of the function.

Definition 6 (Energy-Based Model).

Given fθ(𝒙), we can construct a probability distribution over 𝒙 using (Unnormalized)pθ(𝒙)=efθ(𝒙)Zθ, where Zθ is the partition function Zθ=Defθ(𝒙)d𝒙, assumed to exist and be finite. Because evaluating Zθ is generally intractable, the distribution pθ(𝒙) in (Unnormalized) is called an unnormalised probability model.

Despite the unknown normalisation constant, the unnormalised distribution pθ(𝒙) still encodes the relative likelihood of different values of 𝒙 under the parameterisation θ. The crucial observation is that the gradient of logpθ(𝒙) does not depend on Zθ.

Definition 7 (Score Function).

Let pθ(𝒙) be a parameterised density. The score is the gradient of the log-density with respect to the random variable: 𝒙logpθ(𝒙). For the unnormalised model in (Unnormalized), the score reduces to 𝒙logpθ(𝒙)=𝒙fθ(𝒙), since 𝒙logZθ=0.

Note that the score does not depend on the intractable partition function Zθ; however, maximum likelihood estimation would, because logpθ(𝒙)=fθ(𝒙)logZθ. This motivates score matching as an alternative training principle, which we develop in Score Matching Methods.

From Denoising to Scores: Tweedie's Formula

Understanding the score function in the context of diffusion models involves applying a classical statistical result known as Tweedie's formula. This formula provides a method for predicting the true posterior mean of a variable from its distribution and the gradient of its log probability.

Theorem 11 (Tweedie's Formula).

For a Gaussian variable 𝒛𝒩(𝒛;μz,Σz), the conditional expectation of the mean given the observation is (Tweedie)𝔼[μz|𝒛]=𝒛+Σz𝒛logp(𝒛).

This expression tells us that the expected mean can be adjusted by a term that scales with the variance and the score of 𝒛.

In diffusion models the forward noising process gives q(𝒙t|𝒙0)=𝒩(𝒙t;αt𝒙0,(1αt)I). Applying Tweedie's formula with μz=αt𝒙0 and Σz=(1αt)I, we obtain αt𝔼[𝒙0|𝒙t]=𝒙t+(1αt)logp(𝒙t), so that the clean data can be recovered in conditional expectation as (Tweedie X0)𝔼[𝒙0|𝒙t]=𝒙t+(1αt)logp(𝒙t)αt. The conditional expectation matters: a single 𝒙t does not determine 𝒙0, and 𝔼[𝒙0|𝒙t] is precisely what a trained denoiser x^θ(𝒙t,t) learns to output. This provides a method to reconstruct 𝒙0 from 𝒙t, leveraging the score to adjust for the noise added during the forward process.

Theorem 12 (Score-Based Denoising Mean).

The posterior mean of the reverse transition can be expressed as μq(𝒙t,𝒙0)=1αt𝒙t+1αtαtlogp(𝒙t).

Proof.

We have μq(xt,x0)=αt(1αt1)xt+αt1(1αt)x01αt=αt(1αt1)xt+αt1(1αt)xt+(1αt)logp(xt)αt1αt=αt(1αt1)xt+(1αt)xt+(1αt)logp(xt)αt1αt=αt(1αt1)xt1αt+(1αt)xt(1αt)αt+(1αt)(1αt)logp(xt)(1αt)αt=(αt(1αt1)1αt+1αt(1αt)αt)xt+1αtαtlogp(xt)=(αt(1αt1)(1αt)αt+1αt(1αt)αt)xt+1αtαtlogp(xt)=αtαt+1αt(1αt)αtxt+1αtαtlogp(xt)=1αt(1αt)αtxt+1αtαtlogp(xt)=1αtxt+1αtαtlogp(xt).

As expected, an approximation to the denoising transition mean μθ(𝒙t,t) is μθ(𝒙t,t)=1αt𝒙t+1αtαtsθ(𝒙t,t), where sθ(𝒙t,t) is a neural network trained to approximate the score logp(𝒙t).

Theorem 13 (Score Prediction Loss).

The KL divergence between the forward posterior and the learned reverse transition reduces to a score-matching objective: arg minθDKL(q(xt1|xt,x0)pθ(xt1|xt))=arg minθ12σq2(t)(1αt)2αt[sθ(xt,t)logp(xt)22].

Proof.

arg minθDKL(q(xt1|xt,x0)pθ(xt1|xt))=arg minθDKL(𝒩(xt1;μq,Σq(t))𝒩(xt1;μθ,Σq(t)))=arg minθ12σq2(t)[1αtxt+1αtαtsθ(xt,t)1αtxt1αtαtlogp(xt)22]=arg minθ12σq2(t)[1αtαtsθ(xt,t)1αtαtlogp(xt)22]=arg minθ12σq2(t)[1αtαt(sθ(xt,t)logp(xt))22]=arg minθ12σq2(t)(1αt)2αt[sθ(xt,t)logp(xt)22].

The Score as Noise

There is a striking algebraic identity connecting the score function to the noise ϵ0 added during the forward process. Recall from (Tweedie X0) and the forward reparameterisation 𝒙t=αt𝒙0+1αtϵ0 that 𝒙0=𝒙t+(1αt)logp(𝒙t)αt=𝒙t1αtϵ0αt. Equating the numerators and solving gives (1αt)logp(𝒙t)=1αtϵ0,logp(𝒙t)=11αtϵ0. This shows that the score is proportional to the negative of the noise added to the data, scaled by a factor that depends on αt. The identity is exact for the conditional score logq(𝒙t|𝒙0); for the marginal score it holds with ϵ0 replaced by 𝔼[ϵ0|𝒙t], consistently with (Tweedie X0).

Insight.

Score, Noise, and Denoising The three views of the learned signal are equivalent:

  • Score: logp(𝒙t) points toward regions of higher probability density.

  • Noise: ϵ0/1αt points away from the noise that was added.

  • Denoising: both representations enable recovery of 𝒙0 from 𝒙t via Tweedie's formula.

Predicting the score, predicting the noise, and predicting the clean data are all reparameterisations of the same underlying objective.

Langevin Dynamics

Given access to the score function 𝒙logp(𝒙), we can draw approximate samples from p(𝒙) without ever evaluating p(𝒙) itself. The idea is to initialise a particle at an arbitrary location and iteratively nudge it in the direction of increasing log-probability, while adding noise to ensure exploration. This is the essence of Langevin dynamics.

Algorithm 3 (Langevin Dynamics Sampling).

Algorithm 3: Langevin Dynamics.

[H]

  1. Initialise 𝒙0 arbitrarily
  2. for k=0,1,2,,K1
  3. Sample 𝒛k𝒩(0,I)
  4. 𝒙k+1𝒙k+ϵ2𝒙logp(𝒙k)+ϵ𝒛k
  5. return 𝒙K

The update rule balances a deterministic “drift” term ϵ2𝒙logp(𝒙k) that pushes toward higher density with a stochastic “diffusion” term ϵ𝒛k that prevents the chain from collapsing to a mode. As the step size ϵ0 and the number of steps K (with Kϵ), the distribution of 𝒙K converges to p(𝒙) [3].

In practice, several challenges complicate Langevin sampling:

  • Manifold hypothesis. Real data often lives on a low-dimensional manifold embedded in D. The score is undefined off the data manifold, so Langevin dynamics may behave poorly in the ambient space.

  • Low-density regions. Between modes of p(𝒙) the density is very small, so the estimated score is unreliable. The chain may get “stuck” before finding the next mode.

  • Mixing between modes. Even with infinite time, mixing between well-separated modes can be exponentially slow.

Noise-conditional score models, which train a separate score for each noise level, address these issues by providing reliable score estimates at every scale [3].

Historical Note.

Langevin and Brownian Motion Paul Langevin introduced the Langevin equation in 1908 to describe Brownian motion of particles suspended in a fluid. His stochastic differential equation modelled the balance between a viscous drag force and random thermal kicks from surrounding molecules. Over a century later, the same equation drives modern generative modelling: the “drag” is the score function pulling samples toward data, and the “kicks” are injected Gaussian noise that ensures the chain explores the full distribution.

Score Matching Methods

The score function is attractive because it avoids the intractable partition function. But how do we train a model to output accurate scores when we cannot evaluate the true data score 𝒙logpdata(𝒙)? This section surveys four solutions: Fisher divergence minimisation, implicit (Hyvärinen) score matching, denoising score matching, and sliced score matching.

Fisher Divergence

Definition 8 (Fisher Divergence and Score Matching).

Score matching trains an unnormalised distribution by minimising the Fisher divergence rather than the negative log-likelihood: 12𝔼pdata[𝒙logpdata(𝒙)𝒙logpθ(𝒙)22].

The first term inside the norm involves 𝒙logpdata(𝒙), which is unknown. Apart from density estimation, scores can be used to generate samples via Langevin dynamics as we have seen. Hence, we need effective methods to estimate scores without access to the true data score [3].

Implicit Score Matching

A tractable score-matching objective would be one that depends only on pθ(𝒙). The following classical result achieves exactly this.

Theorem 14 (Hyvärinen's Theorem).

Under regularity conditions [5], 12𝔼pdata [xlogpdata (x)xlogpθ(x)22]=𝔼pdata[tr(x2logpθ(x))+12xlogpθ(x)2]+const.

Proof.

(1D Case) We will prove for 1D case. We have 12𝔼pdata [(xlogpdata (x)xlogpθ(x))2]Using definition of expectation, we have=12pdata (x)(xlogpdata (x)xlogpθ(x))2 dxTerm independent of θ is constant, we separate them as follows=12pdata (x)(xlogpdata (x))2 dxconst +12pdata (x)(xlogpθ(x))2 dxpdata (x)xlogpθ(x)xlogpdata (x)dx.

For the last term, using integration by parts, we have

pdata (x)xlogpθ(x)xlogpdata (x)dxWe note that pdata (x)xlogpdata (x)=xpdata (x),we have=xlogpθ(x)xpdata (x)dxUsing integration by parts: udv=uvvduwith u=xlogpθ(x),v=xpdata (x),we have=pdata (x)xlogpθ(x)|+pdata (x)x2logpθ(x)dxif we assume pdata (x)0 when |x|,we have=𝔼pdata [x2logpθ(x)]. Substituting the results of integration by parts above into the 1D Fisher divergence (LHS of theorem), we obtain 12𝔼pdata [(xlogpdata (x)xlogpθ(x))2]=𝔼pdata [x2logpθ(x)]+12𝔼pdata [(xlogpθ(x))2]+ const. 

Proof.

(General case) Expanding the quadratic and dropping the constant term, we have arg minθ𝔼xp[12sθ(x)xlogp(x)22]=arg minθ𝔼xp[12sθ(x)2sθ(x)Txlogp(x)]. So we just need to show that the inner product term is the negative of tr(xsθ(x)), where sθ(x)=xlogpθ(x). Using integration by parts 𝔼xp[sθ(x)Txlogp(x)]=i=1d𝒳sθ(x)ilogp(x)xip(x)dx=i=1d𝒳sθ(x)ip(x)xidx=i=1d𝒳sθ(x)ixip(x)dx=𝒳tr(xsθ(x))p(x)dx=𝔼xp[tr(xsθ(x))].

Example 1 (Score Matching for a Gaussian).

The probability density function is given by p(x;M,μ)=1Z(M,μ)exp(12(xμ)TM(xμ)), where M is a symmetric positive-definite matrix (the inverse of the covariance matrix). Here our parameter θ={M,μ}. Here Z for Gaussian is well-known, but use this example as illustration. Here x is a general n-dimensional vector. We have q(x,M,μ)=exp(12(xμ)TM(xμ)), and we obtain xlogq(x;M,μ)=M(xμ) and i(xlogq(x;M,μ))=mii. Thus, we obtain the estimate (expectation over T samples) in the RHS of Theorem 14 as follows L~(M,μ)=1Tt=1T[imii+12(x(t)μ)TMM(x(t)μ)]. To minimize this with respect to μ, it is enough to compute the gradient (second order convexity) μL~=MMμMM1Tt=1Tx(t), which is zero if and only if μ is the sample average 1Tt=1Tx(t). This is a global minimum because the matrix MM that defines the quadratic form is positive-definite (second order critera for convexity). Similarly, we compute the gradient with respect to M, which gives ML~=I+M12Tt=1T(x(t)μ)(x(t)μ)T+12T[t=1T(x(t)μ)(x(t)μ)T]M, which is zero if and only if M is the inverse of the sample covariance matrix 1Tt=1T(x(t) μ)(x(t)μ)T, which thus gives the score matching estimate.

Remark 8.

Quoting Hyvärinen (2005) [5]: “Interestingly, we see that score matching gives exactly the same estimator as maximum likelihood estimation. In fact, the estimators are identical for any sample (and not just asymptotically). The maximum likelihood estimator is known to be consistent, so the score matching estimator is consistent as well.”

Denoising Score Matching

A different route to tractable score matching starts from a practical observation: if we corrupt data with known noise, the score of the noisy distribution can be computed in closed form. Given a training dataset {x()}=1L drawn from a clean distribution, the denoising score matching loss trains a score network sθ by minimising θ=arg minθ1L=1L12sθ(x()+σz())+z()σ2,z()𝒩(0,I). The model learns to predict the negative gradient of the log conditional probability of the noisy data, which for Gaussian noise q(𝒙|𝒙)=𝒩(𝒙|𝒙,σ2I) equals (𝒙𝒙)/σ2=𝒛/σ when 𝒙=𝒙+σ𝒛.

A natural theoretical question is whether this denoising objective is consistent with the original (explicit) score matching loss. Vincent (2011) showed that the two are equivalent up to a constant.

Theorem 15 (Vincent's Theorem).

For up to a constant C which is independent of θ, it holds that: LDSM(θ)=LESM(θ)+C.

Proof.

We recall the explicit score matching loss function LESM(θ)=𝔼q(x)[12sθ(x)xlogq(x)2]=𝔼q(x)[12sθ(x)2sθ(x)Txlogq(x)+12xlogq(x)2], where the last term is a constant because it is independent of θ. Looking at the second term, we have

(Vincent Expectation)𝔼q(x)[sθ(x)Txlogq(x)]=(sθ(x)Txlogq(x))q(x)dx=(sθ(x)Txq(x)q(x))q(x)dx=sθ(x)Txq(x)dx. Above, the first equality is by definition of expectation, the second equality is obtained by computing the gradient xlogq(x). Substituting q(x)=q(x)q(x|x)dx in , we have

sθ(x)Txq(x)dx=sθ(x)Tx(q(x)q(x|x)dx)dxMoving the gradient x inside the integral=sθ(x)T(q(x)xq(x|x)dx)dxNow, multiplying and dividing by q(x|x)), we get=sθ(x)T(q(x)xq(x|x)×q(x|x)q(x|x)dx)dxRearranging the terms, we get=sθ(x)Tq(x)(xq(x|x)q(x|x))q(x|x)dxdxNow, identifying xq(x|x)q(x|x)  as xlogq(x|x), we have=sθ(x)T(q(x)(xlogq(x|x))q(x|x)dx)dxMoving inner integral outside, we get=q(x|x)q(x)(sθ(x)Txlogq(x|x))dxdxIdentifying q(x|x)q(x)=q(x,x), we get=𝔼q(x,x)[sθ(x)Txlogq(x|x)].

Sliced Score Matching

Implicit score matching (Theorem 14) removes the dependence on the unknown data score, but it requires computing the trace of the Hessian trace(𝒙2logpθ(𝒙)), which costs D backpropagation passes for data of dimension D. This is prohibitively expensive for high-dimensional data such as images.

The idea behind sliced score matching [6] draws inspiration from sliced Wasserstein distances: instead of comparing full D-dimensional scores, project them onto random directions and compare the resulting one-dimensional quantities.

Definition 9 (Sliced Wasserstein Distance).

The sliced Wasserstein distance W~ is a cheaper approximation of the true Wasserstein distance, computed by averaging the 1D Wasserstein distance of projected point clouds: (Sliced Wasserstein)W~(x,y)2=θΩW(xθ,yθ)2 dθ where xθ={xi,θ}iI, where Ω={θd\θ=1} is the unit sphere, and I contains the indices of all point cloud data. Furthermore, can be re-written as a 1D assignment problem, seeking an optimal assignment of xi to yσθ(i): (Sliced Wasserstein2)W~(x,y)2=θΩminσθΣNiI|xiyσθ(i),θ|2 dθ.

The 1D assignment problem in can be solved in closed form. Sliced score matching proposes to compute sliced scores analogous to sliced Wasserstein distances.

Definition 10 (Sliced Score Matching).

L(θ;pv)12𝔼pv𝔼pd[(vxlogpθ(x)vxlogp(x))2], where vpv is a random projection vector and xpd is a sample from the data distribution.

To eliminate the need for xlogp(x), analogous to Theorem 14, the authors propose approximate sliced score matching [6].

Definition 11 (Approximate Sliced Score Matching).

(Approximate Unbiased Estimator)L(θ;pv)𝔼pv𝔼pd[vx(xlogpθ(x))v+12(vxlogpθ(x))2]. Analogous to Theorem 14, there is no dependence on the actual data distribution p(x). Moreover it leads to a matrix-vector computation rather than a full trace.

To prepare for the proof of equivalence, we list the assumptions from the original paper [6].

Assumption 1 (Regularity of score functions).

The model score function xlogpθ(x) and data score function xlogp(x) are both differentiable. They additionally satisfy 𝔼pd[xlogpθ(x)22]< and 𝔼pd[xlogp(x)22]<.

Assumption 2 (Regularity of projection vectors).

The projection vectors satisfy 𝔼pv[v22]<, and 𝔼pv[vv]0.

Assumption 3 (Boundary conditions).

We have the following boundary condition: θΘ,limxxpθ(x)pd(x)=0.

Assumption 4 (Identifiability).

The model family {pm(x;θ)|θΘ} is well-specified, i.e., p(x)=pθ(x). Furthermore, pθ(x)pθ(x) whenever θθ.

Assumption 5 (Positiveness).

We have the following positiveness condition: pθ(x)>0,θΘ and x.

We will also need the following lemma for integration by parts.

Lemma 1 (Multivariate Integration by Parts).

lima,bf(a,ξ2,,ξn)g(a,ξ2,,ξn)f(b,ξ2,,ξn)g(b,ξ2,,ξn)=f(ξ)g(ξ)ξ1dξ1+g(ξ)f(ξ)ξ1dξ1, assuming that f and g are differentiable. The same applies for all indices of ξi, but for notational simplicity we only write the case i=1 here.

Proof.

f(ξ)g(ξ)ξ1=f(ξ)g(ξ)ξ1+g(ξ)f(ξ)ξ1.

We can now consider this as a function of ξ1 alone, all other variables being fixed. Then, integrating over ξ1, we have proven the lemma.

Theorem 16 (Sliced Score Matching Equivalence).

Assume xlogpθ(x),xlogp(x) and pv satisfy some regularity conditions (def:diff:sm-assumption1,def:diff:sm-assumption2). Under proper boundary conditions (Assumption 3), we have 12𝔼pv𝔼pd[(vxlogpθ(x)vxlogp(x))2]=𝔼pv𝔼pd[vxxlogpθ(x)v+12(vxlogpθ(x))2]+C, where C is a constant w.r.t. θ.

Proof.

The basic idea of this proof is similar to that of Theorem 14 [6]. First, note that L(θ,pv) can be expanded to L(θ,pv)=12𝔼pv𝔼pd[(vxlogpθ(x)vxlogp(x))2]=Expanding the square and using boundedness of expectation, we have=(i)12𝔼pv𝔼pd[(vxlogpθ(x))2+(vxlogp(x))22(vxlogpθ(x))(vxlogp(x))]=(vxlogp(x))2 is a constant. Put it in C we have.=𝔼pv𝔼pd[(vxlogpθ(x))(vxlogp(x))+12(vxlogpθ(x))2]+C Now what we need to prove is 𝔼pv𝔼pd[(vxlogpθ(x))(vxlogp(x))]=𝔼pv𝔼pd[vxxlogpθ(x)v].

This can be shown by first observing that

(Sliced LAST)𝔼pv𝔼p[(vxlogpθ(x))(vxlogp(x))]=Writing expectation w.r.t. p as integral, we have=𝔼pvp(x)(vxlogpθ(x))(vxlogp(x))dx=Using the fact that p(x)xlogp(x)=xp(x), we have=𝔼pv(vxlogpθ(x))(vxp(x))dx=Writing last term vxp(x)=i=1Dvip(x)xi, we have=𝔼pvi=1D(vxlogpθ(x))vip(x)xi dx,

where we assume xD. Then, we plan to apply multivariate integration by parts as proved in Lemma 1. We first identify f(x) and g(x) and integration by parts for each i as follows:

(Integration Parts I)=With f(x)=vip(x) and g(x)=vTxlogpθ(x), we have for any i=|(vxlogpθ(x))vip(x)xi dx+vip(x)vxlogpθ(x)xi dx|=|[limxi(vxlogpθ(x))vipd(x)limxi(vsm(x;θ))vipd(x)]|.

We now have using above, which is due to Lemma 1

|𝔼pvi=1D(vxlogpθ(x)))vip(x)xi dx+𝔼pvi=1Dvip(x)vxlogpθ(x)xi dx|=|𝔼pv[i=1Dlimxi(vxlogpθ(x))vip(x)i=1Dlimxi(vsm(x;θ))vip(x)]|=Using triangle inequality and using sm,j(x;θ) to denote the jth component of xlogpθ(x)i=1Dlimxij=1D𝔼pv|vivj||sm,j(x;θ)p(x)|+i=1Dlimxij=1D𝔼pv|vivj||xlogpθ(x)p(x)|=Using Cauchy Schwarz inequality: |u,v|uv, we have(i)i=1Dlimxij=1D𝔼pvvi2𝔼pvvj2|sm,j(x;θ)p(x)|+i=1Dlimxij=1D𝔼pvvi2𝔼pvvj2|sm,j(x;θ)p(x)|=From the assumption that 𝔼pv[v2]< and s(x;θ)p(x) vanishes at infinity, we have=(ii)0,

Continuing from , we have

𝔼pvi=1D(vxlogpθ(x))vip(x)xi dx=𝔼pvi=1Dvip(x)vxlogpθ(x)xi dx=𝔼pvp(x)vxxlogpθ(x)vdx.

Research 1.

Other Score Matching Methods We have seen explicit score matching, implicit score matching, denoising score matching, and finally sliced score matching. What are other ways you may think of for score matching? For example, when there is some structure expected in the distribution, can it be made more specific to such distributions?

Practical Implementation

In the following, let sm(xi;θ)=xlogpθ(x). For practical implementation [6], consider a dataset x1n and draw m projection vectors independently for each point xi from pv. We denote all such vectors {vij}1in,1jm by v11nm. For L(θ;pv) defined in , an unbiased estimator is (Sliced Practical1)L^(θ;x1n,v11nm)1n1mi=1nj=1mvijxsm(xi;θ)vij+12(vijsm(xi;θ))2.

If pv is known to be a multivariate standard normal or multivariate Rademacher distribution, we may use existing results to conclude 𝔼pv[(vsm(x;θ))2]=sm(x;θ)22.

Hence, reduces to the following variance-reduced estimator (SSM-VR): L^vr(θ;x1n,v11nm)1n1mi=1nj=1mvijxsm(xi;θ)vij+12sm(xi;θ)22, where the last term can be integrated analytically. It was found empirically that this variance-reduced version had better performance [6].

Definition 12 (Hutchinson's Trace Estimator).

Given a matrix An×n, Hutchinson's trace estimator approximates the trace of A as 𝔼[Hm(A)]=trace(A),where Hm(A)=1mi=1mziTAzi, where zin are I.I.D. random vectors. Typically, zi are chosen to be I.I.D. Gaussians or Rademacher vectors with components randomly chosen as ±1. Notably, these random vectors have mean 0 and variance 1.

The sliced score matching technique is closely related to randomised trace estimation. In implicit score matching (Theorem 14), one must compute trace(x2logpθ(x)), which requires D additional backpropagation passes after computing xlogpθ(x). Sliced score matching replaces this with a single matrix-vector product vx2logpθ(x)v, which needs only one additional backpropagation pass regardless of dimension.

A vector-valued deep neural network hθ(x):DD can be trained as a score model. Substituting h into L^(θ;x1n,v11nm) in for sm, we get (SSM SUM)1n1mi=1nj=1mvijxhθ(xi)vij+12(vijhθ(xi))2. Minimising the above loss w.r.t. θ gives optimal θ^, and hθ^(xi) can be treated as an approximation to the reference score function xlogq(x). This is justified with the following identity, obtained by integration by parts as before: 𝔼pv𝔼p[vxhθ(x)v+12(vhθ(x))2]=12𝔼pv𝔼p[(vhθ(x)vxlogp(x))2]+C. From the last term, we observe that if a good collection of random projection vectors v are chosen, then hθ(x)xlogp(x).

Historical Note.

From Optimal Transport to Score Matching The connection between sliced Wasserstein distances and sliced score matching reveals a deeper link between optimal transport and score-based generative modelling. Both frameworks deal with comparing and transporting distributions, but while optimal transport moves mass between distributions, score matching aligns the gradients of their log-densities. The “slicing” trick, projecting high-dimensional problems onto random one-dimensional subspaces, makes both computationally feasible and bridges these two foundational perspectives.

Exercises

Exercise 1 (Forward Marginal Distribution).

In a diffusion model, the forward process is defined as q(xt|xt1)=𝒩(xt;αtxt1,(1αt)I). Using the Markov property, derive the marginal distribution q(xt|x0) in closed form. Specifically, show that q(xt|x0)=𝒩(xt;αtx0,(1αt)I) where αt=s=1tαs. Hint: Use the reparameterisation trick iteratively and combine the noise terms.

Exercise 2 (Noise Schedule Design).

Explain why the noise schedule {αt}t=1T must be chosen so that αT0. What happens to the distribution q(xT|x0) in this limit? Discuss the trade-off between using a small number of timesteps T (faster sampling) and a large number of timesteps (better approximation of the reverse process). How do methods like DDIM address this trade-off?

Exercise 3 (ELBO Decomposition).

The ELBO for the variational diffusion model decomposes into three types of terms: a reconstruction term, a prior matching term, and denoising matching terms. Write down each term explicitly and explain its role. Why does the prior matching term have no trainable parameters? Under what conditions on T and {αt} does it vanish?

Exercise 4 (Score of a Gaussian Mixture).

The score function is defined as s(x)=xlogp(x). Consider a mixture of two Gaussians in one dimension: p(x)=12𝒩(x;μ1,σ2)+12𝒩(x;μ2,σ2).

  1. Compute the score function s(x) explicitly.

  2. Show that at the midpoint x=(μ1+μ2)/2, the score is zero.

  3. Describe the behaviour of the score function in the regions near each mode.

  4. (Optional) Consider a two-component mixture in 2 with means μ1=[2,2], μ2=[2,2], covariances Σ1=[20.30.32], Σ2=[20.30.32], and mixing weights π1=2/3, π2=1/3. Derive and plot the score (gradient field) in Python.

Exercise 5 (Langevin Dynamics Convergence).

Langevin dynamics generates samples from a distribution p(x) via the iteration xk+1=xk+ϵ2xlogp(xk)+ϵzk,zk𝒩(0,I). Show that as ϵ0 and K (with Kϵ), the distribution of xK converges to p(x). Why is an accurate estimate of the score function xlogp(x) crucial for the quality of the generated samples?

Exercise 6 (Denoising Score Matching).

In denoising score matching, instead of computing the intractable xlogp(x) directly, we train a neural network sθ(x) by minimising 𝔼pdata(x0)𝔼q(xt|x0)[sθ(xt,t)xtlogq(xt|x0)2]. For the Gaussian forward process q(xt|x0)=𝒩(xt;αtx0,(1αt)I), compute xtlogq(xt|x0) in closed form and show that it is proportional to the noise ϵ that was added during the forward process. Explain how this connects to the ϵ-prediction formulation used in DDPM.

Exercise 7 (Computational Cost of Score Matching).

Compare the computational costs of explicit score matching, implicit score matching, and sliced score matching. Explicit score matching requires knowledge of xlogp(x), implicit score matching requires computing tr(xsθ(x)) (a Hessian trace), and sliced score matching approximates this trace using random projections. For a data dimension D and m random projection vectors, express the computational cost of each method in terms of the number of backpropagation passes required and explain when sliced score matching offers a significant advantage.

References

  1. Deep Unsupervised Learning using Nonequilibrium Thermodynamics

    Jascha Sohl-Dickstein, Eric Weiss, Niru Maheswaranathan, Surya Ganguli

    International Conference on Machine Learning, pp. 2256-2264 · 2015

    BibTeX
    @inproceedings{sohl2015deep,
      title={Deep Unsupervised Learning using Nonequilibrium Thermodynamics},
      author={Sohl-Dickstein, Jascha and Weiss, Eric and Maheswaranathan, Niru and Ganguli, Surya},
      booktitle={International Conference on Machine Learning},
      pages={2256--2264},
      year={2015},
      organization={PMLR}
    }

    Conference paper

  2. Denoising Diffusion Probabilistic Models

    Jonathan Ho, Ajay Jain, Pieter Abbeel

    Advances in Neural Information Processing Systems, vol. 33, pp. 6840-6851 · 2020

    BibTeX
    @inproceedings{ho2020denoising,
      title={Denoising Diffusion Probabilistic Models},
      author={Ho, Jonathan and Jain, Ajay and Abbeel, Pieter},
      booktitle={Advances in Neural Information Processing Systems},
      volume={33},
      pages={6840--6851},
      year={2020}
    }

    Conference paper

  3. Score-Based Generative Modeling through Stochastic Differential Equations

    Yang Song, Jascha Sohl-Dickstein, Diederik P. Kingma, Abhishek Kumar, Stefano Ermon, Ben Poole

    International Conference on Learning Representations · 2021

    BibTeX
    @inproceedings{song2020score,
      title={Score-Based Generative Modeling through Stochastic Differential Equations},
      author={Song, Yang and Sohl-Dickstein, Jascha and Kingma, Diederik P. and Kumar, Abhishek and Ermon, Stefano and Poole, Ben},
      booktitle={International Conference on Learning Representations},
      year={2021}
    }

    Conference paper

  4. Variational Diffusion Models

    Diederik Kingma, Tim Salimans, Ben Poole, Jonathan Ho

    Advances in Neural Information Processing Systems, vol. 34, pp. 21696-21707 · 2021

    BibTeX
    @inproceedings{kingma2021variational,
      title={Variational Diffusion Models},
      author={Kingma, Diederik and Salimans, Tim and Poole, Ben and Ho, Jonathan},
      booktitle={Advances in Neural Information Processing Systems},
      volume={34},
      pages={21696--21707},
      year={2021}
    }

    Conference paper

  5. Understanding Diffusion Models: A Unified Perspective

    Calvin Luo

    arXiv:2208.11970 · 2022

    BibTeX
    @misc{luo2022understanding,
          title={Understanding Diffusion Models: A Unified Perspective}, 
          author={Calvin Luo},
          year={2022},
          eprint={2208.11970},
          archivePrefix={arXiv},
          primaryClass={cs.LG}
    }

    Reference

  6. Sliced Score Matching: A Scalable Approach to Density and Score Estimation

    Yang Song, Sahaj Garg, Jiaxin Shi, Stefano Ermon

    Uncertainty in Artificial Intelligence, pp. 574-584 · 2020

    BibTeX
    @inproceedings{song2019sliced,
      title={Sliced Score Matching: A Scalable Approach to Density and Score Estimation},
      author={Song, Yang and Garg, Sahaj and Shi, Jiaxin and Ermon, Stefano},
      booktitle={Uncertainty in Artificial Intelligence},
      pages={574--584},
      year={2020},
      organization={PMLR}
    }

    Conference paper