Foundations to Frontier
Tests
H. Generative Models › H7 Diffusion Models

DDPM: forward and reverse processes, full derivation

A denoising diffusion probabilistic model (DDPM; Ho, Jain and Abbeel, 2020, building on Sohl-Dickstein et al., 2015) destroys data with a fixed Markov chain that adds a little Gaussian noise at each of T steps, then learns to undo one step at a time. Three facts make it work: the noised sample at any step has a closed form x_t = √ā_t x0 + √(1 − ā_t) ε; the reverse of one small step is Gaussian, with a posterior q(x_(t−1) | x_t, x0) that is known exactly; and the variational bound on the likelihood collapses to a weighted noise-prediction loss, which Ho et al. simplified to L_simple = ‖ε − ε_θ(x_t, t)‖². This page derives all three, from the forward marginal through the ELBO decomposition into KL terms to the ε-parameterisation and the ancestral sampler, and connects DDPM to denoising score matching. In the exercises you code the schedule, the posterior and the sampling step, then train a tiny ε-network on an 8-mode 2-D Gaussian mixture and compare it with the exact Bayes-optimal denoiser of that mixture. The network's loss came within 0.028 of the irreducible minimum (0.262 against 0.234), yet its samples were about twice as blurry (spread ratio 2.07, with 17% of samples off every mode), while the exact denoiser with 100 ancestral steps reproduced the data (spread 1.07). Using only 10 of the 100 steps blurred even the exact model (spread 1.34).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learn to clean up noise, one small step at a time

Take a photo and add a sprinkle of static, then a little more, and keep going for a thousand rounds: you end up with pure television snow. Each round is easy to describe and needs no learning. A diffusion model learns the reverse: given a slightly noisy picture, guess what it looked like one round earlier. Because each round adds very little noise, each reverse guess is a small, almost Gaussian correction, which a neural network can learn by practising on noisy copies of real data. To create something new, start from fresh snow and apply the learned clean-up a thousand times. The demo uses an 8-blob ring as the ‘data’ and the exact clean-up rule for that ring, so you can see how the number of reverse steps and the amount of noise injected per step change the samples.

Diffusion lab
Spread 0.99 (1 = data), off-mode 1.0%. 20 strided steps of the T = 1000 linear schedule, exact ε. Few steps blur the samples; σ² = β̃ is sharper than σ² = β.
Why engineers care

Diffusion models are the engine behind Stable Diffusion, Imagen, Sora-style video models, AlphaFold 3’s structure module and generative weather models such as GenCast. DDPM is the cleanest place to learn the mathematics that all of them share.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The forward process of a DDPM…
  2. 2. Sampling from a trained DDPM starts from…
  3. 3. The network in a DDPM is trained to predict…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The three closed forms

  • Forward chain (fixed): q(x_t | x_(t−1)) = N(√(1 − β_t) x_(t−1), β_t I), with a variance schedule β_1, …, β_T; Ho et al. used T = 1000 and β linear from 10⁻⁴ to 0.02.
  • Closed-form marginal: with α_t = 1 − β_t and ā_t = α_1 ⋯ α_t, q(x_t | x0) = N(√ā_t x0, (1 − ā_t) I), so x_t = √ā_t x0 + √(1 − ā_t) ε with ε ~ N(0, I). For Ho’s schedule ā_T ≈ 4.0 × 10⁻⁵: x_T is pure noise.
  • Signal-to-noise ratio: SNR(t) = ā_t / (1 − ā_t), decreasing in t; it crosses 1 at t = 260 for the linear schedule.
  • Tractable posterior: q(x_(t−1) | x_t, x0) = N(μ̃_t, β̃_t I) with μ̃_t = √ā_(t−1) β_t/(1 − ā_t) · x0 + √α_t (1 − ā_(t−1))/(1 − ā_t) · x_t and β̃_t = (1 − ā_(t−1))/(1 − ā_t) · β_t ≤ β_t.
  • Reverse model: p_θ(x_(t−1) | x_t) = N(μ_θ(x_t, t), σ_t² I) with σ_t² = β_t or β̃_t (fixed). Substituting x0 = (x_t − √(1 − ā_t) ε)/√ā_t into μ̃ gives the ε-parameterisation μ_θ = (x_t − β_t/√(1 − ā_t) · ε_θ(x_t, t)) / √α_t.
  • Training: sample x0, t uniform in 1…T, ε; minimise L_simple = ‖ε − ε_θ(√ā_t x0 + √(1 − ā_t) ε, t)‖².
  • Sampling (ancestral): x_T ~ N(0, I); for t = T…1: x_(t−1) = μ_θ(x_t, t) + σ_t z (no noise at the last step). T network evaluations.
  • Worked number: with ā = 0.64, x0 = 1 and x_t = 1, the noise that was added is ε = (1 − 0.8)/0.6 = 0.333.
📝 Quick check (Level 1)
0/3 answered
  1. 1. q(x_t | x0) has mean…
  2. 2. Compared with β_t, the posterior variance β̃_t is…
  3. 3. Ancestral sampling with T = 1000 needs how many network evaluations?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Coding the forward process and one reverse step

