Skip to content
ml-recall

Topic notes

Diffusion Models

Diffusion models add Gaussian noise in a fixed forward process and learn to undo it step by step; DDPM training and sampling, the score/SDE view, DDIM, classifier-free guidance and latent diffusion.

3-min read AI-checked

Practise 30 questions Take a test

Key ideas

Formulas

q(xt∣x0)=N(αˉt x0, (1−αˉt)I),xt=αˉt x0+1−αˉt ϵq(x_t \mid x_0) = \mathcal{N}\big(\sqrt{\bar\alpha_t}\,x_0,\ (1 - \bar\alpha_t) I\big), \qquad x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1 - \bar\alpha_t}\,\epsilon Lsimple(θ)=Et,x0,ϵ∥ϵ−ϵθ(αˉt x0+1−αˉt ϵ, t)∥2L_{\text{simple}}(\theta) = \mathbb{E}_{t, x_0, \epsilon}\big\lVert \epsilon - \epsilon_\theta(\sqrt{\bar\alpha_t}\,x_0 + \sqrt{1 - \bar\alpha_t}\,\epsilon,\ t) \big\rVert^2 xt−1=1αt(xt−βt1−αˉt ϵθ(xt,t))+σtzx_{t-1} = \frac{1}{\sqrt{\alpha_t}}\Big(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\,\epsilon_\theta(x_t, t)\Big) + \sigma_t z

Worked example

Schedule β=(0.1,0.1,0.2)\beta = (0.1, 0.1, 0.2), so αˉ=(0.9,0.81,0.648)\bar\alpha = (0.9, 0.81, 0.648). Take x0=1x_0 = 1 and noise ϵ=0.5\epsilon = 0.5:

In code

import torch
T = 1000
betas = torch.linspace(1e-4, 0.02, T)               # DDPM linear schedule
alphas_bar = torch.cumprod(1 - betas, dim=0)         # index i holds alpha_bar_{i+1}
print(round(alphas_bar[-1].sqrt().item(), 4))        # 0.0064
x0 = torch.randn(8, 3, 32, 32)                       # a batch of "images"
t = torch.randint(0, T, (8,))                        # one random step per example
eps = torch.randn_like(x0)
ab = alphas_bar[t].view(-1, 1, 1, 1)                 # broadcast over C, H, W
xt = ab.sqrt() * x0 + (1 - ab).sqrt() * eps          # jump straight to x_t
eps_model = lambda x, t: torch.zeros_like(x)         # stand-in for the U-Net
loss = ((eps - eps_model(xt, t)) ** 2).mean()        # L_simple
print(xt.shape, round(loss.item(), 1))               # torch.Size([8, 3, 32, 32]) 1.0

Common pitfalls

References

  1. Ho, Jain & Abbeel (2020), Denoising Diffusion Probabilistic Models, arXiv:2006.11239 — Sec. 2 Background (Eqs. 2, 4, 6–7); Sec. 3.1 Forward process and L_T; Sec. 3.2 (Eq. 11, Algorithms 1–2); Sec. 3.4 Simplified training objective; Sec. 4 Experiments
  2. Nichol & Dhariwal (2021), Improved Denoising Diffusion Probabilistic Models, arXiv:2102.09672 — Sec. 3.2 Improving the Noise Schedule
  3. Song et al. (2021), Score-Based Generative Modeling through Stochastic Differential Equations, arXiv:2011.13456 — Sec. 3.2 Generating samples by reversing the SDE; Sec. 3.3 Estimating scores for the SDE; Sec. 4.3 Probability flow and connection to neural ODEs
  4. Song, Meng & Ermon (2021), Denoising Diffusion Implicit Models, arXiv:2010.02502 — Sec. 3 Variational inference for non-Markovian forward processes; Sec. 4 Sampling from Generalized Generative Processes (4.1–4.2); Sec. 5 Experiments (Eq. 16, η)
  5. Ho & Salimans (2022), Classifier-Free Diffusion Guidance, arXiv:2207.12598 — Sec. 3.2 Classifier-free guidance (Eq. 6, Algorithm 1)
  6. Rombach et al. (2022), High-Resolution Image Synthesis with Latent Diffusion Models, arXiv:2112.10752 — Sec. 1 Introduction (contributions); Sec. 3.1 Perceptual Image Compression; Sec. 3.2 Latent Diffusion Models; Sec. 3.3 Conditioning Mechanisms
  7. Wikipedia: Diffusion model — Score-based generative model › Their equivalence