Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

LABS · 08  /  DIFFUSION

Out of the Static.

A diffusion model doesn't paint — it un-noises. Start with a screen of pure random pixels and, step by step, wipe away a little of the noise. Do it enough times and a picture climbs out of the static. Watch a shape appear from nothing.

THE GIST · 20 SECONDS

Training teaches a model to reverse a noising process: add a little Gaussian noise to real images over and over until they're pure static, and learn to undo one step. To generate, start from noise and run it backward — each step the model predicts the noise and removes a bit, revealing a cleaner image. After all steps, a picture emerges. That's DDPM.

  • Forwardadd noise (fixed)
  • Reverseremove noise (learned)
  • Schedulenoise: high → 0
  • Samplenoise → image

Hit Step to remove one round of noise — or Play and watch the picture climb out of the static.

UNDER THE HOOD

What you just played, written down

Corruption is easy and fixed; restoration is hard and learned. Diffusion trains a network to reverse noise one small step at a time — then runs that reversal from pure static to make something new.

How diffusion works — four moves

  1. Forward (training only). Take a real image and add a little Gaussian noise, over and over, until it's pure static.
  2. Learn to undo one step. Train a network to look at a noisy image and its timestep and predict the noise that was added.
  3. Start from noise. To generate, begin with a fresh screen of random Gaussian noise — no image at all.
  4. Denoise to a sample. Repeatedly predict and remove a bit of noise; after all timesteps, a coherent image emerges.
WHY NOISE, OF ALL THINGS?

Turning an image into noise is trivial and needs no learning — so it's a free source of training pairs. All the intelligence goes into the reverse step, and running that reverse process from scratch is how new images are born.

The reverse step in code

x = torch.randn(shape)              # start from pure noise
for t in reversed(range(T)):
    eps = model(x, t)               # predict the noise
    x = remove_noise(x, eps, t)     # take one step cleaner
    if t > 0:
        x = x + sigma(t) * torch.randn_like(x)
return x                            # a fresh sample
TRAININGpredict εthe noise added
SAMPLINGT stepsnoise → image
WHY IT'S SLOW — AND THE FIX

One network call per step, dozens to hundreds of steps per image. DDIM samplers cut the steps; latent diffusion (Stable Diffusion) runs the whole process in a compressed space to make it affordable.

⚠ This is a simplification

Here the noise simply fades toward a fixed target so you can see the emergence. A real model doesn't know the target — it learns to predict noise, and each denoise is a genuine neural-network step, not a scripted fade.

↔ How you steer it

Add a text prompt and the denoiser is conditioned on it (via cross-attention), so the image that emerges matches the words. That's text-to-image generation.

★ Where it's used

Stable Diffusion, DALL·E, Midjourney, and modern video and audio generators — diffusion is the dominant way to generate high-fidelity media.

STEP BY STEP

Diffusion, step by step

The exact forward formula from the DDPM paper (Ho et al., 2020), the numbers it gives at six timesteps, one pixel worked by hand, and the knobs that decide speed and fidelity.

The formulas

  1. Schedule. Pick noise amounts βt for t = 1 to T. DDPM uses T = 1000, rising linearly from 0.0001 to 0.02.
  2. Jump to any step. With ᾱt the running product of (1 − β), a noisy image is xt = √ᾱ·x₀ + √(1 − ᾱ)·ε, where ε is fresh Gaussian noise. No loop is needed in training.
  3. Train. Pick a random t, build xt, and minimize ||ε − εθ(xt, t)||².
  4. Sample. Each reverse step uses the predicted noise: xt−1 = (xt − βt/√(1 − ᾱt)·εθ) / √(1 − βt), plus σt·z noise with σt = √βt for every step except the last.
# forward, any step t
x_t = sqrt(abar_t) * x0
    + sqrt(1 - abar_t) * eps
# training loss
loss = mse(eps, model(x_t, t))

