1. Home
  2. Explore
  3. Computer Science
  4. Diffusion Models Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Diffusion Models Overview

Diffusion Models and DDPM: Comprehensive Study Notes

History and Context

Diffusion models have become a powerful generative modeling technique, applied to images, videos, music, 3D models, and molecular discovery. The key breakthrough paper is "Denoising Diffusion Probabilistic Models" (DDPM) by Jonathan Ho et al., published in 2020. However, DDPM did not invent diffusion models.

  • The first diffusion model formulation appeared in "Deep Unsupervised Learning using Nonequilibrium Thermodynamics" (Jascha Sohl-Dickstein et al., 2015). At that time, GANs were state-of-the-art for image generation, and diffusion models received little attention.

  • The 2020 DDPM paper transformed diffusion models from a theoretical concept (limited to toy datasets) into a state-of-the-art technique for image generation. Its main contribution was a much simpler training objective and highlighting connections to earlier denoising models.

Core Idea of Diffusion Models

Given a complex data distribution p(x)p(x)p(x) (e.g., all natural images), the goal is to generate new samples from this distribution without an explicit formula.

Diffusion models approach this by removing Gaussian noise from images:

  1. Forward process (diffusion): Gradually corrupt an image by adding small amounts of Gaussian noise over many steps. After enough steps, the image becomes pure noise.

  2. Reverse process (denoising): Train a neural network to reverse the corruption steps — starting from random noise and progressively removing noise until a meaningful image emerges.

If the reverse model is effective, sampling from pure noise yields realistic data.


Mathematical Formulation of the Forward Process

Let x0x_0x0​ be the original clean image. The forward process adds Gaussian noise at each step.

Variance Exploding Formulation (initial naive approach)

A simple forward step:

q(xt∣xt−1)=N(xt;xt−1,βtI)q(x_t | x_{t-1}) = \mathcal{N}(x_t; x_{t-1}, \beta_t \mathbf{I})q(xt​∣xt−1​)=N(xt​;xt−1​,βt​I)

where βt\beta_tβt​ controls the noise variance. After ttt steps:

q(xt∣x0)=N(xt;x0,tβI)q(x_t | x_0) = \mathcal{N}(x_t; x_0, t\beta \mathbf{I})q(xt​∣x0​)=N(xt​;x0​,tβI)

Problem: The mean stays fixed at x0x_0x0​ and variance grows indefinitely — the process does not converge to a standard normal distribution. This is called a variance exploding diffusion.

Variance Preserving Formulation (used in DDPM)

To make the process converge to N(0,I)\mathcal{N}(0, \mathbf{I})N(0,I), two conditions are needed:

  • Mean must converge to 000

  • Variance must converge to 111

The DDPM paper modifies each forward step:

q(xt∣xt−1)=N(xt;1−βt xt−1,βtI)q(x_t | x_{t-1}) = \mathcal{N}(x_t; \sqrt{1 - \beta_t}\, x_{t-1}, \beta_t \mathbf{I})q(xt​∣xt−1​)=N(xt​;1−βt​​xt−1​,βt​I)

where βt∈(0,1)\beta_t \in (0,1)βt​∈(0,1). This choice leads to a convenient closed-form expression for any timestep ttt directly from x0x_0x0​:

Define:

  • αt=1−βt\alpha_t = 1 - \beta_tαt​=1−βt​

  • αˉt=∏s=1tαs\bar{\alpha}_t = \prod_{s=1}^t \alpha_sαˉt​=∏s=1t​αs​

Then:

q(xt∣x0)=N(xt;αˉt x0,(1−αˉt)I)q(x_t | x_0) = \mathcal{N}\left(x_t; \sqrt{\bar{\alpha}_t}\, x_0, (1 - \bar{\alpha}_t) \mathbf{I}\right)q(xt​∣x0​)=N(xt​;αˉt​​x0​,(1−αˉt​)I)

As t→∞t \to \inftyt→∞, αˉt→0\bar{\alpha}_t \to 0αˉt​→0, so the mean goes to 000 and variance goes to 111 — a variance preserving diffusion.

