Skip to content
BIASED

DDPM: reverse process and objective

How to run the noising chain backwards. The ELBO of a VAE with a fixed encoder splits into per-step KL terms, and training reduces to predicting the added noise.

Reverse Gaussian steps

The topic DDPM: the forward process built a fixed chain that turns an image x0x_0 into almost pure noise xTx_T in TT small Gaussian steps. Nothing in it is learned. To generate, we have to walk the chain the other way: start from noise and remove it one step at a time. This topic derives what a network must learn to do that, and how to train it.

The model of the way back is the reverse process, again a Markov chain. Writing x0:Tx_{0:T} for the whole chain (x0,…,xT)(x_0, \dots, x_T),

pθ(x0:T)=p(xT)∏t=1Tpθ(xt−1∣xt),p(xT)=N(0,I).p_\theta(x_{0:T}) = p(x_T) \prod_{t=1}^{T} p_\theta(x_{t-1} \mid x_t), \qquad p(x_T) = \mathcal N(0, I).

DDPM (Ho, Jain and Abbeel, 2020) makes every reverse step a Gaussian:

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).

The mean comes from one network shared by all steps, a U-Net that receives tt as an extra input. The variance σt2\sigma_t^2 is fixed by the schedule and not learned; DDPM uses σt2=βt\sigma_t^2 = \beta_t.

Why a Gaussian? The true reverse step q(xt−1∣xt)q(x_{t-1} \mid x_t) is not Gaussian in general: it mixes over every image that could have led to xtx_t. But when βt\beta_t is small, one step moves xt−1x_{t-1} only a little, and over such a short distance the reverse step is close to a Gaussian (Feller, 1949; Sohl-Dickstein et al., 2015). That is why diffusion uses many small steps rather than a few large ones.

The ELBO again

How do we fit θ\theta? As always, by maximum likelihood of real images. The model's density of an image is the marginal of the whole chain:

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

This integral runs over every path from noise to x0x_0: TT states, each the size of an image. The network inside the steps makes it non-Gaussian, and it has no closed form. It is the same obstacle as the evidence in Latent variables and the ELBO.

The way around is the same too. Read DDPM as a latent-variable model. The latents are the noisy images x1:Tx_{1:T}, the decoder is the reverse process, and the role of the encoder qφ(z∣x)q_\varphi(z \mid x) is played by the forward process q(x1:T∣x0)q(x_{1:T} \mid x_0). The ELBO derivation goes through word for word:

log⁡pθ(x0)≥Eq(x1:T∣x0)[log⁡pθ(x0:T)q(x1:T∣x0)]=:L(x0).\log p_\theta(x_0) \ge \mathbb E_{q(x_{1:T} \mid x_0)}\left[\log \frac{p_\theta(x_{0:T})}{q(x_{1:T} \mid x_0)}\right] =: \mathcal L(x_0).

As in the VAE, the gap is the KL between the encoder and the model's true posterior pθ(x1:T∣x0)p_\theta(x_{1:T} \mid x_0), which we cannot compute.

So DDPM is a VAE with an unusual encoder. It has no parameters and is fixed before training. Its latents have the same dimension DD as the data, and there are TT layers of them. Only the decoder learns.

One KL per step

The ELBO is one expectation over a whole chain. Write pθ(x0:T)p_\theta(x_{0:T}) and q(x1:T∣x0)q(x_{1:T} \mid x_0) as products of steps, and it falls apart into one term per step. One trick is needed: the forward process has to be written backwards, so that each of its factors, like a reverse step, is a density of xt−1x_{t-1} given xtx_t. The chain is Markov, so q(xt∣xt−1)=q(xt∣xt−1,x0)q(x_t \mid x_{t-1}) = q(x_t \mid x_{t-1}, x_0), and Bayes' theorem turns each forward step into a backward step that also knows x0x_0. Reversed in time, with x0x_0 held fixed, the chain is still Markov: once xtx_t and the image are known, the later, noisier states say nothing more about xt−1x_{t-1}. The chain rule, applied from the last state backwards, then gives

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

