Course contents 27 / 34

27 / 34 · Diffusion Models

DDPM: reverse process and training objective

How DDPM learns to run the noising chain backwards: a likelihood bound split into one KL term per step, a Gaussian posterior known in closed form, and the noise-prediction loss with its training and sampling algorithms.

On this page 14

The basic DDPM training objective fits in one line: take an image, add Gaussian noise of a random strength, and ask the network to say what noise was added. The loss is the squared error of that guess. Nothing in it mentions likelihoods, chains or posteriors, yet the network it trains generates images by running the forward process backwards. Why is this the right regression, and what does it give up?

The answer comes from treating the noisy images x1,…,xTx_1, \dots, x_T as latent variables, as in the VAE. The encoder is the fixed forward chain, the decoder is the learned reverse chain, and the loss starts as the VAE’s bound on the likelihood. Every simplification from that bound to the one-line loss can be derived, and one of them is a deliberate change of objective.

We keep the notation of the forward process: βt∈(0,1)\beta_t \in (0, 1) is the noise variance of step tt, and

αt=1−βt,αˉt=∏s=1tαs,αˉ0=1,q(xt∣xt−1)=N(αt xt−1,  βtI),q(xt∣x0)=N(αˉt x0,  (1−αˉt)I).\begin{aligned} \alpha_t &= 1 - \beta_t , \\ \bar\alpha_t &= \textstyle\prod_{s=1}^{t} \alpha_s , \\ \bar\alpha_0 &= 1 , \\ q(x_t \mid x_{t-1}) &= \mathcal N\big(\sqrt{\alpha_t}\, x_{t-1},\; \beta_t I\big) , \\ q(x_t \mid x_0) &= \mathcal N\big(\sqrt{\bar\alpha_t}\, x_0,\; (1 - \bar\alpha_t) I\big) . \end{aligned}

The second argument of N\mathcal N is the variance, and xs:tx_{s:t} is short for (xs,…,xt)(x_s, \dots, x_t).

The reverse process#

Generation starts from p(xT)=N(0,I)p(x_T) = \mathcal N(0, I), a fixed distribution with no parameters, and applies a learned step pθ(xt−1∣xt)p_\theta(x_{t-1} \mid x_t) for t=T,…,1t = T, \dots, 1. The complete forward and reverse processes are

q(x1:T∣x0)=∏t=1Tq(xt∣xt−1),pθ(x0:T)=p(xT)∏t=1Tpθ(xt−1∣xt).\begin{aligned} q(x_{1:T} \mid x_0) &= \prod_{t=1}^{T} q(x_t \mid x_{t-1}) , \\ p_\theta(x_{0:T}) &= p(x_T) \prod_{t=1}^{T} p_\theta(x_{t-1} \mid x_t) . \end{aligned}
Figure

The forward step is known; the backward step must be learned

Forward and reverse steps of the diffusion chainA row of four pixel tiles: x0, the image; x t minus 1; x t; and x T, pure noise, with ellipses between x0 and x t minus 1 and between x t and x T. A curved arrow above goes from x t minus 1 to x t, labelled q of x t given x t minus 1, fixed forward step. A curved blue arrow below goes back from x t to x t minus 1, labelled p theta of x t minus 1 given x t, the probability density to invert, learned with network parameters theta.x0imagext−1xtxTnoise⋯⋯q(xₜ | xₜ₋₁)fixed: adds noisepθ(xₜ₋₁ | xₜ)pdf to invert:learned; θ: NN parametersForward and reverse steps of the diffusion chainA column of four pixel tiles from top to bottom: x0, the image; x t minus 1; x t; and x T, pure noise, with vertical ellipses in between. On the right a curved arrow goes down from x t minus 1 to x t, labelled q of x t given x t minus 1, fixed. On the left a curved blue arrow goes up from x t to x t minus 1, labelled p theta of x t minus 1 given x t, the learned probability density to invert.x0imagext−1xtxTnoise⋮⋮q(xₜ | xₜ₋₁)fixedpθ(xₜ₋₁ | xₜ)pdf to invert(learned)
Tiles show marginal noising of a local pattern at ᾱ = 1, 0.6, 0.42 and 0. Shared noise aids comparison; this is not a Markov trajectory. The last tile is the pure-noise limit. Arrows indicate the directions of fixed q and learned pθ.

What family should pθ(xt−1∣xt)p_\theta(x_{t-1} \mid x_t) belong to? The step it imitates is the true reverse step, by Bayes’ theorem

q(xt−1∣xt)∝q(xt∣xt−1) q(xt−1),q(x_{t-1} \mid x_t) \propto q(x_t \mid x_{t-1})\, q(x_{t-1}) ,

which involves the unknown marginal q(xt−1)q(x_{t-1}). For a smooth positive marginal and a sufficiently small step, a local Gaussian approximation is useful. In one coordinate it is

q(xt−1∣xt)≈N(y+v ∇log⁡q(y),  v),y=xt/αt,v=βt/αt,\begin{aligned} q(x_{t-1} \mid x_t) &\approx \mathcal N\big(y + v\, \nabla \log q(y),\; v\big) , \\ y &= x_t / \sqrt{\alpha_t} , \\ v &= \beta_t / \alpha_t , \end{aligned}

where qq in the score means the marginal of xt−1x_{t-1}. The Taylor argument below controls the log-density locally when its curvature is bounded; turning that into an approximation of the whole conditional distribution also needs its mass to concentrate in that neighbourhood. It does not cover an atomic data distribution at the final step. Sohl-Dickstein et al. (2015), §2.2, motivate small-step reverse diffusions using Feller’s result; Isachenko (2025), lecture 7, gives the same motivation.

Why a small reverse step is nearly Gaussian

As a function of xt−1x_{t-1}, the first factor is a Gaussian centred at yy with the tiny variance vv; its exponent is −αt(xt−1−y)2/(2βt)-\alpha_t (x_{t-1} - y)^2 / (2\beta_t). Over a width of order v\sqrt v around yy, the smooth function log⁡q(xt−1)\log q(x_{t-1}) is close to its first-order Taylor expansion, log⁡q(y)+(xt−1−y) ∇log⁡q(y)\log q(y) + (x_{t-1} - y)\, \nabla \log q(y). A Gaussian multiplied by the exponential of a linear function is a Gaussian with the same variance and its mean moved by the variance times the slope (complete the square). That gives the display above.

The error comes from the neglected quadratic term of the Taylor expansion. If the second derivative of log⁡q\log q stays within ±K\pm K near yy, that term is at most 12K(xt−1−y)2\tfrac12 K (x_{t-1} - y)^2, which is of order KvK v over the width v\sqrt v and goes to 00 with vv.

This motivates many small steps and the Gaussian family used for the intermediate reverse steps, t≥2t \ge 2:

pθ(xt−1∣xt)=N(μθ(xt,t),  σt2I),p_\theta(x_{t-1} \mid x_t) = \mathcal N\big(\mu_\theta(x_t, t),\; \sigma_t^2 I\big) ,

a Gaussian whose mean a network computes from xtx_t and tt, and whose variance σt2\sigma_t^2 is a fixed number for each step. The gradient ∇log⁡q\nabla \log q in the mean is the score of the noised data; the score matching note returns to it.

An upper bound via Jensen’s inequality#

We want real images to be likely under the model: minimize Eq(x0)[−log⁡pθ(x0)]\mathbb E_{q(x_0)}[-\log p_\theta(x_0)]. The likelihood of one image integrates the reverse process over all intermediate states, every path from noise to the image:

pθ(x0)=∫pθ(x0:T) dx1:T.p_\theta(x_0) = \int p_\theta(x_{0:T})\, dx_{1:T} .

For a general nonlinear network the composed reverse steps need not be Gaussian, and this integral has no tractable closed form. Numerically it runs over TT times the number of pixels dimensions.

Figure

The likelihood integral sums over every path from noise to the image

Many paths from noise to an imageTwo ovals: images on the left containing the point x0, noise on the right containing the point x T and other noise samples. Many dashed wiggly paths run from points in the noise oval to x0. One path is highlighted in blue with three intermediate dots labelled one path equals one value of x 1 to T. Above, the integral of p theta of x 0 to T over x 1 to T. Below, a red note: we can't really compute all of these paths.−log ∫ pθ(x0:T) dx1:TimagesnoisexTx0one path = one value of x1:Tcan't really compute all of these pathsMany paths from noise to an imageTwo ovals: images at the top containing x0, noise at the bottom containing x T and other noise samples. Many dashed wiggly paths run upward from the noise oval to x0; one is highlighted in blue with three dots, one path equals one value of x 1 to T. Below, a red note: we can't really compute all of these paths.−log ∫ pθ(x0:T) dx1:TimagesnoisexTx0one path =one x1:Tcan't really computeall of these paths
The exact likelihood integrates the joint reverse density over all states x₁, …, x_T. Each value of these states is one path from a noise sample to the image x₀; paths start anywhere in the noise. The drawn curves are schematic random bridges with a fixed seed, not trajectories of a trained model.

