Turning noise into data: a diffusion model from scratch
Every model in the sprint so far has been a transformer — the architecture behind text. But the models
that generate images (Stable Diffusion, DALL·E, Midjourney) are a completely different beast: diffusion
models. So I built one from scratch, on a 2D toy so I could actually watch it, in pure numpy, with no
training at all. Watching a cloud of random noise organise itself into structure is the most visual
thing I’ve done all sprint.
What I built
Diffusion is two mirror-image processes. Forward: take data and gradually stir in Gaussian noise
until it’s pure static. Reverse: start from static and, step by step, nudge each point toward higher
data-likelihood — along the score, ∇ log p(x) — until it lands back on the data. The target was 8
Gaussians on a ring. Starting from 3,000 points of pure noise, the reverse process reconstructs it:
generated from pure noise:
.. ..
#% #%
#: -*
#@ %#
%#
99.9% of the generated points land on a ring centre; all 8 modes covered. Nothing was trained — for a
known distribution the score is closed-form, so I could implement the exact mechanism and see it run.
The concepts that made it work
- Generation is iterative denoising. No tokens, no autoregression, no attention. The whole sample
is refined at once, over many steps. After six weeks of transformers, it was clarifying to build a
genuinely different machine — “AI” is several distinct architectures, not one. - The score is the entire game. “Which direction is data from here?” Everything else — the noise
schedule, the update rule — is bookkeeping around that one question. For my Gaussian mixture the answer
is a softmax over “which mode is closest,” weighting a pull toward each. - Forward is free; reverse is guided. Adding noise needs no model. Removing it is where the
intelligence lives — and for real images, that’s the one piece you’d learn (with a U-Net). The
scaffolding around it is exactly what I wrote here.
The thing that bit me
My first noise schedule looked reasonable and even generated a decent ring — but a test failed, and it
was right to. The schedule only noised the data down to about half signal; the forward process never
actually reached pure N(0, I). Which matters, because the reverse chain starts from N(0, I) — so I
was sampling from a prior the forward process never produced. A quiet train/sample mismatch that a pretty
output completely hid. The forward-noising test caught it (final standard deviation ≈ 2.2 instead of ~1),
and steepening the schedule fixed both the test and the inconsistency. Same lesson as the whole sprint: a
correctness check sees what your eyes forgive.
Tradeoffs & the road not taken
- Analytic score, not a learned one. I used the closed-form score of a known distribution to see the
mechanism without training. Swapping in a tiny trained MLP denoiser is the obvious next step — and the
moment diffusion stops being free. - 2D toy, not images. A ring is legible in a terminal; MNIST would need a real U-Net and real
training. The math is identical; only the denoiser grows. - Plain DDPM sampling. Deterministic DDIM and few-step samplers are the speed story, left for later.
Where this sits in the system
This is the generative-vision half of the extension — the other great architecture family, built to the
same standard as the rest: from scratch, correctness-checked, explained with its own real output. Next I
join the two worlds — a multimodal (CLIP-style) embedding that puts text and images in one space — and
then a capstone that wires the text stack into one small service. Same spine, new territory: one system,
a layer at a time.
Part of a series building a modern AI stack from scratch, told visually. (Earlier: quantization from scratch.)