Equations · Ho, Jain and Abbeel, “Denoising Diffusion Probabilistic Models” (2020), building on Sohl-Dickstein et al. (2015)

The diffusion forward (noising) process, explained

A diffusion model is trained to undo a fixed noising process. This is that process: over TT steps (1000 in DDPM), a data point is shrunk slightly and has a little Gaussian noise added, until it is pure noise. The second formula jumps straight to any step tt in one draw.

q(xt∣xt−1)=N(xt;1−βt xt−1,βtI),q(xt∣x0)=N(xt;αˉt x0,(1−αˉt)I),αˉt=∏s=1t(1−βs)q(\mathbf{x}_t \mid \mathbf{x}_{t-1}) = \mathcal{N}\left(\mathbf{x}_t; \sqrt{1 - \beta_t}\,\mathbf{x}_{t-1}, \beta_t \mathbf{I}\right), \quad q(\mathbf{x}_t \mid \mathbf{x}_0) = \mathcal{N}\left(\mathbf{x}_t; \sqrt{\bar{\alpha}_t}\,\mathbf{x}_0, (1 - \bar{\alpha}_t)\mathbf{I}\right), \quad \bar{\alpha}_t = \prod_{s=1}^{t} (1 - \beta_s)

Open in the decoder →

Term by term

  1. x0, xt\mathbf{x}_0, \ \mathbf{x}_t

    The clean data (say, an image) and its noised version after tt steps. The subscript is the step number.

  2. q(xt∣xt−1)q(\mathbf{x}_t \mid \mathbf{x}_{t-1})

    The distribution of the next noisier version given the current one. qq names the fixed forward process; pθp_\theta is used for the learned reverse one.

  3. N(xt;μ,Σ)\mathcal{N}(\mathbf{x}_t; \boldsymbol{\mu}, \Sigma)

    A Gaussian density in xt\mathbf{x}_t, with the mean and covariance after the semicolon.

  4. βt\beta_t

    The noise variance added at step tt: the noise schedule. DDPM raises it linearly from 10−410^{-4} to 0.020.02.

  5. 1−βt xt−1\sqrt{1 - \beta_t}\,\mathbf{x}_{t-1}

    The previous step, shrunk slightly, so the total variance stays near 1 instead of growing.

  6. βtI\beta_t \mathbf{I}

    Covariance βt\beta_t times the identity: independent noise of the same size in every pixel.

  7. αˉt=∏s=1t(1−βs)\bar{\alpha}_t = \prod_{s=1}^{t} (1 - \beta_s)

    The product of all the shrink factors so far (the paper writes αt=1−βt\alpha_t = 1 - \beta_t). It falls from near 1 to near 0 as tt goes to TT: the fraction of signal variance left.

A small worked example

If αˉt=0.25\bar{\alpha}_t = 0.25, then xt=0.5 x0+0.75 ϵ≈0.5 x0+0.866 ϵ\mathbf{x}_t = 0.5\,\mathbf{x}_0 + \sqrt{0.75}\,\boldsymbol{\epsilon} \approx 0.5\,\mathbf{x}_0 + 0.866\,\boldsymbol{\epsilon} with ϵ∼N(0,I)\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}). A pixel with value 11 and drawn noise −0.4-0.4 becomes 0.5−0.346≈0.1540.5 - 0.346 \approx 0.154.

In code

import numpy as np

betas = np.linspace(1e-4, 0.02, 1000)       # DDPM's linear schedule
alpha_bar = np.cumprod(1 - betas)

def q_sample(x0, t, rng):
    eps = rng.standard_normal(x0.shape)
    return np.sqrt(alpha_bar[t]) * x0 + np.sqrt(1 - alpha_bar[t]) * eps, eps

Every symbol in it

x∈Rd\mathbf{x} \in \mathbb{R}^{d}Bold lowercase (vector)xi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)p(x),  q(z)p(\mathbf{x}), \; q(\mathbf{z})Probability p(x)p(y∣x)p(y \mid x)Given (conditional bar)N(μ,σ2)\mathcal{N}(\mu, \sigma^2)Normal distributionp(x;θ)p(x; \theta)Semicolon (parameterized by)β1,β2\beta_1, \beta_2Betadk\sqrt{d_k}Square rootIn\mathbf{I}_nIdentity matrixxˉ\bar{x}Bar (mean)α\alphaAlpha∏i=1Np(xi)\prod_{i=1}^{N} p(x_i)Product

Other equations