The way out is the one of the VAE. Multiply and divide by the forward process, which is positive everywhere and easy to sample, then apply Jensen’s inequality to the concave logarithm, log⁡E[Y]≥E[log⁡Y]\log \mathbb E[Y] \ge \mathbb E[\log Y]:

−log⁡pθ(x0)=−log⁡Eq(x1:T∣x0)[pθ(x0:T)q(x1:T∣x0)]≤Eq(x1:T∣x0)[−log⁡pθ(x0:T)q(x1:T∣x0)]=:L(x0).\begin{aligned} -\log p_\theta(x_0) &= -\log \mathbb E_{q(x_{1:T} \mid x_0)}\Big[\frac{p_\theta(x_{0:T})}{q(x_{1:T} \mid x_0)}\Big] \\ &\le \mathbb E_{q(x_{1:T} \mid x_0)}\Big[-\log \frac{p_\theta(x_{0:T})}{q(x_{1:T} \mid x_0)}\Big] \\ &=: L(x_0) . \end{aligned}

LL is the negative ELBO, an upper bound on the negative log-likelihood. The gap is DKL(q(x1:T∣x0) ∥ pθ(x1:T∣x0))D_{\mathrm{KL}}\big(q(x_{1:T} \mid x_0) \,\|\, p_\theta(x_{1:T} \mid x_0)\big), by the same decomposition as in variational inference. Training minimizes Eq(x0)[L(x0)]\mathbb E_{q(x_0)}[L(x_0)]. As written, LL is still an expectation over all TT noisy states at once. A one-sample estimate draws a whole path and adds TT random log-ratios, so the randomness of every step enters it. The next two sections rewrite LL as a sum of terms that each involve one step, and replace the average over each step by its closed form; Ho, Jain and Abbeel (2020, §2) introduce this rewriting as variance reduction.

The lemma: the reversed chain is Markov given the image#

The bound pairs the reverse steps pθ(xt−1∣xt)p_\theta(x_{t-1} \mid x_t) with forward steps q(xt∣xt−1)q(x_t \mid x_{t-1}), which are densities of different variables. To compare like with like we need the forward process written backwards. The tool is a lemma. The proof, bound decomposition and image-to-noise route below adapt BMML lecture 5, §5.4 and Lemma 1, under the MIT license and copyright notice; notation and explanation are expanded here. The underlying DDPM equations are credited to Ho, Jain and Abbeel.

Lemma. If q(x1:T∣x0)=∏t=1Tq(xt∣xt−1)q(x_{1:T} \mid x_0) = \prod_{t=1}^T q(x_t \mid x_{t-1}), then for every t<Tt < T

q(xt∣xt+1,…,xT,x0)=q(xt∣xt+1,x0).q(x_t \mid x_{t+1}, \dots, x_T, x_0) = q(x_t \mid x_{t+1}, x_0) .

Reversed in time and with x0x_0 held fixed, the chain is still Markov: once xt+1x_{t+1} and the image are known, the later, noisier states say nothing more about xtx_t.

Proof of the lemma

Fix tt. Integrating x1,…,xt−1x_1, \dots, x_{t-1} out of the forward product turns its first tt factors into the marginal q(xt∣x0)q(x_t \mid x_0):

q(xt,…,xT∣x0)=q(xt∣x0)∏s=t+1Tq(xs∣xs−1).(1)q(x_t, \dots, x_T \mid x_0) = q(x_t \mid x_0) \prod_{s=t+1}^{T} q(x_s \mid x_{s-1}) . \tag{1}

By the definition of a conditional density, the same joint is

q(xt,…,xT∣x0)=q(xt∣xt+1:T,x0)  q(xt+1:T∣x0).(2)q(x_t, \dots, x_T \mid x_0) = q(x_t \mid x_{t+1:T}, x_0)\; q(x_{t+1:T} \mid x_0) . \tag{2}

Now rewrite the first two factors of (1). The chain is Markov, so q(xt+1∣xt)=q(xt+1∣xt,x0)q(x_{t+1} \mid x_t) = q(x_{t+1} \mid x_t, x_0), and Bayes’ theorem for the pair (xt,xt+1)(x_t, x_{t+1}) given x0x_0 gives

q(xt∣x0) q(xt+1∣xt)=q(xt∣x0) q(xt+1∣xt,x0)=q(xt∣xt+1,x0) q(xt+1∣x0).\begin{aligned} q(x_t \mid x_0)\, q(x_{t+1} \mid x_t) &= q(x_t \mid x_0)\, q(x_{t+1} \mid x_t, x_0) \\ &= q(x_t \mid x_{t+1}, x_0)\, q(x_{t+1} \mid x_0) . \end{aligned}

Substituted into (1), the factors left after q(xt∣xt+1,x0)q(x_t \mid x_{t+1}, x_0) are q(xt+1∣x0)∏s=t+2Tq(xs∣xs−1)q(x_{t+1} \mid x_0) \prod_{s=t+2}^{T} q(x_s \mid x_{s-1}), which is (1) written for t+1t + 1, that is q(xt+1:T∣x0)q(x_{t+1:T} \mid x_0):

q(xt,…,xT∣x0)=q(xt∣xt+1,x0)  q(xt+1:T∣x0).q(x_t, \dots, x_T \mid x_0) = q(x_t \mid x_{t+1}, x_0)\; q(x_{t+1:T} \mid x_0) .

Comparing with (2) and dividing by q(xt+1:T∣x0)>0q(x_{t+1:T} \mid x_0) > 0 proves the lemma.

The chain rule of probability, applied from the last state backwards, and then the lemma, give the factorization the bound needs:

q(x1:T∣x0)=q(xT∣x0)∏t=1T−1q(xt∣xt+1:T,x0)=q(xT∣x0)∏t=1T−1q(xt∣xt+1,x0).\begin{aligned} q(x_{1:T} \mid x_0) &= q(x_T \mid x_0) \prod_{t=1}^{T-1} q(x_t \mid x_{t+1:T}, x_0) \\ &= q(x_T \mid x_0) \prod_{t=1}^{T-1} q(x_t \mid x_{t+1}, x_0) . \end{aligned}

Splitting the bound into KL terms#

Put the two factorizations into the ratio inside LL. In the numerator, peel off the last reverse step pθ(x0∣x1)p_\theta(x_0 \mid x_1); in the denominator, shift the index of the product by one, so that both products run over t=2,…,Tt = 2, \dots, T and pair a reverse step with a backward forward step of the same variables:

log⁡pθ(x0:T)q(x1:T∣x0)=log⁡p(xT)q(xT∣x0)+∑t=2Tlog⁡pθ(xt−1∣xt)q(xt−1∣xt,x0)+log⁡pθ(x0∣x1).\begin{aligned} \log \frac{p_\theta(x_{0:T})}{q(x_{1:T} \mid x_0)} &= \log \frac{p(x_T)}{q(x_T \mid x_0)} \\ &\quad + \sum_{t=2}^{T} \log \frac{p_\theta(x_{t-1} \mid x_t)}{q(x_{t-1} \mid x_t, x_0)} \\ &\quad + \log p_\theta(x_0 \mid x_1) . \end{aligned}

Each term involves at most two states, so its expectation over all of x1:Tx_{1:T} reduces to those two. For the term with (xt−1,xt)(x_{t-1}, x_t), apply the definition of a conditional density twice:

q(x1:T∣x0)=q(xt∣x0)  q(xt−1∣xt,x0)⋅q(other states∣xt−1,xt,x0).\begin{aligned} q(x_{1:T} \mid x_0) &= q(x_t \mid x_0)\; q(x_{t-1} \mid x_t, x_0) \\ &\quad \cdot q(\text{other states} \mid x_{t-1}, x_t, x_0) . \end{aligned}

The last factor integrates to 11 over the other states. What remains is an average over xt∼q(xt∣x0)x_t \sim q(x_t \mid x_0) of an inner average over xt−1∼q(xt−1∣xt,x0)x_{t-1} \sim q(x_{t-1} \mid x_t, x_0), and the inner average of log⁡(q/pθ)\log(q / p_\theta) is by definition a KL divergence (lecture notes, lecture 5). Changing the sign, the bound splits into

L(x0)=LT+∑t=2TLt−1+L0,L(x_0) = L_T + \sum_{t=2}^{T} L_{t-1} + L_0 , LT=DKL(q(xT∣x0) ∥ p(xT)),Lt−1=Eq(xt∣x0) DKL(qt−1 ∥ pθ,t−1),qt−1=q(xt−1∣xt,x0),pθ,t−1=pθ(xt−1∣xt),L0=−Eq(x1∣x0)log⁡pθ(x0∣x1),\begin{aligned} L_T &= D_{\mathrm{KL}}\big(q(x_T \mid x_0) \,\|\, p(x_T)\big) , \\ L_{t-1} &= \mathbb E_{q(x_t \mid x_0)}\, D_{\mathrm{KL}}\big(q_{t-1} \,\|\, p_{\theta, t-1}\big) , \\ q_{t-1} &= q(x_{t-1} \mid x_t, x_0) , \\ p_{\theta, t-1} &= p_\theta(x_{t-1} \mid x_t) , \\ L_0 &= -\mathbb E_{q(x_1 \mid x_0)} \log p_\theta(x_0 \mid x_1) , \end{aligned}

