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 into almost pure noise in 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 for the whole chain ,
DDPM (Ho, Jain and Abbeel, 2020) makes every reverse step a Gaussian:
The mean comes from one network shared by all steps, a U-Net that receives as an extra input. The variance is fixed by the schedule and not learned; DDPM uses .
Why a Gaussian? The true reverse step is not Gaussian in general: it mixes over every image that could have led to . But when is small, one step moves 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 ? As always, by maximum likelihood of real images. The model's density of an image is the marginal of the whole chain:
This integral runs over every path from noise to : 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 , the decoder is the reverse process, and the role of the encoder is played by the forward process . The ELBO derivation goes through word for word:
As in the VAE, the gap is the KL between the encoder and the model's true posterior , 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 as the data, and there are layers of them. Only the decoder learns.
One KL per step
The ELBO is one expectation over a whole chain. Write and 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 given . The chain is Markov, so , and Bayes' theorem turns each forward step into a backward step that also knows . Reversed in time, with held fixed, the chain is still Markov: once and the image are known, the later, noisier states say nothing more about . The chain rule, applied from the last state backwards, then gives
Divide by it, and every reverse step is paired with a backward step of the same two variables. Each pair involves only and , and the average of its over is by definition a KL. For the negative ELBO, the loss we minimize, the result is (Ho et al., Eq. 5)
Read it term by term.
- compares the end of the forward chain with the noise that generation starts from. Both are fixed, so it contains no . It is almost zero anyway: the linear schedule ends at .
- is the reconstruction term: how likely the last reverse step makes the clean image, given the barely noisy .
- for do the work. Each compares the model's reverse step with , the true posterior: the reverse step of the forward process when the clean image is known.
The true posterior
Unlike , the true posterior is exactly Gaussian, for any step size. By Bayes' theorem it is proportional to . Both factors are Gaussians in . Read as a function of , the first is centred at with precision (inverse variance) , the second at with precision . 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 and the summed precision is , and the result is (Ho et al., Eq. 6–7):
The mean is a weighted mix of the clean image and the current noisy state. The variance is a little smaller than , since . With the convention we get : at the first step, knowing leaves nothing random. That is why is handled by instead.
is also the second standard choice for the model's variance. Ho et al. report similar samples with and .
The KL becomes a squared error
Both sides of each are now Gaussians with fixed variances: for the true posterior and 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:
Here does not depend on , and when . 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 but not , and many clean images could have produced the same . A squared error is minimized by the average target, so the best possible network outputs
the mean of the true reverse step from the first block: averaging the posterior over the images compatible with gives back . Even this network leaves above zero, unless pins down the image.
Predict the noise
We could let the network output directly. DDPM does something better: it rewrites the target so that only one unknown piece is left.
During training, is made by the closed form of the forward process, . So can be expressed through and the noise . Substitute into and use , from . The two coefficients of then add up to , because , and the coefficients collapse (Ho et al., Eq. 10):
Read it as a recipe: take , subtract a scaled copy of the noise that was added, rescale. At sampling time everything here is known except . So give the model's mean the same form, and let a network predict the noise (Ho et al., Eq. 11):
In the difference the cancels, and only 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: is a deterministic function of and of noise drawn outside the network.
The simple loss
Summed over steps, the bound is a weighted noise-prediction loss,
plus terms without . Two simplifications turn it into the loss DDPM actually trains.
First, do not sum over all steps. Draw one step uniformly per image. Since , 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 . Each comes straight from the closed form, so training never runs the chain.
Second, drop the weight. Ho et al. set and include , where the noise regression stands in for :
This is a deliberate reweighting. Under the paper's linear schedule (, from to , ) the bound's weight is about at and falls to near . With 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 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 :
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 . For , sample from the reverse step, with the mean in its noise form:
At the last step , so 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 calls, one after another, because each step needs the result of the previous one. With 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 it cannot know which noise was actually added. What it learns is an average, : the noise that best explains . 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.
Answers are saved on the server and tied to this browser until you sign in.