Exercise 1.

  • The linear β schedule and ā_t.
  • The closed-form forward sample q_sample.
  • The posterior mean and variance of q(x_(t−1) | x_t, x0) (checked against Bayes’ rule).
  • x̂0 from a noise prediction, and one ancestral reverse step.

Practice.

  • Predict ε (or v, or x0) rather than the mean directly; ε-prediction with L_simple is the robust default from Ho et al.
  • Sample t uniformly during training and embed it with sinusoidal features, as in the transformer positional encoding.
  • Keep an exponential moving average (EMA) of the weights for sampling; DDPM used decay 0.9999.
  • Clip x̂0 to the data range (for images [−1, 1]) inside the sampler.
  • In code, use Hugging Face diffusers (DDPMScheduler, UNet2DModel) or the original TensorFlow release; 1000 steps is slow, which motivates DDIM and fast solvers (h7-4, h7-5).
import torch
def ddpm_loss(model, x0, abar):                      # abar: tensor of ā_t, length T
    t = torch.randint(0, len(abar), (x0.shape[0],), device=x0.device)
    eps = torch.randn_like(x0); a = abar[t].view(-1, *[1] * (x0.dim() - 1))
    xt = a.sqrt() * x0 + (1 - a).sqrt() * eps
    return ((model(xt, t) - eps) ** 2).mean()        # L_simple
@torch.no_grad()
def ddpm_sample(model, shape, beta, abar):
    x = torch.randn(shape)
    for t in reversed(range(len(beta))):
        eps = model(x, torch.full((shape[0],), t))
        x = (x - beta[t] / (1 - abar[t]).sqrt() * eps) / (1 - beta[t]).sqrt()
        if t > 0: x = x + beta[t].sqrt() * torch.randn_like(x)
    return x
🐍 Exercise 1: schedule, posterior and the ancestral step
Write the linear schedule, the closed-form forward sample, the Gaussian posterior q(x_(t−1) | x_t, x0), the x̂0 inversion and one DDPM reverse step.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A common trick at sampling time is to…
  2. 2. The timestep is fed to the network as…
  3. 3. x̂0 is often clipped to…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Closed-form marginal. By induction: if x_(t−1) = √ā_(t−1) x0 + √(1 − ā_(t−1)) ε′, then x_t = √α_t x_(t−1) + √β_t ε″ = √ā_t x0 + noise with variance α_t (1 − ā_(t−1)) + β_t = 1 − ā_t, because a sum of independent Gaussians is Gaussian with added variances.

The posterior. By Bayes, q(x_(t−1) | x_t, x0) ∝ q(x_t | x_(t−1)) q(x_(t−1) | x0), a product of two Gaussians in x_(t−1). Its precision is α_t/β_t + 1/(1 − ā_(t−1)) = (1 − ā_t)/(β_t (1 − ā_(t−1))), so β̃_t = (1 − ā_(t−1)) β_t/(1 − ā_t); the mean is β̃_t times (√α_t x_t/β_t + √ā_(t−1) x0/(1 − ā_(t−1))), which gives μ̃_t.

The ELBO. As for any latent-variable model (h3-1), log p_θ(x0) ≥ E_q[log p_θ(x_(0:T))/q(x_(1:T) | x0)]. Writing q(x_t | x_(t−1)) = q(x_(t−1) | x_t, x0) q(x_t | x0)/q(x_(t−1) | x0) makes the sum telescope into L = KL(q(x_T | x0) ‖ p(x_T)) + Σ_(t > 1) E KL(q(x_(t−1) | x_t, x0) ‖ p_θ(x_(t−1) | x_t)) − E log p_θ(x0 | x1), a constant prior term, T − 1 KLs between Gaussians, and a decoder term.

From KL to noise prediction. With fixed σ_t², each KL is (1/2σ_t²) ‖μ̃_t − μ_θ‖² + const. In the ε-parameterisation μ̃_t − μ_θ = β_t/(√α_t √(1 − ā_t)) (ε_θ − ε), so L_(t−1) = β_t²/(2σ_t² α_t (1 − ā_t)) · ‖ε − ε_θ‖² + const. Ho et al. dropped the weight (L_simple), which down-weights easy low-noise steps and gave better samples (FID 3.17 on CIFAR-10) though a worse likelihood.

Score connection. Tweedie’s formula gives ∇ log q_t(x_t) = −E[ε | x_t]/√(1 − ā_t), so the optimal ε_θ is a scaled score and L_simple is denoising score matching (Vincent, 2011) summed over noise levels, the NCSN view of Song and Ermon (2019).

Exercise check. The Bayes-optimal ε of the mixture leaves an irreducible L_simple/2 of 0.234 (seed 0): even a perfect network cannot reach 0 because x_t does not determine ε.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The telescoping step rewrites q(x_t | x_(t−1)) using…
  2. 2. With fixed variances, each KL term is proportional to…
  3. 3. L_simple equals the ELBO terms with…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How close can a tiny ε-network get to the optimal denoiser, and what does that buy in samples?