the form of Ho, Jain and Abbeel (2020, Eq. 5). The three kinds of terms behave differently.

  • LTL_T compares the end of the forward chain with the fixed prior. Neither contains a trainable parameter, so LTL_T is a constant during training; for DDPM’s schedule it is small for order-one pixels, with its exact value depending on x0x_0 (forward process).
  • L0L_0 is the decoder’s reconstruction term. It is not a constant: even with the family of pθ(x0∣x1)p_\theta(x_0 \mid x_1) fixed, its mean μθ(x1,1)\mu_\theta(x_1, 1) is the network’s output, so L0L_0 changes with θ\theta.
  • Lt−1L_{t-1} for t=2,…,Tt = 2, \dots, T does the main work. Each compares the model’s step with q(xt−1∣xt,x0)q(x_{t-1} \mid x_t, x_0), the forward step reversed with the image known. Both are Gaussians, as the next section shows, so every KL has a closed form instead of a noisy Monte Carlo estimate.
The decoder at t = 1 and bits per dimension

Pixels are integers 0,…,2550, \dots, 255 scaled linearly to [−1,1][-1, 1], so neighbouring values are 2/2552/255 apart. DDPM’s last step gives each value the probability mass of its bin under the Gaussian of the reverse step (Ho, Jain and Abbeel, 2020, Eq. 13), one factor per pixel value i=1,…,Di = 1, \dots, D:

pθ(x0∣x1)=∏i=1D∫δ−(x0i)δ+(x0i)N(x; μθi(x1,1), σ12) dx,δ±(x)=x±1255,\begin{aligned} p_\theta(x_0 \mid x_1) &= \prod_{i=1}^{D} \int_{\delta_-(x_0^i)}^{\delta_+(x_0^i)} \mathcal N\big(x;\ \mu_\theta^i(x_1, 1),\ \sigma_1^2\big)\, dx , \\ \delta_\pm(x) &= x \pm \tfrac{1}{255} , \end{aligned}

with the outer edges δ+(1)=+∞\delta_+(1) = +\infty and δ−(−1)=−∞\delta_-(-1) = -\infty, so that the 256 probabilities sum to 11. Each factor is a difference of two values of the standard normal CDF Φ\Phi. For an interior bin, with σ1=β1=0.01\sigma_1 = \sqrt{\beta_1} = 0.01 and the mean exactly on its pixel value, the bin ±1/255\pm 1/255 spans ±0.392\pm 0.392 standard deviations, so its probability is 2Φ(0.392)−1=0.3052\Phi(0.392) - 1 = 0.305, or −log⁡20.305=1.71-\log_2 0.305 = 1.71 bits.

Likelihoods are reported in bits per dimension: the bound in nats divided by Dln⁡2D \ln 2 (definition). For CIFAR-10, D=32⋅32⋅3=3072D = 32 \cdot 32 \cdot 3 = 3072, so 3.753.75 bits per dimension mean 11,52011{,}520 bits per image.

The true posterior#

Figure

Forward step, model step and the true posterior

Graph of x t minus 1, x t and x 0 with three conditional distributionsThree circled nodes: x t minus 1 on the left, tagged noisy; x t on the right, tagged more noisy; and x 0 below x t, tagged original image. A solid arrow arcs over the top from x t minus 1 to x t, labelled q of x t given x t minus 1. A dashed blue arrow returns from x t to x t minus 1, labelled p theta of x t minus 1 given x t, the reverse diffusion step. A dotted line links x t and x 0, grouping the two conditions of the true posterior. A green arrow sweeps from x 0 along the bottom to x t minus 1, labelled q of x t minus 1 given x t and x 0, the true posterior.noisymore noisyoriginalimageq(xₜ | xₜ₋₁)pθ(xₜ₋₁ | xₜ)reverse diffusion stepq(xₜ₋₁ | xₜ, x0)“true posterior”: needs x₀xt−1xtx0Graph of x t minus 1, x t and x 0 with three conditional distributionsThree circled nodes: x t minus 1 on the left, noisy; x t on the right, more noisy; x 0 below x t, original image. A solid arrow arcs over the top from x t minus 1 to x t, labelled q of x t given x t minus 1. A dashed blue arrow returns from x t to x t minus 1, labelled p theta of x t minus 1 given x t. A dotted line links x t and x 0, grouping the two conditions of the true posterior. A green arrow sweeps from x 0 to x t minus 1, labelled q of x t minus 1 given x t and x 0, the true posterior.noisymore noisyoriginal imageq(xₜ | xₜ₋₁)pθ(xₜ₋₁ | xₜ)q(xₜ₋₁ | xₜ, x0)“true posterior”xt−1xtx0
The forward step q(xₜ | xₜ₋₁) adds noise. The model's step pθ(xₜ₋₁ | xₜ) sees only xₜ. The true posterior q(xₜ₋₁ | xₜ, x₀) also goes back to xₜ₋₁ but conditions on the image x₀ as well (dotted group); it has a closed form but needs an image.

The forward step reversed with the image known, q(xt−1∣xt,x0)q(x_{t-1} \mid x_t, x_0), is the “true posterior” that each Lt−1L_{t-1} compares with. Bayes’ theorem conditioned on x0x_0, followed by the Markov property q(xt∣xt−1,x0)=q(xt∣xt−1)q(x_t \mid x_{t-1}, x_0) = q(x_t \mid x_{t-1}), gives

q(xt−1∣xt,x0)=q(xt∣xt−1)  q(xt−1∣x0)q(xt∣x0).q(x_{t-1} \mid x_t, x_0) = \frac{q(x_t \mid x_{t-1})\; q(x_{t-1} \mid x_0)}{q(x_t \mid x_0)} .

Both factors of the numerator are Gaussian in xt−1x_{t-1}, and the denominator does not depend on xt−1x_{t-1}. Take logarithms, per coordinate, and collect the terms in xt−1x_{t-1}; every term without xt−1x_{t-1} goes into a constant cc:

log⁡q(xt−1∣xt,x0)=−(xt−αt xt−1)22βt−(xt−1−αˉt−1 x0)22(1−αˉt−1)+c=−12(A xt−12−2B xt−1)+c,\begin{aligned} \log q(x_{t-1} \mid x_t, x_0) &= -\frac{(x_t - \sqrt{\alpha_t}\, x_{t-1})^2}{2\beta_t} \\ &\quad - \frac{(x_{t-1} - \sqrt{\bar\alpha_{t-1}}\, x_0)^2}{2(1 - \bar\alpha_{t-1})} + c \\ &= -\tfrac12 \big(A\, x_{t-1}^2 - 2 B\, x_{t-1}\big) + c , \end{aligned}

where expanding the squares gives

A=αtβt+11−αˉt−1,B=αtβt xt+αˉt−11−αˉt−1 x0.\begin{aligned} A &= \frac{\alpha_t}{\beta_t} + \frac{1}{1 - \bar\alpha_{t-1}} , \\ B &= \frac{\sqrt{\alpha_t}}{\beta_t}\, x_t + \frac{\sqrt{\bar\alpha_{t-1}}}{1 - \bar\alpha_{t-1}}\, x_0 . \end{aligned}

Completing the square, Ax2−2Bx=A(x−B/A)2−B2/AA x^2 - 2 B x = A (x - B/A)^2 - B^2/A, so the posterior is Gaussian with variance 1/A1/A and mean B/AB/A. Over a common denominator, and with αtαˉt−1=αˉt\alpha_t \bar\alpha_{t-1} = \bar\alpha_t and αt+βt=1\alpha_t + \beta_t = 1,

A=αt(1−αˉt−1)+βtβt(1−αˉt−1)=αt−αˉt+βtβt(1−αˉt−1)=1−αˉtβt(1−αˉt−1).\begin{aligned} A &= \frac{\alpha_t (1 - \bar\alpha_{t-1}) + \beta_t}{\beta_t (1 - \bar\alpha_{t-1})} \\ &= \frac{\alpha_t - \bar\alpha_t + \beta_t}{\beta_t (1 - \bar\alpha_{t-1})} \\ &= \frac{1 - \bar\alpha_t}{\beta_t (1 - \bar\alpha_{t-1})} . \end{aligned}

Inverting AA gives the variance, and multiplying BB by 1/A1/A term by term gives the mean:

αtβt⋅βt(1−αˉt−1)1−αˉt=αt (1−αˉt−1)1−αˉt,αˉt−11−αˉt−1⋅βt(1−αˉt−1)1−αˉt=αˉt−1 βt1−αˉt.\begin{aligned} \frac{\sqrt{\alpha_t}}{\beta_t} \cdot \frac{\beta_t (1 - \bar\alpha_{t-1})}{1 - \bar\alpha_t} &= \frac{\sqrt{\alpha_t}\, (1 - \bar\alpha_{t-1})}{1 - \bar\alpha_t} , \\ \frac{\sqrt{\bar\alpha_{t-1}}}{1 - \bar\alpha_{t-1}} \cdot \frac{\beta_t (1 - \bar\alpha_{t-1})}{1 - \bar\alpha_t} &= \frac{\sqrt{\bar\alpha_{t-1}}\, \beta_t}{1 - \bar\alpha_t} . \end{aligned}

Together:

q(xt−1∣xt,x0)=N(μ~t(xt,x0),  β~tI),β~t=1−αˉt−11−αˉt βt,μ~t=αˉt−1 βt1−αˉt x0+αt (1−αˉt−1)1−αˉt xt.\boxed{ \begin{aligned} q(x_{t-1} \mid x_t, x_0) &= \mathcal N\big(\tilde\mu_t(x_t, x_0),\; \tilde\beta_t I\big) , \\ \tilde\beta_t &= \frac{1 - \bar\alpha_{t-1}}{1 - \bar\alpha_t}\, \beta_t , \\ \tilde\mu_t &= \frac{\sqrt{\bar\alpha_{t-1}}\, \beta_t}{1 - \bar\alpha_t}\, x_0 + \frac{\sqrt{\alpha_t}\, (1 - \bar\alpha_{t-1})}{1 - \bar\alpha_t}\, x_t . \end{aligned} }

This is DDPM’s Eq. 6–7, and the same computation as the normal–normal conjugate update: the precisions add, and the mean is a precision-weighted average. Toy example: β1=0.1\beta_1 = 0.1 and β2=0.2\beta_2 = 0.2, so αˉ1=0.9\bar\alpha_1 = 0.9 and αˉ2=0.72\bar\alpha_2 = 0.72. Then q(x1∣x2,x0)q(x_1 \mid x_2, x_0) has variance β~2=0.1/0.28⋅0.2=0.0714\tilde\beta_2 = 0.1/0.28 \cdot 0.2 = 0.0714 and mean 0.678 x0+0.319 x20.678\, x_0 + 0.319\, x_2; the Code section recovers these numbers by simulation. At t=1t = 1, αˉ0=1\bar\alpha_0 = 1 gives β~1=0\tilde\beta_1 = 0 and μ~1=x0\tilde\mu_1 = x_0: given the image, there is nothing left to guess, which is why that step is handled by the decoder term L0L_0.

The posterior needs x0x_0, and at generation time there is no image. The network must imitate it from xtx_t alone.

Matching two Gaussians: KL becomes L2#

DDPM does not learn the variance of the reverse step: σt2\sigma_t^2 is fixed in advance. With pθ=N(μθ,σt2I)p_\theta = \mathcal N(\mu_\theta, \sigma_t^2 I), the KL divergence between two 1-D Gaussians, summed over the dd coordinates, is

DKL(q(xt−1∣xt,x0) ∥ pθ)=∥μ~t−μθ∥22σt2+Ct,Ct=d2(β~tσt2−1−ln⁡β~tσt2).\begin{aligned} D_{\mathrm{KL}}\big(q(x_{t-1} \mid x_t, x_0) \,\|\, p_\theta\big) &= \frac{\lVert \tilde\mu_t - \mu_\theta \rVert^2}{2\sigma_t^2} + C_t , \\ C_t &= \frac d2 \Big(\frac{\tilde\beta_t}{\sigma_t^2} - 1 - \ln \frac{\tilde\beta_t}{\sigma_t^2}\Big) . \end{aligned}

CtC_t does not depend on θ\theta, and it is 00 when σt2=β~t\sigma_t^2 = \tilde\beta_t. So each Lt−1L_{t-1} is a squared error between means, plus a constant (Ho, Jain and Abbeel, 2020, Eq. 8).

Figure

With equal variances, the KL divergence is an L2 loss on the means

Two equal-variance Gaussians and the KL divergence as a function of their mean gapLeft: a blue Gaussian centred at mu theta equals minus 1.3 and a green Gaussian of the same width centred at mu tilde t equals 1.4, with dashed lines at both means and arrows from mu theta to mu tilde t, labelled training moves mu theta. Right: an inset plot of the KL divergence against the mean gap, the parabola gap squared over 2 sigma squared, with the current point at gap 2.7 and KL 3.645 and an arrow down the parabola towards zero.μθμ̃ttraining moves μθpθqsame variance: σₜ² = β̃t (= 1 here)KL = ‖μ̃t − μθ‖² / (2σₜ²)when σₜ² = β̃t3.645KL2.70μ̃t − μθTwo equal-variance Gaussians and the KL divergence as a function of their mean gapTop: a blue Gaussian at mu theta and a green Gaussian of the same width at mu tilde t, with arrows from mu theta to mu tilde t. Bottom: the KL divergence against the mean gap, a parabola, with the current point at gap 2.7 and KL 3.645 and an arrow down the parabola towards zero.μθμ̃ttraining moves μθsame variance: σₜ² = β̃t (= 1 here)KL = ‖μ̃t − μθ‖² / (2σₜ²)when σₜ² = β̃t3.645KL2.70μ̃t − μθ
The model's step pθ = N(μθ, σₜ²) against the true posterior q for one pair (xₜ, x₀), with σₜ² equal to the posterior variance β̃ₜ = 1. The KL is then (gap)² / (2σₜ²), the parabola on the right: a gap of 2.7 gives 3.645 nats. Training moves μθ down the parabola.

Which σt2\sigma_t^2? Ho, Jain and Abbeel tried βt\beta_t and β~t\tilde\beta_t and found similar results (§3.2). They are the right variance in two extreme cases. If the data are a single point, the true reverse step is the posterior itself, with variance β~t\tilde\beta_t. If the data are N(0,I)\mathcal N(0, I), every xtx_t is N(0,I)\mathcal N(0, I) and Cov⁡(xt−1,xt)=αt I\operatorname{Cov}(x_{t-1}, x_t) = \sqrt{\alpha_t}\, I, so the Gaussian conditional has variance 1−αt=βt1 - \alpha_t = \beta_t.

What does the network learn? The target μ~t\tilde\mu_t depends on x0x_0, which it does not see, and many images are compatible with the same xtx_t. Since the conditional mean minimizes the expected squared error, the optimum is the average of μ~t\tilde\mu_t over the images compatible with xtx_t, and by the tower property that is the mean of the true reverse step:

μθ∗(xt,t)=E[μ~t(xt,x0)∣xt]=E[xt−1∣xt].\begin{aligned} \mu_\theta^*(x_t, t) &= \mathbb E\big[\tilde\mu_t(x_t, x_0) \mid x_t\big] \\ &= \mathbb E[x_{t-1} \mid x_t] . \end{aligned}

In general the KL terms remain nonzero at this optimum. What remains of the squared error is the variance of μ~t(xt,x0)\tilde\mu_t(x_t, x_0) given xtx_t. Since μ~t\tilde\mu_t is linear in x0x_0, that is the square of its coefficient of x0x_0 times Var⁡(x0∣xt)\operatorname{Var}(x_0 \mid x_t), summed over pixels: it is zero only when xtx_t determines the image.

From means to noise#

The network could output μθ\mu_\theta directly. The lecture notes take a route through the image first. Since μ~t\tilde\mu_t is linear in x0x_0 with coefficient ct=αˉt−1 βt/(1−αˉt)c_t = \sqrt{\bar\alpha_{t-1}}\, \beta_t / (1 - \bar\alpha_t), let a network x^θ(xt,t)\hat x_\theta(x_t, t) guess the image and plug the guess into the posterior mean, μθ=μ~t(xt,x^θ)\mu_\theta = \tilde\mu_t(x_t, \hat x_\theta). Then the means differ only through the images:

μ~t(xt,x0)−μθ=ct (x0−x^θ(xt,t)),Lt−1=ct22σt2 E ∥x0−x^θ(xt,t)∥2+Ct.\begin{aligned} \tilde\mu_t(x_t, x_0) - \mu_\theta &= c_t\, \big(x_0 - \hat x_\theta(x_t, t)\big) , \\ L_{t-1} &= \frac{c_t^2}{2\sigma_t^2}\, \mathbb E\, \lVert x_0 - \hat x_\theta(x_t, t) \rVert^2 + C_t . \end{aligned}

Training is regression of the clean image on the noisy one: a denoiser.

DDPM goes one step further and predicts the noise (Ho, Jain and Abbeel, 2020, §3.2). The one-draw jump xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon can be solved for the image:

x0=1αˉt(xt−1−αˉt ε).x_0 = \frac{1}{\sqrt{\bar\alpha_t}} \big(x_t - \sqrt{1 - \bar\alpha_t}\, \varepsilon\big) .