Divide pθ(x0:T)=p(xT) pθ(x0∣x1)∏t=2Tpθ(xt−1∣xt)p_\theta(x_{0:T}) = p(x_T)\, p_\theta(x_0 \mid x_1) \prod_{t=2}^{T} p_\theta(x_{t-1} \mid x_t) by it, and every reverse step is paired with a backward step of the same two variables. Each pair involves only xt−1x_{t-1} and xtx_t, and the average of its log⁡(q/pθ)\log(q / p_\theta) over xt−1∼q(xt−1∣xt,x0)x_{t-1} \sim q(x_{t-1} \mid x_t, x_0) is by definition a KL. For the negative ELBO, the loss we minimize, the result is (Ho et al., Eq. 5)

−L(x0)=Eq[ DKL(q(xT∣x0) ∥ p(xT))⏟LT+∑t=2TDKL(q(xt−1∣xt,x0) ∥ pθ(xt−1∣xt))⏟Lt−1 − log⁡pθ(x0∣x1)⏟L0 ].\begin{aligned} -\mathcal L(x_0) = \mathbb E_q\Big[\, &\underbrace{D_{\mathrm{KL}}\big(q(x_T \mid x_0) \,\|\, p(x_T)\big)}_{L_T} \\ &+ \sum_{t=2}^{T} \underbrace{D_{\mathrm{KL}}\big(q(x_{t-1} \mid x_t, x_0) \,\|\, p_\theta(x_{t-1} \mid x_t)\big)}_{L_{t-1}} \ \underbrace{-\,\log p_\theta(x_0 \mid x_1)}_{L_0} \,\Big]. \end{aligned}

Read it term by term.

  • LTL_T compares the end of the forward chain with the noise that generation starts from. Both are fixed, so it contains no θ\theta. It is almost zero anyway: the linear schedule ends at αˉT≈4⋅10−5\bar\alpha_T \approx 4 \cdot 10^{-5}.
  • L0L_0 is the reconstruction term: how likely the last reverse step makes the clean image, given the barely noisy x1x_1.
  • Lt−1L_{t-1} for t=2,…,Tt = 2, \dots, T do the work. Each compares the model's reverse step with q(xt−1∣xt,x0)q(x_{t-1} \mid x_t, x_0), the true posterior: the reverse step of the forward process when the clean image is known.

The true posterior

Unlike q(xt−1∣xt)q(x_{t-1} \mid x_t), the true posterior is exactly Gaussian, for any step size. By Bayes' theorem it is proportional to q(xt∣xt−1) q(xt−1∣x0)q(x_t \mid x_{t-1})\, q(x_{t-1} \mid x_0). Both factors are Gaussians in xt−1x_{t-1}. Read as a function of xt−1x_{t-1}, the first is centred at xt/αtx_t / \sqrt{\alpha_t} with precision (inverse variance) αt/βt\alpha_t / \beta_t, the second at αˉt−1 x0\sqrt{\bar\alpha_{t-1}}\, x_0 with precision 1/(1−αˉt−1)1 / (1 - \bar\alpha_{t-1}). Completing the square in their product gives a Gaussian again: the precisions add, and the mean is the precision-weighted average of the two centres. With αtαˉt−1=αˉt\alpha_t \bar\alpha_{t-1} = \bar\alpha_t and αt+βt=1\alpha_t + \beta_t = 1 the summed precision is 1−αˉtβt(1−αˉt−1)\frac{1 - \bar\alpha_t}{\beta_t (1 - \bar\alpha_{t-1})}, and the result is (Ho et al., Eq. 6–7):

q(xt−1∣xt,x0)=N(μ~t(xt,x0), β~tI),q(x_{t-1} \mid x_t, x_0) = \mathcal N\big(\tilde\mu_t(x_t, x_0),\ \tilde\beta_t I\big), μ~t(xt,x0)=αˉt−1 βt1−αˉt x0+αt (1−αˉt−1)1−αˉt xt,β~t=1−αˉt−11−αˉt βt.\tilde\mu_t(x_t, x_0) = \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, \qquad \tilde\beta_t = \frac{1 - \bar\alpha_{t-1}}{1 - \bar\alpha_t}\, \beta_t .