Key point: The forward process is fixed — no learnable parameters. It transforms any data distribution into a standard normal.

Noise Schedules

  • In the original DDPM, a linear schedule is used: βt\beta_tβt​ linearly interpolates from βstart\beta_{\text{start}}βstart​ to βend\beta_{\text{end}}βend​ over TTT steps.

  • Other schedules (e.g., cosine) are possible, as long as each βt∈(0,1)\beta_t \in (0,1)βt​∈(0,1).


Reverse Process and Training Objective

Setting Up the Reverse Process

Define the reverse process with learnable parameters θ\thetaθ (neural network weights):

pθ(xt−1∣xt)=N(xt−1;μθ(xt,t),Σθ(xt,t))p_\theta(x_{t-1} | x_t) = \mathcal{N}(x_{t-1}; \mu_\theta(x_t, t), \Sigma_\theta(x_t, t))pθ​(xt−1​∣xt​)=N(xt−1​;μθ​(xt​,t),Σθ​(xt​,t))

Typically, the variance Σθ\Sigma_\thetaΣθ​ is fixed to a constant σt2I\sigma_t^2\mathbf{I}σt2​I (not learned). The network predicts the mean μθ\mu_\thetaμθ​.

The true posterior q(xt−1∣xt,x0)q(x_{t-1} | x_t, x_0)q(xt−1​∣xt​,x0​) (conditioned on the clean image x0x_0x0​) has a closed-form Gaussian expression:

q(xt−1∣xt,x0)=N(xt−1;μ~t(xt,x0),β~tI)q(x_{t-1} | x_t, x_0) = \mathcal{N}\left(x_{t-1}; \tilde{\mu}_t(x_t, x_0), \tilde{\beta}_t \mathbf{I}\right)q(xt−1​∣xt​,x0​)=N(xt−1​;μ~​t​(xt​,x0​),β~​t​I)

where μ~t\tilde{\mu}_tμ~​t​ and β~t\tilde{\beta}_tβ~​t​ depend on xtx_txt​, x0x_0x0​, and the noise schedule.

Deriving the Training Objective

The goal is to maximize the log-likelihood log⁡pθ(x0)\log p_\theta(x_0)logpθ​(x0​). Since this is intractable (requires summing over all possible reverse paths), a variational lower bound (ELBO) is used:

Evidence Lower Bound (ELBO): An upper bound on the negative log-likelihood, equivalent to a lower bound on the likelihood itself.

Starting with:

−log⁡pθ(x0)≤Eq(x1:T∣x0)[−log⁡pθ(x0:T)q(x1:T∣x0)]-\log p_\theta(x_0) \leq \mathbb{E}_{q(x_{1:T}|x_0)}\left[ -\log \frac{p_\theta(x_{0:T})}{q(x_{1:T}|x_0)} \right]−logpθ​(x0​)≤Eq(x1:T​∣x0​)​[−logq(x1:T​∣x0​)pθ​(x0:T​)​]

After algebraic manipulation and using the Markov property, the ELBO can be expressed as a sum of KL divergences:

LELBO=Eq[DKL(q(xT∣x0)∥p(xT))+∑t=2TDKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))−log⁡pθ(x0∣x1)]\mathcal{L}_{\text{ELBO}} = \mathbb{E}_q\left[ D_{\text{KL}}(q(x_T|x_0) \| p(x_T)) + \sum_{t=2}^T D_{\text{KL}}(q(x_{t-1}|x_t,x_0) \| p_\theta(x_{t-1}|x_t)) - \log p_\theta(x_0|x_1) \right]LELBO​=Eq​[DKL​(q(xT​∣x0​)∥p(xT​))+∑t=2T​DKL​(q(xt−1​∣xt​,x0​)∥pθ​(xt−1​∣xt​))−logpθ​(x0​∣x1​)]

Where:

  • The KL divergence DKL(P∥Q)D_{\text{KL}}(P \| Q)DKL​(P∥Q) measures how much distribution QQQ diverges from PPP (zero when identical).

  • The first term involves no learnable parameters and is ignored.

  • The last term (reconstruction from x1x_1x1​ to x0x_0x0​) is negligible in practice and often dropped.