Substitute this into μ~t\tilde\mu_t and use αˉt−1/αˉt=1/αt\sqrt{\bar\alpha_{t-1}} / \sqrt{\bar\alpha_t} = 1/\sqrt{\alpha_t}, from αˉt=αtαˉt−1\bar\alpha_t = \alpha_t \bar\alpha_{t-1}. The coefficient of xtx_t collects two terms, which add up by βt+αt=1\beta_t + \alpha_t = 1:

βtαt (1−αˉt)+αt (1−αˉt−1)1−αˉt=βt+αt−αˉtαt (1−αˉt)=1αt.\begin{aligned} \frac{\beta_t}{\sqrt{\alpha_t}\,(1 - \bar\alpha_t)} + \frac{\sqrt{\alpha_t}\,(1 - \bar\alpha_{t-1})}{1 - \bar\alpha_t} &= \frac{\beta_t + \alpha_t - \bar\alpha_t}{\sqrt{\alpha_t}\,(1 - \bar\alpha_t)} \\ &= \frac{1}{\sqrt{\alpha_t}} . \end{aligned}

The coefficient of ε\varepsilon is −βt1−αˉt/(αt (1−αˉt))=−βt/(αt1−αˉt)-\beta_t \sqrt{1 - \bar\alpha_t} / \big(\sqrt{\alpha_t}\,(1 - \bar\alpha_t)\big) = -\beta_t / \big(\sqrt{\alpha_t} \sqrt{1 - \bar\alpha_t}\big). Hence

μ~t=1αt(xt−βt1−αˉt ε).\tilde\mu_t = \frac{1}{\sqrt{\alpha_t}} \Big(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\, \varepsilon\Big) .

The network sees xtx_t and tt but not ε\varepsilon, so DDPM gives the model’s mean the same form and lets a network εθ\varepsilon_\theta predict the missing noise (Eq. 11):

  μθ(xt,t)=1αt(xt−βt1−αˉt εθ(xt,t))  \boxed{\;\mu_\theta(x_t, t) = \frac{1}{\sqrt{\alpha_t}} \Big(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\, \varepsilon_\theta(x_t, t)\Big)\;}

In the difference μ~t−μθ\tilde\mu_t - \mu_\theta the xtx_t terms cancel, leaving βt(εθ−ε)/(αt1−αˉt)\beta_t (\varepsilon_\theta - \varepsilon) / (\sqrt{\alpha_t}\sqrt{1 - \bar\alpha_t}). Squaring and dividing by 2σt22\sigma_t^2 gives Eq. 12:

Lt−1−Ct=wt Eε∥ε−εθ(xt,t)∥2,wt=βt22σt2 αt (1−αˉt).\begin{aligned} L_{t-1} - C_t &= w_t\, \mathbb E_{\varepsilon} \big\lVert \varepsilon - \varepsilon_\theta(x_t, t) \big\rVert^2 , \\ w_t &= \frac{\beta_t^2}{2\sigma_t^2\, \alpha_t\, (1 - \bar\alpha_t)} . \end{aligned}

Here x0x_0 is fixed, as in L(x0)L(x_0); the training objective additionally averages over data images. The three parameterizations describe the same model with the same bound. Define the image guess from the noise guess by the same solved jump, x^θ=(xt−1−αˉt εθ)/αˉt\hat x_\theta = (x_t - \sqrt{1 - \bar\alpha_t}\, \varepsilon_\theta) / \sqrt{\bar\alpha_t}; subtracting it from the expression for x0x_0 gives x0−x^θ=−(1−αˉt)/αˉt (ε−εθ)x_0 - \hat x_\theta = -\sqrt{(1 - \bar\alpha_t)/\bar\alpha_t}\, (\varepsilon - \varepsilon_\theta), and ct2(1−αˉt)/αˉt=2σt2wtc_t^2 (1 - \bar\alpha_t)/\bar\alpha_t = 2\sigma_t^2 w_t. They differ in what the network finds easy to output, and in what the unweighted losses below emphasize. Ho, Jain and Abbeel found that predicting x0x_0 gave worse samples early in their experiments, and that predicting μ~\tilde\mu trained well only with the true weights (§3.2, §4.2). The optimal noise predictor is εθ∗=E[ε∣xt]\varepsilon_\theta^* = \mathbb E[\varepsilon \mid x_t], the average noise consistent with xtx_t. Up to the factor −1−αˉt-\sqrt{1 - \bar\alpha_t} it is the score of q(xt)q(x_t), the gradient that appeared in the small-step mean of the reverse process (score matching).

Final simplification: a random timestep per sample#

The bound now reads

L(x0)=const+∑t=2Twt Eε∥ε−εθ(xt,t)∥2+L0.L(x_0) = \text{const} + \sum_{t=2}^{T} w_t\, \mathbb E_\varepsilon \lVert \varepsilon - \varepsilon_\theta(x_t, t) \rVert^2 + L_0 .

Summing all T−1T - 1 terms for every image would cost T−1T - 1 network evaluations per image. Instead we draw one timestep per image. For any ff, ∑t=2Tf(t)=(T−1) Et∼Uniform{2,…,T}f(t)\sum_{t=2}^{T} f(t) = (T - 1)\, \mathbb E_{t \sim \mathrm{Uniform}\{2, \dots, T\}} f(t), so (T−1)f(t)(T - 1)f(t) is an unbiased estimate of that sum. Omitting the constant preserves the minimizer of the sum alone; to retain the full bound, keep its scale relative to the separate decoder term L0L_0. Because xtx_t comes from x0x_0 in one draw, no chain is ever simulated during training.

Ho, Jain and Abbeel then drop the weight (Eq. 14):

Lsimple(θ)=Et,x0,ε∥ε−εθ(xt,t)∥2,xt=αˉt x0+1−αˉt ε,\begin{aligned} L_{\text{simple}}(\theta) &= \mathbb E_{t, x_0, \varepsilon} \big\lVert \varepsilon - \varepsilon_\theta(x_t, t) \big\rVert^2 , \\ x_t &= \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon , \end{aligned}

with tt uniform on {1,…,T}\{1, \dots, T\}. The t=1t = 1 term stands in for L0L_0: the decoder’s bin probability approximated by the Gaussian density times the bin width, ignoring σ12\sigma_1^2 and the edge bins (§3.4).

Dropping wtw_t changes the objective. With DDPM’s linear schedule and σt2=βt\sigma_t^2 = \beta_t, the weight simplifies to wt=βt/(2αt(1−αˉt))w_t = \beta_t / (2\alpha_t (1 - \bar\alpha_t)). It is 0.2730.273 at t=2t = 2, falls to a minimum of 0.00500.0050 at t=349t = 349 and ends at 0.01020.0102 at t=Tt = T: the bound puts 5555 times more weight on the nearly clean step t=2t = 2 than at the minimum. LsimpleL_{\text{simple}} weights every step equally, so relative to the bound it down-weights the small-tt steps, which describe tiny pixel-level corrections, and shifts relative weight toward noisier steps. This does not mean that predicting the added noise is always harder at a higher noise level. With unlimited capacity the weights would not matter: each step’s term is minimized separately by εθ∗=E[ε∣xt]\varepsilon_\theta^* = \mathbb E[\varepsilon \mid x_t], whatever its positive weight. They matter because one network serves all steps and trades errors at one step against another, so minimizing LsimpleL_{\text{simple}} no longer minimizes the bound; Ho, Jain and Abbeel call it a weighted variational bound (§3.4). In the authors’ CIFAR-10 experiment it gave FID 3.173.17 against 13.5113.51 for the true-bound objective, evaluated against training-set statistics, and a slightly worse negative-log-likelihood upper bound, 3.753.75 against 3.703.70 bits per dimension (their Table 1). These are cited research results, not measurements of our local examples.

Figure

The bound's weight favours the nearly clean steps

Loss weight against the timestepA log-log plot of the weight w t against t from 1 to 1000. The blue curve for sigma squared equal to beta t starts at 0.273 at t equals 2, drops steeply, reaches a minimum of 0.0050 at t equals 349 and rises to 0.0102 at t equals 1000. A dashed grey curve for sigma squared equal to beta tilde t starts higher, at 0.600, and merges with the blue curve within a few dozen steps. A flat green line at 1 marks the simple loss.0.0050.010.050.10.511251020501002005001000t (log scale)weight wₜ (log scale)Lsimple: wₜ = 10.273min 0.0050 at t = 3490.0102σₜ² = βₜσₜ² = β̃t (dashed)Loss weight against the timestepA log-log plot of the weight w t against t from 1 to 1000. The blue curve for sigma squared equal to beta t starts at 0.273 at t equals 2, drops steeply, reaches a minimum of 0.0050 at t equals 349 and rises to 0.0102 at t equals 1000. A dashed grey curve for sigma squared equal to beta tilde t starts higher, at 0.600, and merges with the blue curve within a few dozen steps. A flat green line at 1 marks the simple loss.0.0050.010.111101001000t (log scale)weight wₜ (log scale)Lsimple: wₜ = 10.273min 0.0050 at t = 3490.0102σₜ² = βₜσₜ² = β̃t (dashed)
The bound's weight wₜ = βₜ² / (2σₜ² αₜ (1 − ᾱₜ)) for the linear schedule (T = 1000), on logarithmic axes. With σₜ² = βₜ: 0.273 at t = 2, minimum 0.0050 at t = 349, 0.0102 at t = T. Dashed: σₜ² = β̃ₜ (0.600 at t = 2). L_simple uses wₜ = 1.

The training loop is DDPM’s Algorithm 1:

  1. Draw an image x0x_0 from the data.
  2. Draw a timestep tt uniformly from {1,…,T}\{1, \dots, T\} and noise ε∼N(0,I)\varepsilon \sim \mathcal N(0, I).
  3. Form xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon.
  4. Take a gradient step on ∥ε−εθ(xt,t)∥2\lVert \varepsilon - \varepsilon_\theta(x_t, t) \rVert^2, and repeat until converged.
Figure

One training step: draw, jump, predict the noise

Data flow of one DDPM training stepThree input boxes on the left: x0 drawn from the data, t drawn uniformly from 1 to T, and epsilon drawn from N of 0 and I. All three feed a box that computes x t equals square root of alpha bar t times x0 plus square root of 1 minus alpha bar t times epsilon. Its output x t and t go into the blue network box epsilon theta of x t and t. The predicted noise and the true noise epsilon, routed along the bottom, meet in the loss box the squared norm of epsilon minus epsilon theta. The loss feeds a gradient step on theta, which updates the network. A note gives w t equals beta t squared over 2 sigma t squared alpha t times 1 minus alpha bar t, and w t equals 1 for L simple.x₀ ~ dataa training imaget ~ Uniform{1, …, T}a random timestepε ~ N(0, I)fresh Gaussian noisejump straight to xₜxₜ = √α̅ₜ x₀+ √(1 − α̅ₜ) εεθ(xₜ, t)networkxₜ, t‖ε − εθ(xₜ, t)‖²predicted noisetrue noise εgradientstepupdate θKL, t ≥ 2: wₜ = βₜ² / (2σₜ² αₜ (1 − α̅ₜ))Lsimple: t = 1…T, unit weightData flow of one DDPM training stepTop: three input boxes, x0 from the data, t uniform from 1 to T, epsilon from N of 0 and I. They feed the box x t equals square root of alpha bar t times x0 plus square root of 1 minus alpha bar t times epsilon, which feeds the blue network epsilon theta of x t and t. The predicted noise and the true noise epsilon, routed down the right side, meet in the loss box the squared norm of epsilon minus epsilon theta, which feeds a gradient step that updates the network. Notes give the weight from the bound and w t equals 1 for L simple.x₀ ~ dataimaget ~ U{1…T}timestepε ~ N(0, I)noisejump straight to xₜxₜ = √α̅ₜ x₀ + √(1 − α̅ₜ) εεθ(xₜ, t)networkxₜ, t‖ε − εθ(xₜ, t)‖²predicted noisetruenoise εgradient stepupdate θKL, t ≥ 2: wₜ = βₜ² / (2σₜ² αₜ (1 − α̅ₜ))Lsimple: t = 1…T, unit weight
One training step: draw an image x₀, a timestep t and a noise ε, jump straight to xₜ, ask the network for its estimate εθ(xₜ, t), and step on the unweighted squared error. This is L_simple: t from 1 to T, unit weight. The KL sum for t ≥ 2 uses wₜ; the decoder term is separate.

Sampling: running the chain backwards#

Generation does run the chain. DDPM’s Algorithm 2 samples each reverse step pθ(xt−1∣xt)=N(μθ(xt,t),σt2I)p_\theta(x_{t-1} \mid x_t) = \mathcal N(\mu_\theta(x_t, t), \sigma_t^2 I) with the noise form of the mean:

  1. Draw xT∼N(0,I)x_T \sim \mathcal N(0, I).
  2. For t=T,…,1t = T, \dots, 1, draw z∼N(0,I)z \sim \mathcal N(0, I) if t>1t > 1, else set z=0z = 0, and compute
xt−1=1αt(xt−βt1−αˉt εθ(xt,t))+σtz.x_{t-1} = \frac{1}{\sqrt{\alpha_t}} \Big(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\, \varepsilon_\theta(x_t, t)\Big) + \sigma_t z .
  1. Return x0x_0.

Each step removes the network’s estimate of the noise, rescales, and adds a little fresh noise of variance σt2\sigma_t^2, either βt\beta_t or β~t\tilde\beta_t.

The last step, with z=0z = 0, returns the decoder’s mean μθ(x1,1)\mu_\theta(x_1, 1), not a draw from pθ(x0∣x1)p_\theta(x_0 \mid x_1); Ho, Jain and Abbeel display this mean (§3.3). Setting σ12=β~1=0\sigma_1^2 = \tilde\beta_1 = 0 instead defines a deterministic continuous endpoint, not a nondegenerate Gaussian density or the discrete decoder above. With σ12=β1\sigma_1^2 = \beta_1, an exact continuous draw adds σ1z\sigma_1 z; the discrete decoder then maps that draw to the appropriate pixel bin. Returning the mean suppresses this final randomness, but in general it is not a sample from the specified decoder.

How far an exact last step moves a pixel

Take a mean exactly on an interior pixel value, with at least two bins on either side, and σ1=β1=0.01\sigma_1 = \sqrt{\beta_1} = 0.01. The bins have half-width 1/255=0.39 σ11/255 = 0.39\,\sigma_1, so the draw stays in the mean’s bin with probability 2Φ(0.392)−1=0.3052\Phi(0.392) - 1 = 0.305, moves one level up or down with probability 2(Φ(1.176)−Φ(0.392))=0.4562\big(\Phi(1.176) - \Phi(0.392)\big) = 0.456, and moves further with probability 0.2390.239.

Generation costs T=1000T = 1000 network evaluations, one per step, against one for training on an image; this is the main practical cost of DDPM. Samplers that reuse the trained network with far fewer steps are in the latent diffusion note.

Where it breaks: too few, too large steps#

For general data the Gaussian reverse-step approximation relies on sufficiently small steps; Gaussian data are a special case where it can be exact at larger steps too. Take data that are two points, x0∈{−1,+1}x_0 \in \{-1, +1\} with equal weights. For this data the optimal noise predictor is known exactly: E[x0∣xt]=tanh⁡(αˉt xt/(1−αˉt))\mathbb E[x_0 \mid x_t] = \tanh\big(\sqrt{\bar\alpha_t}\, x_t / (1 - \bar\alpha_t)\big), from Bayes’ theorem with two Gaussian likelihoods, and E[ε∣xt]\mathbb E[\varepsilon \mid x_t] follows from the one-draw jump. Run Algorithm 2 with this perfect network and σt2=β~t\sigma_t^2 = \tilde\beta_t, for two schedules that end at the same αˉT=4.0×10−5\bar\alpha_T = 4.0 \times 10^{-5}:

chainsamples within 0.050.05 of ±1\pm 1samples with ∣x∣<0.5\lvert x \rvert < 0.5
T=1000T = 1000, DDPM’s linear schedule1.01.00.00.0
T=10T = 10, constant β=0.636\beta = 0.6360.030.030.470.47

With a thousand small steps all 20,000 samples in this seeded run land near one of the two data points; this tolerance check does not establish equality of distributions. With ten large steps it fails although every step has the exact optimal mean E[xt−1∣xt]\mathbb E[x_{t-1} \mid x_t]. The true reverse step averages the two posteriors over the image, by the law of total probability:

q(xt−1∣xt)=∑x0=±1P(x0∣xt)⋅q(xt−1∣xt,x0).\begin{aligned} q(x_{t-1} \mid x_t) = \textstyle\sum_{x_0 = \pm 1} {} & P(x_0 \mid x_t) \\ & \cdot q(x_{t-1} \mid x_t, x_0) . \end{aligned}

The two Gaussians have the same variance β~t\tilde\beta_t, and their means differ by 2αˉt−1 βt/(1−αˉt)2\sqrt{\bar\alpha_{t-1}}\, \beta_t / (1 - \bar\alpha_t), the coefficient of x0x_0 in μ~t\tilde\mu_t times the distance between the images. The mixture is far from one Gaussian only where both weights P(x0∣xt)P(x_0 \mid x_t) are substantial and this gap is large next to the standard deviation β~t\sqrt{\tilde\beta_t}. In the thousand-step chain the numerical criterion below finds no substantially populated, widely separated pair. In the ten-step chain they do at the last steps. At t=1t = 1, where β~1=0\tilde\beta_1 = 0, the true step goes to −1-1 or +1+1, while the sampler returns their weighted average, which lies between −0.8-0.8 and 0.80.8 for 74%74\% of the x1x_1 of the forward process. Almost half the samples end near 00, where the data have none.

The gap in numbers

Divided by the standard deviation, the gap is

rt=2αˉt−1 βt(1−αˉt)(1−αˉt−1),r_t = \frac{2\sqrt{\bar\alpha_{t-1}\, \beta_t}}{\sqrt{(1 - \bar\alpha_t)(1 - \bar\alpha_{t-1})}} ,

which shrinks like βt\sqrt{\beta_t} at fixed noise levels. In the thousand-step chain, both weights first exceed 0.10.1 for at least 1%1\% of the xtx_t at t=110t = 110, where rt=0.74r_t = 0.74, and rtr_t only falls after that. The early steps, where rtr_t is large (148148 at t=2t = 2), see an xtx_t that still identifies the image, so one weight is close to 11. In the ten-step chain r2=1.29r_2 = 1.29 with almost every x2x_2 ambiguous, and r1r_1 is infinite.

The denoising network#

The derivation treats εθ(xt,t)\varepsilon_\theta(x_t, t) as a function of two arguments. In practice it is one network for all steps, a U-Net, that receives the timestep as an embedding added inside every block.

Figure

The DDPM denoiser: a U-Net that receives the step t

The DDPM denoising U-Net with its timestep embeddingOn the left, x_t enters an encoder of three blocks, 32 by 32, 16 by 16 with attention, and 8 by 8, stepping down to a middle block of 4 by 4; on the right, the decoder climbs back through 8 by 8, 16 by 16 and 32 by 32 to the output epsilon theta. Dashed skip connections join blocks of equal resolution. At the bottom, the step t becomes a sinusoidal code e(t), an MLP gives m(t), and an amber bus with arrows carries it into every block.xt3 × 32 × 3232 × 3216 × 16+ attention8 × 84 × 4te(t)sin, cosMLPm(t)into every block: + Aₖ m(t)32 × 3216 × 16+ attention8 × 8skip connectionεθ3 × 32 × 32‖ε − εθ‖²12345inputstateweights, gatesoutputThe DDPM denoising U-Net with its timestep embeddingLeft column: x_t and the encoder blocks 32 by 32, 16 by 16 with attention, and 8 by 8 from top to bottom, then the middle block 4 by 4. Right column: the decoder from bottom to top and the output epsilon theta. Short dashed skip connections join the columns. At the bottom, t, the code e(t) and the MLP; an amber bus runs around the figure and brings m(t) to every block.xt32 × 3216 × 16+ attention8 × 84 × 4te(t)sin, cosMLPm(t)+ Aₖ m(t)32 × 3216 × 16+ attention8 × 8skipεθ‖ε − εθ‖²12345inputstateweights, gatesoutput
Schematic from Ho et al. (2020), Appendix B. Each box condenses a resolution stage: 32 → 16 → 8 → 4 and back. Skip connections join matching sizes; the timestep embedding enters residual blocks. This depicts the architecture, not an executed model.

One network for all timesteps#

Neighbouring steps solve almost the same problem, so DDPM shares the parameters across time instead of training a thousand networks, and tells the network which step it is at (§4). The same input calls for different outputs at different noise levels: the noise in a given xtx_t is a small part of it when tt is small and almost all of it when tt is large.

An example: one input, two noise levels

For 1-D data N(0,0.52)\mathcal N(0, 0.5^2), ε\varepsilon and xtx_t are jointly Gaussian with covariance 1−αˉt\sqrt{1 - \bar\alpha_t}, so the Gaussian conditional mean is

E[ε∣xt]=1−αˉt0.25 αˉt+1−αˉt xt.\mathbb E[\varepsilon \mid x_t] = \frac{\sqrt{1 - \bar\alpha_t}}{0.25\, \bar\alpha_t + 1 - \bar\alpha_t}\, x_t .

With the linear schedule and xt=1x_t = 1 it is 0.170.17 at t=10t = 10 (αˉt=0.998\bar\alpha_t = 0.998), where xtx_t is mostly image, and 1.021.02 at t=500t = 500 (αˉt=0.079\bar\alpha_t = 0.079), where xtx_t is mostly noise.

A U-Net#

The output has the shape of the input, one noise value per pixel: a dense prediction problem that needs context from far away and precision at the pixel itself. A U-Net, built in the segmentation note of the Deep Learning course, provides both: an encoder halves the resolution level by level, a decoder doubles it back, and skip connections bring the encoder’s features of each size to the decoder. DDPM’s network follows the PixelCNN++ backbone, a U-Net of Wide ResNet blocks, with group normalization and self-attention at the 16×1616 \times 16 resolution (§4 and Appendix B).

Telling the network the timestep#

DDPM uses a sinusoidal embedding of tt. One common frequency convention is the Transformer position code (positional encodings):

e(t)2i=sin⁡(ωit),e(t)2i+1=cos⁡(ωit),ωi=10000−2i/d,\begin{aligned} e(t)_{2i} &= \sin(\omega_i t) , \\ e(t)_{2i+1} &= \cos(\omega_i t) , \\ \omega_i &= 10000^{-2i/d} , \end{aligned}

for i=0,…,d/2−1i = 0, \dots, d/2 - 1.

A small MLP turns the code into a feature vector m(t)m(t), and every residual block kk adds its own linear projection Ak m(t)A_k\, m(t) to its feature maps, one bias per channel. A raw number tt would give the MLP one input running up to 10001000, from which it would have to build every function of tt it needs. The sinusoidal code supplies them ready-made: its components turn at rates from ω0=1\omega_0 = 1 down to ωd/2−1≈10−4\omega_{d/2-1} \approx 10^{-4} radians per step, periods from about 66 to about 60,00060{,}000 steps, so linear combinations of it can follow both fast and slow changes in tt. Alternatives set a scale and a shift of each channel after group normalization from the embedding (Murphy, 2023, §25.2), and text enters through cross-attention.

Why the pixels are scaled to [−1, 1]#

DDPM maps pixel values 0,…,2550, \dots, 255 linearly to [−1,1][-1, 1] (§3.3). The chain removes the image only by shrinking it, and with αˉT=0.0064\sqrt{\bar\alpha_T} = 0.0064 the leftover is negligible only for values of order 11 (forward process). By the variance recursion Var⁡(xt)=αˉtVar⁡(x0)+1−αˉt\operatorname{Var}(x_t) = \bar\alpha_t \operatorname{Var}(x_0) + 1 - \bar\alpha_t, every step then sees inputs of similar size, and the decoder’s bins of width 2/2552/255 end at ±1\pm 1.

To display an image, xx is clipped to [−1,1][-1, 1] and mapped back by 255 (x+1)/2255\,(x + 1)/2, because noisy states leave the interval routinely.

How often, and what goes wrong without the clip

A pixel with x0=0.9x_0 = 0.9 at a step with αˉt=0.5\bar\alpha_t = 0.5 has xt∼N(0.64, 0.5)x_t \sim \mathcal N(0.64,\ 0.5), which falls outside [−1,1][-1, 1] with probability 0.310.31. An unchecked NumPy cast to unsigned 8-bit integers can wrap out-of-range values into wrong colours; other image libraries may saturate or reject them. Clipping explicitly makes the intended display conversion clear.

Training in practice: averaged weights and clipped gradients#

An exponential moving average of the weights. Keep a second copy θˉ\bar\theta of the parameters and update it after every optimizer step:

θˉ←μ θˉ+(1−μ) θ.\bar\theta \leftarrow \mu\, \bar\theta + (1 - \mu)\, \theta .

Unrolled over nn steps, θˉ\bar\theta is a weighted average of the past iterates with weights (1−μ) μn−k(1 - \mu)\, \mu^{n-k}, plus μn\mu^n times its starting value: roughly an average over the last 1/(1−μ)1/(1 - \mu) steps, which carry the weight 1−μ1/(1−μ)≈1−1/e=0.631 - \mu^{1/(1-\mu)} \approx 1 - 1/e = 0.63. Training continues with θ\theta; samples are drawn with θˉ\bar\theta. Every example brings its own image, timestep and noise, so the iterates jitter around a good region. By the triangle inequality, its distance to any fixed point is bounded by the same weighted average of distances. This can smooth cancelling fluctuations, but does not guarantee a lower loss or better samples for a nonlinear network. DDPM uses μ=0.9999\mu = 0.9999 (Appendix B), a window of about 10,00010{,}000 steps, and Nichol and Dhariwal (2021) found 0.99990.9999 and 0.999950.99995 best in a sweep.

How long the initialization remains influential

The starting value keeps the weight μn\mu^n: 0.999910 000=0.370.9999^{10\,000} = 0.37 and 0.999950 000=0.00670.9999^{50\,000} = 0.0067, so a large window retains substantial initialization weight for a long time. Whether early EMA samples are useful depends on initialization and training; choose the checkpoint using validation, rather than treating this arithmetic as a universal warm-up rule.