Worked example: the schedule and one pixel

  1. t = 1: ᾱ = 0.9999; signal 0.9999, noise 0.010. The image is untouched.
  2. t = 100: ᾱ = 0.897; signal 0.947, noise 0.321. t = 250: ᾱ = 0.524; signal 0.724, noise 0.690.
  3. t = 500: ᾱ = 0.0786; signal 0.280, noise 0.960. t = 750: ᾱ = 0.0034; signal 0.058, noise 0.998.
  4. t = 1000: ᾱ = 0.00004; signal 0.006. The image is gone, which is why sampling can start from pure noise.
  5. One pixel at t = 500. Take x₀ = 0.8 and noise ε = −0.5: xt = 0.280 × 0.8 + 0.960 × (−0.5) = −0.256.
  6. Reverse it. A perfect noise prediction (−0.5) gives a mean of (−0.256 − 0.0100/0.960 × (−0.5)) / √0.9900 = −0.252, with σ = 0.100 of fresh noise added. A step moves the pixel only slightly, hence the many steps.

⚠ Steps cost compute

Sampling is 1,000 network calls with the original recipe. DDIM sampling and distilled models bring that down to roughly 10 to 50 calls, trading a little fidelity for a large speedup.

↔ Guidance and schedules

Classifier-free guidance runs the model with and without the prompt and extrapolates: ε = εu + w(εc − εu). With εu = 0.2, εc = 0.9 and w = 7.5 the guided value is 5.45. Higher w follows the prompt harder but loses variety. A cosine schedule destroys signal more gently than the linear one.

★ In production

Latent diffusion denoises a compressed code, not pixels: a 512×512×3 image (786,432 values) becomes a 64×64×4 latent (16,384 values), 48 times smaller, and a decoder turns the result back into an image.

THE PICTURE

The picture: one fixed road in, one learned road out

forward: add Gaussian noise (fixed formula, training only) → x₀signal 1.000noise 0.000 t = 250signal 0.724noise 0.690 t = 500signal 0.280noise 0.960 t = 750signal 0.058noise 0.998 t = 1000signal 0.006noise 1.000 ← reverse: the network predicts ε and removes a little (learned) training steppick a random t, draw εx_t = √ᾱ·x₀ + √(1−ᾱ)·εloss = ‖ε − ε_θ(x_t, t)‖² one pixel at t = 500x₀ = 0.8, ε = −0.50.280·0.8 + 0.960·(−0.5) = −0.256one reverse step: −0.252
The thin forward arrows are a fixed formula that can jump straight to any t, so training never walks the chain. Only the accent arrows are learned, and each one costs a network call. The signal weight is already 0.280 at t = 500 and 0.006 at t = 1000, which is why sampling may start from pure noise. The pixel shows how small one reverse step is: it moves 0.004. Read the boxes as weights, not pictures: √ᾱ is how much of the original survives and √(1−ᾱ) how much noise is mixed in, and their squares always sum to 1. The lab’s ten steps compress this road; the original recipe walks it in 1,000.
WATCH FOR

Common mistakes with diffusion