The mean is a weighted mix of the clean image and the current noisy state. The variance is a little smaller than βt\beta_t, since 1−αˉt−1<1−αˉt1 - \bar\alpha_{t-1} < 1 - \bar\alpha_t. With the convention αˉ0=1\bar\alpha_0 = 1 we get β~1=0\tilde\beta_1 = 0: at the first step, knowing x0x_0 leaves nothing random. That is why t=1t = 1 is handled by L0L_0 instead.

β~t\tilde\beta_t is also the second standard choice for the model's variance. Ho et al. report similar samples with σt2=βt\sigma_t^2 = \beta_t and σt2=β~t\sigma_t^2 = \tilde\beta_t.

The KL becomes a squared error

Both sides of each Lt−1L_{t-1} are now Gaussians with fixed variances: β~tI\tilde\beta_t I for the true posterior and σt2I\sigma_t^2 I for the model. The KL between two Gaussians has a closed form; The reparameterization trick used it against a standard normal. When only the means are free, everything except the distance between them is a constant:

Lt−1=12σt2 ∥μ~t(xt,x0)−μθ(xt,t)∥2+C.L_{t-1} = \frac{1}{2\sigma_t^2}\, \big\|\tilde\mu_t(x_t, x_0) - \mu_\theta(x_t, t)\big\|^2 + C .

Here CC does not depend on θ\theta, and C=0C = 0 when σt2=β~t\sigma_t^2 = \tilde\beta_t. So each KL term is a squared error: the network's mean should land on the mean of the true posterior.

One subtlety. The network sees xtx_t but not x0x_0, and many clean images could have produced the same xtx_t. A squared error is minimized by the average target, so the best possible network outputs

μθ∗(xt,t)=E[μ~t(xt,x0)∣xt]=E[xt−1∣xt],\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],

the mean of the true reverse step from the first block: averaging the posterior over the images compatible with xtx_t gives back q(xt−1∣xt)q(x_{t-1} \mid x_t). Even this network leaves Lt−1L_{t-1} above zero, unless xtx_t pins down the image.

Predict the noise

We could let the network output μθ\mu_\theta directly. DDPM does something better: it rewrites the target so that only one unknown piece is left.

During training, xtx_t is made by the closed form of the forward process, xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1 - \bar\alpha_t}\,\varepsilon. So x0x_0 can be expressed through xtx_t and the noise ε\varepsilon. Substitute x0=(xt−1−αˉt ε)/αˉtx_0 = \big(x_t - \sqrt{1 - \bar\alpha_t}\,\varepsilon\big) / \sqrt{\bar\alpha_t} 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 two coefficients of xtx_t then add up to βt+αt(1−αˉt−1)αt (1−αˉt)=1αt\frac{\beta_t + \alpha_t (1 - \bar\alpha_{t-1})}{\sqrt{\alpha_t}\,(1 - \bar\alpha_t)} = \frac{1}{\sqrt{\alpha_t}}, because βt+αt−αˉt=1−αˉt\beta_t + \alpha_t - \bar\alpha_t = 1 - \bar\alpha_t, and the coefficients collapse (Ho et al., Eq. 10):

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

Read it as a recipe: take xtx_t, subtract a scaled copy of the noise that was added, rescale. At sampling time everything here is known except ε\varepsilon. So give the model's mean the same form, and let a network εθ(xt,t)\varepsilon_\theta(x_t, t) predict the noise (Ho et al., Eq. 11):

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

In the difference μ~t−μθ\tilde\mu_t - \mu_\theta the xtx_t cancels, and only εθ−ε\varepsilon_\theta - \varepsilon survives, times a constant. Each KL term becomes a weighted squared error between the true noise and the predicted one. This is the reparameterization trick once more: xtx_t is a deterministic function of x0x_0 and of noise drawn outside the network.

The simple loss

Summed over steps, the bound is a weighted noise-prediction loss,