Gradient clipping. Before each step, rescale the gradient gg when its norm exceeds a threshold cc, g←g⋅min⁡(1,c/∥g∥)g \leftarrow g \cdot \min(1, c / \lVert g \rVert) (training background). For a zero gradient leave it unchanged. This caps the raw gradient norm and preserves its direction; clipping may occur frequently if cc is too small. It bounds a plain SGD update by the learning rate times cc, but does not directly bound an adaptive optimizer’s full update.

What you should remember#

  • The bound splits into LTL_T (constant), Lt−1=E DKL(q(xt−1∣xt,x0) ∥ pθ)L_{t-1} = \mathbb E\, D_{\mathrm{KL}}(q(x_{t-1} \mid x_t, x_0) \,\|\, p_\theta) and the decoder term L0L_0, which depends on θ\theta.
  • Given x0x_0 the reversed chain is Markov, and q(xt−1∣xt,x0)=N(μ~t,β~tI)q(x_{t-1} \mid x_t, x_0) = \mathcal N(\tilde\mu_t, \tilde\beta_t I) by Bayes and completing the square.
  • With fixed variances each KL is ∥μ~t−μθ∥2/(2σt2)\lVert \tilde\mu_t - \mu_\theta \rVert^2 / (2\sigma_t^2) plus a constant.
  • μθ=(xt−βtεθ/1−αˉt)/αt\mu_\theta = \big(x_t - \beta_t \varepsilon_\theta / \sqrt{1 - \bar\alpha_t}\big) / \sqrt{\alpha_t} turns it into wt∥ε−εθ∥2w_t \lVert \varepsilon - \varepsilon_\theta \rVert^2.
  • LsimpleL_{\text{simple}} sets wt=1w_t = 1 and changes the trade-off across steps. The original paper reports better FID and a slightly worse NLL bound in its comparison; this is not a universal guarantee.
  • Training jumps straight to xtx_t; sampling takes TT steps, which must be small for the Gaussian step to hold.

Code#

The posterior of the toy example checked by simulation, the weights of the bound for DDPM’s schedule, and Algorithm 2 with the exact noise predictor for two-point data, with many small steps and with ten large ones. It uses NumPy and runs in a few seconds.

import numpy as np
rng = np.random.default_rng(0)

# 1. the posterior q(x1 | x2, x0) for beta1 = 0.1, beta2 = 0.2, by regression on simulated pairs
b1, b2 = 0.1, 0.2
ab1, ab2 = 1 - b1, (1 - b1) * (1 - b2)
x0 = 1.0
x1 = np.sqrt(ab1) * x0 + np.sqrt(1 - ab1) * rng.standard_normal(1_000_000)
x2 = np.sqrt(1 - b2) * x1 + np.sqrt(b2) * rng.standard_normal(x1.size)
slope, icpt = np.polyfit(x2, x1, 1)                      # for fixed x0, E[x1 | x2] is linear in x2
resid = (x1 - slope * x2 - icpt).var()
print(np.round([slope, icpt, resid], 4))                  # simulated
print(np.round([np.sqrt(1 - b2) * (1 - ab1) / (1 - ab2),  # formula: coefficient of x_t,
                np.sqrt(ab1) * b2 / (1 - ab2) * x0,       # coefficient of x0 times x0,
                (1 - ab1) / (1 - ab2) * b2], 4))          # and the variance tilde beta
# [0.3202 0.6771 0.0715]
# [0.3194 0.6776 0.0714]

# 2. the bound's weight on ||eps - eps_theta||^2 for DDPM's linear schedule, sigma_t^2 = beta_t
T = 1000
beta = np.linspace(1e-4, 0.02, T); alpha = 1 - beta; abar = np.cumprod(alpha)
w = beta / (2 * alpha * (1 - abar))                       # beta^2 / (2 sigma^2 alpha (1 - abar)) with sigma^2 = beta
t_min = np.argmin(w[1:]) + 2                              # steps t = 2..T
print(round(w[1], 4), round(w.min(), 4), t_min, round(w[-1], 4), round(w[1] / w.min(), 1))
# 0.2727 0.005 349 0.0102 54.9

# 3. sampling (Algorithm 2) with the exact optimal noise predictor for data x0 in {-1, +1}
def sample(beta, n=20_000):
    alpha = 1 - beta; abar = np.cumprod(alpha)
    abar_prev = np.concatenate([[1.0], abar[:-1]])
    x = rng.standard_normal(n)
    for t in range(len(beta) - 1, -1, -1):
        x0_hat = np.tanh(np.sqrt(abar[t]) * x / (1 - abar[t]))          # E[x0 | x_t] for two equal points
        eps = (x - np.sqrt(abar[t]) * x0_hat) / np.sqrt(1 - abar[t])    # E[eps | x_t]
        mean = (x - beta[t] / np.sqrt(1 - abar[t]) * eps) / np.sqrt(alpha[t])
        var = (1 - abar_prev[t]) / (1 - abar[t]) * beta[t]              # sigma_t^2 = tilde beta_t
        x = mean + np.sqrt(var) * rng.standard_normal(n) if t > 0 else mean
    return x

few = np.full(10, 1 - abar[-1] ** 0.1)                    # 10 steps with the same abar_T as the 1000-step chain
for b in (beta, few):
    x = sample(b)
    print(len(b), round(np.prod(1 - b), 7), round(np.mean(np.abs(np.abs(x) - 1) < 0.05), 3), round(np.mean(np.abs(x) < 0.5), 3))
# 1000 4.04e-05 1.0 0.0
# 10 4.04e-05 0.03 0.47

Notebook#

For optional practice, run python3 scripts/notes/generative-practice.py ddpm-training-objective. The standard-library experiment creates 256 grayscale 8×88\times8 signals from a fixed cosine background mm and a sinusoidal stripe pp with random amplitude a∼N(0,0.36)a\sim\mathcal N(0,0.36):

x0=m+ap,xt=0.55 x0+0.45 ε.x_0=m+ap,\qquad x_t=\sqrt{0.55}\,x_0+\sqrt{0.45}\,\varepsilon.

Here ∥p∥2=32\lVert p\rVert^2=32. Subtract the known background and project onto pp. This leaves a one-dimensional Gaussian inference problem. Its posterior variance and mean are

V=(1/0.36+0.55∥p∥2/0.45)−1,a^=V0.550.45p⊤(xt−0.55m).V=\left(1/0.36+0.55\lVert p\rVert^2/0.45\right)^{-1},\qquad \hat a=V\frac{\sqrt{0.55}}{0.45}p^\top(x_t-\sqrt{0.55}m).

Use x^0=m+a^p\hat x_0=m+\hat a p to obtain the noise estimate from the rearranged forward equation. The saved run has noise-prediction MSE 0.014848, compared with 0.995149 for always predicting zero noise; signal MSE is 0.012148. The script checks that signal MSE equals noise MSE times (1−αˉ)/αˉ(1-\bar\alpha)/\bar\alpha.

Change the signal prior variance or noise strength and rerun both the posterior calculation and the check. Which part of the observation identifies the stripe amplitude? Why can the estimator remove noise orthogonal to the stripe particularly well? The signal family is deliberately only one random degree of freedom; these errors are not image-generation quality scores. This exercise performs one forward step and exact conditional denoising, not U-Net training or a reverse chain. The signals and code were authored for this project with Codex assistance, with no external dataset or checkpoint. Gaussian signal amplitudes are unbounded; clipping for display would change the statistical model.

Next#

DDPM reaches its loss through a likelihood bound. The next note reaches the same loss without one: it learns the gradient of the log-density of noised data, samples by following it, and shows that the noise predictor is that gradient up to a known scale.

References#

  • J. Ho, A. Jain, P. Abbeel. Denoising diffusion probabilistic models. NeurIPS, 2020. §2 and Eq. 5–14 (the bound, the posterior and the parameterizations), §3.3–3.4 (the decoder and LsimpleL_{\text{simple}}), Algorithms 1 and 2, §4 and Table 1, Appendix B (architecture and training settings).
  • Конспект курса «Байесовские методы в анализе данных», 2026, BMML lecture 5, лекция 5 (the ELBO of the chain, Lemma 1, the split into L0L_0, LtL_t, LTL_T, the parameterizations and the algorithms).
  • R. Isachenko. Deep Generative Models, MIPT and Yandex School of Data Analysis, 2025. Course repository; lecture 7 (the reverse process and the small-step result), lecture 8 (Gaussian diffusion as a VAE, the ELBO and its reparameterization).
  • K. P. Murphy. Probabilistic Machine Learning: Advanced Topics. MIT Press, 2023. §25.2 (DDPM as a hierarchical VAE with a fixed encoder, model fitting, the U-Net and time conditioning).
  • J. Sohl-Dickstein, E. A. Weiss, N. Maheswaranathan, S. Ganguli. Deep unsupervised learning using nonequilibrium thermodynamics. ICML, 2015. §2.2 (the reverse of a small Gaussian step is Gaussian, after Feller).
  • A. Q. Nichol, P. Dhariwal. Improved denoising diffusion probabilistic models. ICML, 2021. Appendix G and Figure 17 (the EMA sweep; arXiv v1).