Most diffusion bugs do not crash. The sampler runs, an image comes out, and it is only blurrier, flatter or noisier than it should be, which is hard to judge by eye. When you change any of the pieces below, compare against a reference sampler on a fixed seed.

  • Thinking the network outputs the clean image

    A DDPM network outputs its guess of the noise, ε_θ. The clean-image estimate is derived from it: x̂₀ = (x_t − √(1−ᾱ)·ε_θ) / √ᾱ. With the page’s pixel, (−0.256 + 0.960 × 0.5) / 0.280 gives back 0.8. Some models are trained to predict x₀, or a blend called v, instead. Those are re-weightings of the same task, but a sampler must use the one its checkpoint was trained with, or every step subtracts the wrong quantity. A quick check: decode x̂₀ at a middle step and look at it. If it is noise rather than a blurry draft of the picture, the parameterisation is wrong.

  • Removing all the predicted noise in one jump

    At t = 1000 the signal weight is 0.006, so the network cannot know which image is under the static. Its best single guess at x₀ is an average of every image that could be there: a blur. Small steps let it commit to coarse layout first and fine detail later, each step re-reading an image that is a little less ambiguous. That is why one reverse step moves the example pixel by only 0.004, and why images form coarse to fine: early steps settle composition and colour, late steps sharpen edges and texture.

  • Dropping the fresh noise from the DDPM step

    The reverse step adds σ_t·z of new noise on every step except the last. It looks like undoing your own work, but the DDPM update assumes it. Removing it without changing the update makes samples smoother and flatter than they should be. If you want a deterministic sampler, switch to DDIM, whose update is derived to run without that noise. The added noise is also why two DDPM runs from the same starting static end as different images unless the sampler’s random seed is fixed too.

  • Feeding the wrong timestep to a fast sampler

    The network is conditioned on t, the noise level it was trained on. A 50-step sampler on a 1,000-step model must pass the original timesteps it visits, spaced about 20 apart (980, 960, …), not the loop index 49, 48, …. Pass the index and the model believes the image is nearly clean when it is mostly noise, so it removes far too little. The same mismatch appears when a model trained on one schedule is sampled with another: ᾱ at a given t differs, so the model misjudges the noise level at every step.

  • Turning guidance up to force the prompt

    Guidance extrapolates: ε_u + w(ε_c − ε_u). With the page’s values, w = 7.5 gives 5.45 and w = 15 gives 10.7, pushing each step well outside the range the network was trained on. Past a point that shows up as burned colours, harsh contrast and near-identical images. Guidance also needs the model run with and without the prompt, so each step costs two evaluations. The page’s 7.5 is a common default; tune from there on your own prompts rather than assuming more is better.

QUICK CHECK

Did it stick?

See every answer, with the reasoning
  1. How does a diffusion model generate an image?

    Answer: It starts from pure noise and removes a little noise step by step until an image appears — Generation runs the reverse process: begin with random Gaussian noise and repeatedly predict and remove a bit of noise. After all timesteps, a coherent image emerges — unlike a GAN, which generates in one pass.

  2. What does a DDPM network actually learn to predict at each step?

    Answer: The noise that was added to the image — DDPM trains the network to predict the noise present in a noisy image at a given timestep. Subtracting a scaled version of that predicted noise yields a slightly cleaner image, one step at a time.

  3. Why are diffusion models slower to generate than GANs?

    Answer: They call the network many times — once per denoising step — instead of once — A GAN produces an image in a single forward pass; diffusion runs the denoiser once per step over dozens or hundreds of steps. Faster samplers (DDIM) and latent diffusion reduce this cost.

FAQ

Diffusion, answered

How do diffusion models generate images?

By reversing a noising process: start from pure Gaussian noise and, at each step, a network predicts a little noise to remove, revealing a cleaner image. After all steps, a coherent image emerges from the static.

Forward vs reverse process?

Forward (fixed): gradually add noise to real images until they're static — used only in training. Reverse (learned): a network undoes one noising step at a time; generation runs this from noise.

What does the model predict?

In DDPM it predicts the noise that was added to a noisy image at a given timestep. Subtracting a scaled version of that noise gives a slightly cleaner image.

What is the noise schedule?

How much noise is present at each timestep — high near the start, dropping to zero at the end. It paces both the corruption and the denoising.

Why are diffusion models slow?

They call the network once per step, over many steps. DDIM cuts the step count and latent diffusion runs in a compressed space to speed things up.

RESULT

—

Next → condition the denoiser on a text prompt and run it in a compressed latent space, and you've got Stable Diffusion — text to image.

Finished this one? 0 / 61 Labs done
NextGradient Descent: The Descent Closest lab to this one

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs