The World’s Smallest Diffusion Model

W01 · Part 3 — two data points, one line of Bayes, a complete generative model

Uma Satyaranjan · Soma S Dhavala

Invalid Date

The pitch

A diffusion model turns pure static into an image. It does exactly one thing, about a thousand times:

subtract a little noise, correctly.

Everything in this deck is computed exactly — no neural network anywhere. The demos are on the course site.

Our dataset: two points

Samples from an unknown distribution; task: produce more of the same. Today:

pdata=12δ−1+12δ+1p_{\text{data}} \;=\; \tfrac12 \delta_{-1} \;+\; \tfrac12 \delta_{+1}

  • Generating = flipping a coin — that is why it is the right dataset: nothing can hide
  • You already know one generative model: fit 𝒩(μ,σ2)\mathcal{N}(\mu,\sigma^2) by MLE, then sample
  • Module 1: MLE == minimizing KL(pdata∥pθ)\mathrm{KL}(p_{\text{data}} \,\|\, p_\theta)

Why images break the direct approach

  • A 64×6464\times64 RGB image lives in a space with 25612288256^{12288} configurations
  • Almost all of them are static — random pixels never make a face
  • Real data is a vanishingly thin filament of the space

Writing down pθ(image)p_\theta(\text{image}) and fitting it directly is hopeless — the curse of dimensionality (Module 1).

The diffusion idea

xσ=x0+σz,z∼𝒩(0,1)independent of x0x_\sigma \;=\; x_0 + \sigma z, \qquad z \sim \mathcal{N}(0,1) \ \text{independent of } x_0

  • Destroying structure with noise: easy, fully understood
  • It always ends somewhere known — a Gaussian
  • Then learn to walk backwards

Watch noise accumulate

One point at 00, kicked kk times by noise of size δ\delta: spread =δk=\delta\sqrt{k}. Any kick distribution → same Gaussian (CLT).

Forward: noise melts the two points

pσ(x)=12𝒩(x;−1,σ2)+12𝒩(x;+1,σ2),Var(xσ)=1+σ2p_\sigma(x) = \tfrac12 \mathcal{N}(x; -1, \sigma^2) + \tfrac12 \mathcal{N}(x; +1, \sigma^2), \qquad \operatorname{Var}(x_\sigma) = 1 + \sigma^2

  • Two spikes → two bumps → one bell
  • By σ≈3\sigma \approx 3, indistinguishable in practice from 𝒩(0,1+σ2)\mathcal{N}(0, 1+\sigma^2)

Structure destroyed. Destination known.

The inverse problem

You observe a noisy xx. What was the clean point? Best guess under squared error is the posterior mean 𝔼[x0∣xσ=x]\mathbb{E}[x_0 \mid x_\sigma = x].

For the two-point prior, Bayes’ rule gives it in closed form:

ℙ(x0=+1∣x)=sigmoid(2xσ2)\mathbb{P}(x_0 = +1 \mid x) \;=\; \operatorname{sigmoid}\!\left(\frac{2x}{\sigma^2}\right)

One line of algebra — Exercise 1, do it tonight.

The punchline

𝔼[x0∣xσ=x]=tanh(xσ2)\mathbb{E}[x_0 \mid x_\sigma = x] \;=\; \tanh\!\left(\frac{x}{\sigma^2}\right)

  • The optimal denoiser is a tanh — produced by nothing but probability
  • σ→0\sigma \to 0: hard threshold — snap to the nearer point
  • σ→∞\sigma \to \infty: shrink to 00 — trust the prior, not the observation

Denoising = knowing the score

Differentiate logpσ\log p_\sigma and compare — Tweedie’s formula:

𝔼[x0∣x]=x+σ2∇xlogpσ(x)\mathbb{E}[x_0 \mid x] \;=\; x + \sigma^2\, \nabla_x \log p_\sigma(x)

  • The score ∇xlogpσ\nabla_x \log p_\sigma points toward where noised data is denser
  • Denoising and score estimation are the same problem — the hinge of Modules 3–4

Run the diffusion backwards

Anneal σ\sigma downward on a grid, nudging along the score:

y←y+σk2−σk+122⋅tanh(y/σk2)−yσk2y \;\leftarrow\; y + \frac{\sigma_k^2 - \sigma_{k+1}^2}{2}\cdot\frac{\tanh(y/\sigma_k^2) - y}{\sigma_k^2}

What you just saw

  • Particles start as pure noise
  • The cloud splits, condenses, lands on the two data points
  • Generation — with every step computed exactly, no neural network

The whole pipeline: noise the data → learn to denoise → run it backwards.

So where do neural networks come in?

For images, 𝔼[x0∣x]\mathbb{E}[x_0 \mid x] has no usable formula. The network’s only job:

approximate(x,σ)↦𝔼[x0∣x]\text{approximate}\quad (x,\sigma) \;\mapsto\; \mathbb{E}[x_0 \mid x]

— to be the tanh we can no longer derive. Everything else survives unchanged.

The course, mapped onto today

Today Module
“Produce more samples”; why direct density estimation fails M1 Foundations
Mixture, conditioning, Var=1+σ2\operatorname{Var} = 1+\sigma^2 M2 Gaussians & noise
The σ\sigma grid; denoising as the training target M3 DDPM
Score, Tweedie’s formula M4 Score matching
Noise levels as a continuum; densities evolving M5 SDEs, Fokker–Planck
The reverse update; probability-flow ODE M6 Reverse-time dynamics

Tonight: read the companion note, play with the demos, and derive the tanh yourself.

Next lecture — Module 1: why maximum likelihood is KL divergence in disguise.