01 · The World’s Smallest Diffusion Model

The second note of the introduction, and the reason the course exists. It builds a complete diffusion model on a dataset of two points: forward noising, the optimal denoiser, the score, and reverse-time generation — every step exact, in closed form, with nothing learned and nothing approximated.

Read it as a statement of the goal. This is what we want to understand in high dimensions. Everything the course does later is an attempt to recover these same five objects when the data is images instead of two numbers, and the closed forms are gone. The final section names precisely which step breaks first.

The dataset

Our data distribution puts equal mass on two points:

\[ p_{\text{data}}(x_0) \;=\; \tfrac{1}{2}\,\delta(x_0 + 1) \;+\; \tfrac{1}{2}\,\delta(x_0 - 1), \qquad x_0 \in \{-1, +1\}. \]

Generative modeling asks: given samples from \(p_{\text{data}}\), produce more samples. For this dataset the answer is a coin flip — which is exactly why it is the right dataset: the machinery becomes transparent.

Forward process: noise melts the data

Corrupt a clean sample with Gaussian noise of size \(\sigma\):

\[ x_\sigma \;=\; x_0 + \sigma z, \qquad z \sim \mathcal{N}(0, 1) \text{ independent of } x_0. \]

Since \(x_0 = \pm 1\) with probability \(\tfrac12\) each, the density of the corrupted sample is a two-component Gaussian mixture:

\[ p_\sigma(x) \;=\; \tfrac{1}{2}\,\mathcal{N}(x;\, -1,\, \sigma^2) \;+\; \tfrac{1}{2}\,\mathcal{N}(x;\, +1,\, \sigma^2). \]

Two facts, both visible in the demo:

  • Small \(\sigma\): two sharp bumps at \(\pm 1\) — the data is intact.
  • Large \(\sigma\): the bumps merge. Indeed \(\mathbb{E}[x_\sigma] = 0\) and \(\operatorname{Var}(x_\sigma) = \mathbb{E}[x_0^2] + \sigma^2 = 1 + \sigma^2\), and for \(\sigma \gtrsim 3\) the mixture is indistinguishable in practice from \(\mathcal{N}(0,\, 1 + \sigma^2)\) — at \(\sigma = 3\) its excess kurtosis is only \(-0.02\). (It is never exactly Gaussian at finite \(\sigma\); the precise statement is \(x_\sigma / \sqrt{1+\sigma^2} \Rightarrow \mathcal{N}(0,1)\) as \(\sigma \to \infty\).)

This is the first design principle of diffusion models: destroying structure is easy, fully understood, and ends somewhere known (a Gaussian), no matter what the data was.

Denoising: Bayes’ rule gives the optimal denoiser in closed form

Now invert: we observe a noisy \(x_\sigma = x\) and want the clean \(x_0\). The best guess under squared error is the posterior mean \(\mathbb{E}[x_0 \mid x_\sigma = x]\) (we will prove this optimality claim in Module 1; for now, take it).

The posterior over the two candidates follows from Bayes’ rule:

\[ \mathbb{P}(x_0 = +1 \mid x) = \frac{\tfrac12\,\mathcal{N}(x; +1, \sigma^2)} {\tfrac12\,\mathcal{N}(x; +1, \sigma^2) + \tfrac12\,\mathcal{N}(x; -1, \sigma^2)} = \frac{1}{1 + \exp\!\big(-\tfrac{2x}{\sigma^2}\big)} = \operatorname{sigmoid}\!\Big(\frac{2x}{\sigma^2}\Big), \]

where the middle step divides through by \(\mathcal{N}(x; +1, \sigma^2)\) and uses \(\exp\!\big(-\tfrac{(x+1)^2}{2\sigma^2}\big) \big/ \exp\!\big(-\tfrac{(x-1)^2}{2\sigma^2}\big) = \exp\!\big(-\tfrac{2x}{\sigma^2}\big)\). Therefore

\[ \boxed{\;\mathbb{E}[x_0 \mid x_\sigma = x] = (+1)\,\mathbb{P}(+1 \mid x) + (-1)\,\mathbb{P}(-1 \mid x) = \tanh\!\Big(\frac{x}{\sigma^2}\Big).\;} \]

The optimal denoiser is a tanh — an activation function you already know, produced by nothing but probability. Sanity-check its two regimes:

  • \(\sigma \to 0\): \(\tanh(x/\sigma^2)\) becomes a hard step — snap to the nearer data point.
  • \(\sigma \to \infty\): \(\tanh(x/\sigma^2) \approx x/\sigma^2 \to 0\) — the observation is all noise, so the best guess is the prior mean \(0\).

The score function (a preview of Module 4)

Differentiate \(\log p_\sigma\) directly. With \(\varphi_\pm(x) = \mathcal{N}(x; \pm 1, \sigma^2)\) and \(\varphi_\pm'(x) = -\tfrac{x \mp 1}{\sigma^2}\varphi_\pm(x)\),

\[ \nabla_x \log p_\sigma(x) = \frac{\tfrac12\varphi_-'(x) + \tfrac12\varphi_+'(x)}{p_\sigma(x)} = -\frac{x - \mathbb{E}[x_0 \mid x]}{\sigma^2} = \frac{\tanh\!\big(\tfrac{x}{\sigma^2}\big) - x}{\sigma^2}. \]

Rearranged, this is Tweedie’s formula, one of the two or three most important identities of the course:

\[ \mathbb{E}[x_0 \mid x_\sigma = x] \;=\; x + \sigma^2\, \nabla_x \log p_\sigma(x). \]

Denoising and knowing the score are the same thing — for additive Gaussian noise of known size \(\sigma\), which is exactly the diffusion setting. The score \(\nabla_x \log p_\sigma\) points from \(x\) toward where the (noised) data is denser; the denoiser just moves by \(\sigma^2\) times the score. One caveat: the score is a local quantity — at \(x = 0\) it vanishes by symmetry even though, in the two-bump regime, \(x = 0\) is a density minimum. It is an unstable equilibrium: the reverse dynamics leave a particle sitting exactly there alone, and push a particle on either side away toward the nearer mode.

Reverse process: run the diffusion backwards

Generation now works by annealing: start from pure noise at a large \(\sigma_{\max}\), and lower \(\sigma\) along a grid \(\sigma_{\max} = \sigma_0 > \sigma_1 > \cdots > \sigma_K \approx 0\), nudging the sample along the score at every step. (We write \(y_k\) for the reverse iterate at noise level \(\sigma_k\), keeping \(x_0\) reserved for the clean data point; \(y_0\) is pure noise and \(y_K\) is the generated sample.)

\[ y_{k+1} \;=\; y_k \;+\; \frac{\sigma_k^2 - \sigma_{k+1}^2}{2}\, \nabla_y \log p_{\sigma_k}(y_k) \;=\; y_k + \frac{\sigma_k^2 - \sigma_{k+1}^2}{2}\cdot \frac{\tanh\!\big(y_k/\sigma_k^2\big) - y_k}{\sigma_k^2}. \]

This deterministic rule is the explicit Euler discretization of the probability-flow ODE in the variance clock \(\tau = \sigma^2\). (Strictly, to speak of dynamics across noise levels we couple them: \(x_\tau = x_0 + w_\tau\) with \(w\) a Brownian motion — Module 5 makes this precise; nothing below needs it yet.) The stochastic variant both doubles the score drift and injects fresh independent noise at each step,

\[ y_{k+1} = y_k + \big(\sigma_k^2 - \sigma_{k+1}^2\big)\,\nabla_y \log p_{\sigma_k}(y_k) + \sqrt{\sigma_k^2 - \sigma_{k+1}^2}\; z_k, \qquad z_k \sim \mathcal{N}(0,1) \text{ i.i.d.}, \]

giving the Euler–Maruyama discretization of the reverse-time SDE. Where these updates come from is the business of Modules 5–6 (Fokker–Planck, reverse-time dynamics); today we only need to watch them work: a cloud of particles started from \(\mathcal{N}(0,\, 1+\sigma_{\max}^2)\) splits, condenses, and lands within \(\approx \sigma_{\min}\) of \(\{-1, +1\}\), in equal proportions up to sampling error.

To be honest about the word “exact”: the formulas are exact, but the sampler carries four small approximations — we initialize from the moment-matched Gaussian rather than the true mixture \(p_{\sigma_{\max}}\) (indistinguishable at \(\sigma_{\max} = 3\), but not equal); we stop at \(\sigma_{\min} = 10^{-2} > 0\), so samples end a hair’s width from the atoms; the Euler steps discretize the flow; and any finite run has sampling noise. Each vanishes in the appropriate limit — and every one of them has a counterpart in real diffusion models.

The full sampler, with no neural network anywhere:

import numpy as np

def score(y, s2):                 # ∇ log p_σ(y), exact for the two-point data
    return (np.tanh(y / s2) - y) / s2

smax, smin, K, N = 3.0, 1e-2, 500, 100_000
sig = smax * (smin / smax) ** (np.arange(K + 1) / K)    # geometric σ grid
y = np.sqrt(1 + smax**2) * np.random.randn(N)           # start from pure noise

for k in range(K):                                       # probability-flow ODE
    d = sig[k]**2 - sig[k + 1]**2
    y += 0.5 * d * score(y, sig[k]**2)

err = np.abs(np.abs(y) - 1).max()      # worst distance to the nearest data point
print(err, (y > 0).mean())   # ≈ 0 and ≈ 0.5, up to σ_min + discretization + sampling

Why the course exists: what breaks for images

Everything above was exact because the posterior mean had a formula. For images there is no usable one. (Exercise 4 gives a closed form for any finite training set — but a sampler built on it can only ever reproduce the training points; what we actually want is the posterior mean under the unknown population distribution.) Nor can it be tabulated: a \(64 \times 64\) RGB image lives in a space of \(256^{12288}\) configurations, and the data occupies a vanishingly thin filament of it (Module 1: the curse of dimensionality). The entire role of the neural network in a diffusion model is to approximate the map \((x, \sigma) \mapsto \mathbb{E}[x_0 \mid x]\) — to be the tanh we can no longer derive. Every other ingredient of this note survives unchanged, and each becomes a module of the course:

In this note In the course
The “produce more samples” problem; why direct density estimation fails M1 — Foundations of generative modeling
Gaussian mixture, conditioning, \(\operatorname{Var} = 1+\sigma^2\) M2 — Gaussians and noise accumulation
The discrete \(\sigma\) grid; denoising as the training target M3 — DDPM framework
\(\nabla_x \log p_\sigma\), Tweedie’s formula M4 — Score matching and denoising
The \(\sigma\)-continuum, densities evolving with \(\sigma\) M5 — SDEs and Fokker–Planck
The reverse update and the probability-flow ODE M6 — Reverse-time dynamics

Exercises

  1. Fill in the algebra for \(\mathbb{P}(x_0 = +1 \mid x) = \operatorname{sigmoid}(2x/\sigma^2)\) and \(\mathbb{E}[x_0 \mid x] = \tanh(x/\sigma^2)\).
  2. Show \(\operatorname{Var}(x_\sigma) = 1 + \sigma^2\), and more generally \(\operatorname{Var}(x_\sigma) = \operatorname{Var}(x_0) + \sigma^2\) for any data distribution with finite variance (independence of \(z\) and \(x_0\) is all you need — zero mean is not required).
  3. Unequal weights. Let \(\mathbb{P}(x_0 = +1) = \pi\) with \(0 < \pi < 1\). Show \(\mathbb{E}[x_0 \mid x] = \tanh\!\big(\tfrac{x}{\sigma^2} + \tfrac12 \log\tfrac{\pi}{1-\pi}\big)\) — the denoiser acquires a bias. Check that as \(\sigma \to \infty\) this tends to \(2\pi - 1\), the prior mean, and work out what happens in the degenerate cases \(\pi \in \{0, 1\}\) by taking limits.
  4. More points. For data uniform on \(\{a_1, \dots, a_m\}\), show \(\mathbb{E}[x_0 \mid x] = \sum_i a_i\, w_i(x)\) with \(w_i(x) \propto \exp\!\big(\tfrac{a_i x}{\sigma^2} - \tfrac{a_i^2}{2\sigma^2}\big) \propto \exp\!\big(-\tfrac{(x - a_i)^2}{2\sigma^2}\big)\) — a softmax with Gaussian-kernel weights. (So the optimal denoiser for a finite dataset under Gaussian corruption is attention-like: an RBF-weighted average of the training points. For non-uniform weights \(\pi_i\), each logit gains \(\log \pi_i\).)
  5. Run the sampler above with the SDE update instead of the ODE. Compare the RMS distance of the final samples to the nearest data point for both, as a function of \(K\) and of \(\sigma_{\min}\). (You should find that increasing \(K\) removes discretization error but both samplers retain a terminal width \(\approx \sigma_{\min}\) — lowering \(\sigma_{\min}\), not raising \(K\), is what sharpens the atoms.)