∑t=2Twt E ∥ε−εθ(xt,t)∥2,wt=βt22σt2 αt (1−αˉt),\sum_{t=2}^{T} w_t\, \mathbb E\,\big\|\varepsilon - \varepsilon_\theta(x_t, t)\big\|^2, \qquad w_t = \frac{\beta_t^2}{2\sigma_t^2\, \alpha_t\, (1 - \bar\alpha_t)} ,

plus terms without θ\theta. Two simplifications turn it into the loss DDPM actually trains.

First, do not sum over all steps. Draw one step tt uniformly per image. Since ∑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), this gives an unbiased estimate of the sum up to a constant factor, which does not move the minimizer, at the cost of one network call instead of T−1T - 1. Each xtx_t comes straight from the closed form, so training never runs the chain.

Second, drop the weight. Ho et al. set wt=1w_t = 1 and include t=1t = 1, where the noise regression stands in for L0L_0:

Lsimple(θ)=Et, x0, ε[∥ε−εθ(αˉt x0+1−αˉt ε, t)∥2],t∼Uniform{1,…,T}.L_{\text{simple}}(\theta) = \mathbb E_{t,\, x_0,\, \varepsilon}\Big[\big\|\varepsilon - \varepsilon_\theta\big(\sqrt{\bar\alpha_t}\,x_0 + \sqrt{1 - \bar\alpha_t}\,\varepsilon,\ t\big)\big\|^2\Big], \qquad t \sim \mathrm{Uniform}\{1, \dots, T\}.

This is a deliberate reweighting. Under the paper's linear schedule (T=1000T = 1000, βt\beta_t from 10−410^{-4} to 0.020.02, σt2=βt\sigma_t^2 = \beta_t) the bound's weight is about 0.270.27 at t=2t = 2 and falls to 0.0050.005 near t=350t = 350. With wt=1w_t = 1 the noisier steps count relatively more. With unlimited capacity this would change nothing: each step's term would be minimized separately, by the same function whatever its positive weight. It matters because one network serves all steps and trades its errors at one step against another, so minimizing LsimpleL_{\text{simple}} no longer minimizes the bound. Ho et al. (§4.2) found the trade worth it: better samples, a slightly worse bound.

One training step (Algorithm 1), with abar holding αˉ0=1,αˉ1,…,αˉT\bar\alpha_0 = 1, \bar\alpha_1, \dots, \bar\alpha_T:

x0 = next(data)                                   # (B, D) batch of images
t = torch.randint(1, T + 1, (B,))                 # one random step per image
eps = torch.randn_like(x0)
x_t = abar[t].sqrt()[:, None] * x0 + (1 - abar[t]).sqrt()[:, None] * eps
loss = ((eps - eps_model(x_t, t)) ** 2).sum(dim=1).mean()
loss.backward()

Sampling

Generation runs the reverse chain (Algorithm 2). Start from xT∼N(0,I)x_T \sim \mathcal N(0, I). For t=T,…,1t = T, \dots, 1, sample from the reverse step, with the mean in its noise form:

xt−1=1αt(xt−βt1−αˉt εθ(xt,t))+σtz,z∼N(0,I).x_{t-1} = \frac{1}{\sqrt{\alpha_t}}\left(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\,\varepsilon_\theta(x_t, t)\right) + \sigma_t z, \qquad z \sim \mathcal N(0, I).

At the last step z=0z = 0, so x0x_0 is the mean rather than a noisy draw. Each step removes the network's estimate of the noise, rescales, and adds a little fresh noise back.

The price is the loop. Training needs one network call per image. Sampling needs TT calls, one after another, because each step needs the result of the previous one. With T=1000T = 1000 that is a thousand passes through a U-Net for every batch of samples, against a single pass for a VAE decoder, a GAN generator or a flow.

One question is still open. The network was trained to guess the noise, but without x0x_0 it cannot know which noise was actually added. What it learns is an average, εθ∗(xt,t)=E[ε∣xt]\varepsilon_\theta^*(x_t, t) = \mathbb E[\varepsilon \mid x_t]: the noise that best explains xtx_t. Why is that enough to walk from pure noise to an image? Because, up to a scale and a sign, it is the gradient of the log-density of noisy images, a quantity called the score. The next topic, Score matching, starts from there.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.