History.

  • J. Sohl-Dickstein, E. Weiss, N. Maheswaranathan and S. Ganguli, “Deep unsupervised learning using nonequilibrium thermodynamics” (ICML 2015): the original diffusion model.
  • J. Ho, A. Jain and P. Abbeel, “Denoising diffusion probabilistic models” (NeurIPS 2020): ε-prediction, L_simple, FID 3.17 on CIFAR-10.
  • Y. Song and S. Ermon, “Generative modeling by estimating gradients of the data distribution” (NeurIPS 2019): the score-based view.
  • P. Vincent, “A connection between score matching and denoising autoencoders” (Neural Computation, 2011).

Exercise 2.

  • Data: 8 isotropic Gaussians (std 0.15) on a circle of radius 2. Because a noised Gaussian mixture is again a Gaussian mixture, the Bayes-optimal ε*(x_t, t) = E[ε | x_t] is known in closed form: this page uses it as the ‘perfect network’.
  • Schedule: T = 100, β linear from 0.001 to 0.2 (ā_T = 2.0 × 10⁻⁵, comparable to Ho’s T = 1000 schedule).
  • Forward check: run the chain step by step on 4000 points and regress x_T on x0.
  • Tiny network: ε_θ = 2-hidden-layer tanh MLP (64 units) on (x, sin/cos of 4 time frequencies); Adam, learning rate 0.005, batch 256, 1000 steps, L_simple.
  • Samples: 1000 each; spread = RMS distance to the nearest mode divided by 0.15·√2 (1 = data-like), and the share of samples more than 4 std from every mode.
measure seed 0 seed 1 seed 2
chain: x_T regression slope on x0 / variance 0.005 / 1.011 0.003 / 0.976 0.006 / 0.979
L_simple/2: first 20 steps / trained net / Bayes-optimal 0.592 / 0.262 / 0.234 0.582 / 0.256 / 0.229 0.590 / 0.268 / 0.246
spread, real data 0.979 0.973 1.011
spread (off-mode), tiny net, 100 steps 2.071 (17.0%) 2.037 (17.9%) 2.075 (18.5%)
spread (off-mode), exact ε, 100 steps, σ² = β 1.071 (0.1%) 1.095 (0.1%) 1.061 (0.1%)
spread, exact ε, 100 steps, σ² = β̃ 0.880 0.852 0.890
spread (off-mode), exact ε, 10 strided steps 1.344 (7.1%) 1.300 (7.1%) 1.328 (7.4%)
  • Loss is not sample quality: the network closed 92% of the gap between its starting loss and the optimum, but its samples were twice as spread and 17–19% landed between modes. L_simple averages over all t, and most of it comes from large-t steps where everyone is near the optimum; small-t precision, which decides sharpness, is a tiny share of the loss. A larger network trained longer is the real fix; this is a negative result for a tiny budget.
  • The sampler is right: with the exact ε, 100 ancestral steps reproduce the data (spread 1.06–1.10, 0.1% off-mode).
  • Variance choice: σ² = β̃ gave slightly too sharp samples (0.85–0.89) and σ² = β slightly too blurry; Ho et al. found the two similar at T = 1000. Improved DDPM learns an interpolation (h7-2).
  • Steps matter: taking every 10th step of the same schedule blurred even the exact model (spread 1.30–1.34, 7% off-mode). Fast samplers (h7-4, h7-5) fix this.
🐍 Exercise 2 (research): how close can a tiny ε-network get to the optimal denoiser, and what does that buy in samples?
Check the forward chain, train a tiny ε-network with L_simple on an 8-mode ring, and compare its loss and ancestral samples with the exact Bayes-optimal denoiser.

Diffusion in AI, robotics and aerospace

  • Images and video: DDPM’s training objective is still the core of latent diffusion, DiT and video models (h7-8, h7-10, h7-11).
  • Robotics: Diffusion Policy (Chi et al., 2023) generates robot action sequences with a DDPM-style sampler, handling multimodal demonstrations that regression averages away.
  • Earth observation and weather: GenCast (Price et al., Nature 2024) samples ensemble weather forecasts with a diffusion model; diffusion models also fill cloud gaps and super-resolve satellite imagery.
  • Science: RFdiffusion and AlphaFold 3 use diffusion to generate protein backbones and structures.

References

  1. J. Sohl-Dickstein, E. Weiss, N. Maheswaranathan and S. Ganguli (2015). “Deep unsupervised learning using nonequilibrium thermodynamics.” ICML.
  2. J. Ho, A. Jain and P. Abbeel (2020). “Denoising diffusion probabilistic models.” NeurIPS.
  3. Y. Song and S. Ermon (2019). “Generative modeling by estimating gradients of the data distribution.” NeurIPS.
  4. P. Vincent (2011). “A connection between score matching and denoising autoencoders.” Neural Computation 23(7).
  5. C. Chi et al. (2023). “Diffusion Policy: visuomotor policy learning via action diffusion.” RSS.