Simplifying to a Noise Prediction Objective

Since both the true posterior q(xt−1∣xt,x0)q(x_{t-1}|x_t,x_0)q(xt−1​∣xt​,x0​) and the approximate posterior pθ(xt−1∣xt)p_\theta(x_{t-1}|x_t)pθ​(xt−1​∣xt​) are Gaussian with fixed variances, the KL divergence simplifies to a squared distance between their means:

DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))∝∥μ~t(xt,x0)−μθ(xt,t)∥2D_{\text{KL}}(q(x_{t-1}|x_t,x_0) \| p_\theta(x_{t-1}|x_t)) \propto \| \tilde{\mu}_t(x_t, x_0) - \mu_\theta(x_t, t) \|^2DKL​(q(xt−1​∣xt​,x0​)∥pθ​(xt−1​∣xt​))∝∥μ~​t​(xt​,x0​)−μθ​(xt​,t)∥2

Using the reparameterization trick:

  • xt=αˉt x0+1−αˉt ϵx_t = \sqrt{\bar{\alpha}_t}\, x_0 + \sqrt{1 - \bar{\alpha}_t}\, \epsilonxt​=αˉt​​x0​+1−αˉt​​ϵ, where ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, \mathbf{I})ϵ∼N(0,I)

  • The true posterior mean μ~t\tilde{\mu}_tμ~​t​ can be written as a function of xtx_txt​ and ϵ\epsilonϵ

  • The network's predicted mean μθ\mu_\thetaμθ​ is similarly parameterized to predict ϵθ(xt,t)\epsilon_\theta(x_t, t)ϵθ​(xt​,t)

After substitution and cancellation, the loss simplifies to:

Lsimple=Ex0,t,ϵ[∥ϵ−ϵθ(xt,t)∥2]\mathcal{L}_{\text{simple}} = \mathbb{E}_{x_0, t, \epsilon}\left[ \| \epsilon - \epsilon_\theta(x_t, t) \|^2 \right]Lsimple​=Ex0​,t,ϵ​[∥ϵ−ϵθ​(xt​,t)∥2]

Final training objective: The neural network predicts the noise ϵ\epsilonϵ that was added to the clean image x0x_0x0​ to produce xtx_txt​. Minimize the mean squared error (MSE) between the true noise and the predicted noise.

  • In practice, the sum over all timesteps is replaced by sampling a random ttt for each batch element, which converges to the same objective.

  • The DDPM paper uses uniform weights (no time-dependent weighting), which often produces better results than the theoretically derived weights.


Implementation Overview

Training

Tools: PyTorch + DeepInverse (provides pre-built Diffusion UNet architecture and pretrained weights).

Steps:

  1. Load dataset (e.g., MNIST, 32×32 grayscale).

  2. Instantiate a DiffusionUNet with appropriate input/output channels.

  3. Define a noise schedule: linear interpolation from βstart\beta_{\text{start}}βstart​ to βend\beta_{\text{end}}βend​ over T=1000T=1000T=1000 timesteps.

  4. Compute αt\alpha_tαt​, αˉt\bar{\alpha}_tαˉt​, αˉt\sqrt{\bar{\alpha}_t}αˉt​​, 1−αˉt\sqrt{1-\bar{\alpha}_t}1−αˉt​​.

  5. Training loop for each batch:

    • Sample random t∼Uniform(0,T)t \sim \text{Uniform}(0, T)t∼Uniform(0,T) and noise ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, \mathbf{I})ϵ∼N(0,I).

    • Compute noisy image: xt=αˉtx0+1−αˉtϵx_t = \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1-\bar{\alpha}_t} \epsilonxt​=αˉt​​x0​+1−αˉt​​ϵ

    • Network predicts ϵθ(xt,t)\epsilon_\theta(x_t, t)ϵθ​(xt​,t).

    • Loss = MSE(ϵ\epsilonϵ, ϵθ\epsilon_\thetaϵθ​).

  6. Update network weights (e.g., Adam optimizer).

During training, the model gradually learns to denoise at all noise levels.

Inference (Sampling)

Steps:

  1. Start with pure Gaussian noise xT∼N(0,I)x_T \sim \mathcal{N}(0, \mathbf{I})xT​∼N(0,I).

  2. for t=Tt = Tt=T down to 111:

    • Predict noise ϵθ(xt,t)\epsilon_\theta(x_t, t)ϵθ​(xt​,t).

    • Compute posterior mean: μθ(xt,t)=1αt(xt−βt1−αˉtϵθ)\mu_\theta(x_t, t) = \frac{1}{\sqrt{\alpha_t}}\left( x_t - \frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \epsilon_\theta \right)μθ​(xt​,t)=αt​​1​(xt​−1−αˉt​​βt​​ϵθ​)

    • Sample xt−1∼N(μθ,σt2I)x_{t-1} \sim \mathcal{N}(\mu_\theta, \sigma_t^2 \mathbf{I})xt−1​∼N(μθ​,σt2​I), where σt2=β~t\sigma_t^2 = \tilde{\beta}_tσt2​=β~​t​ (or a fixed schedule).

  3. Output x0x_0x0​ as the final generated sample.

The sampling loop is concise (~30 lines of code), running the network TTT times (typically 1000).


Properties and Trade-offs

  • Quality: DDPM generates images of extremely high fidelity — often indistinguishable from real data.

  • Comparison to VAEs: DDPMs produce much sharper images than VAEs, but at the cost of many inference steps (1000 forward passes vs. one for a VAE). This is a compute-quality trade-off.

  • Flexibility: The same sampling code works across different datasets by swapping pretrained weights (e.g., MNIST → FFHQ portraits).


Extensions and Next Steps

  • Faster sampling was achieved shortly after DDPM by reducing the number of steps (e.g., using denoising diffusion implicit models, DDIM).

  • A more general formulation replaces discrete Markov chains with continuous-time stochastic differential equations (score-based diffusion models), which unify and extend the theory.


Summary of Mathematical Pipeline

Step

Operation

Expression

Forward process

q(xt∣xt−1)q(x_t \mid x_{t-1})q(xt​∣xt−1​)

N(1−βt xt−1, βtI)\mathcal{N}(\sqrt{1-\beta_t}\,x_{t-1},\,\beta_t I)N(1−βt​​xt−1​,βt​I)

Direct to xtx_txt​

q(xt∣x0)q(x_t \mid x_0)q(xt​∣x0​)

N(αˉt x0, (1−αˉt)I)\mathcal{N}(\sqrt{\bar{\alpha}_t}\,x_0,\,(1-\bar{\alpha}_t)I)N(αˉt​​x0​,(1−αˉt​)I)

True posterior

q(xt−1∣xt,x0)q(x_{t-1}\mid x_t,x_0)q(xt−1​∣xt​,x0​)

Gaussian with closed-form mean μ~t\tilde{\mu}_tμ~​t​

Approx. posterior

pθ(xt−1∣xt)p_\theta(x_{t-1}\mid x_t)pθ​(xt−1​∣xt​)

N(μθ(xt,t), σt2I)\mathcal{N}(\mu_\theta(x_t,t),\,\sigma_t^2I)N(μθ​(xt​,t),σt2​I)

Training loss

Lsimple\mathcal{L}_{\text{simple}}Lsimple​

E[∣ϵ−ϵθ(xt,t)∣2]\mathbb{E}[|\epsilon - \epsilon_\theta(x_t,t)|^2]E[∣ϵ−ϵθ​(xt​,t)∣2]

Sampling (one step)

xt−1x_{t-1}xt−1​ from pθp_\thetapθ​

μθ+σt⋅N(0,I)\mu_\theta + \sigma_t \cdot \mathcal{N}(0,I)μθ​+σt​⋅N(0,I)

The central insight: training a denoiser to predict the noise enables generating new data by running the reverse process from pure noise.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent