Diffusion Models

Destroy a signal with noise in small steps, then learn to undo one step. Run the undoing from pure noise and you have a sample.

Overview

Forwards is free

Take data and add a little Gaussian noise. Repeat. After enough steps nothing of the first remains and you've pure noise.

This forward process has no params and nothing to learn. Better still, it has a closed form: you can jump straight to any step without modelling the ones before it.

x_t  =  sqrt(abar_t) * x_0  +  sqrt(1 - abar_t) * epsilon

abar_t is the fraction of the first signal still present, and it falls along a fixed schedule. The second panel plots the cosine schedule used here, and the readout gives the signal and noise proportions at what step you select.

That closed form is why training is cheap: pick a random t, jump there right, and train on that one step. No modelling.

Diffusion Models

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

The forward process is fixed and has no parameters: xt = √ā x0 + √(1−ā) ε.
You can jump to any step in one shot, which is why training samples a random t rather than stepping through.
The model predicts the noise, not the image. Given the noise, the original is recoverable by rearranging one equation.
Sampling is the expensive part: the reverse process is many small steps, where a GAN needs one.

Diffusion Models

Learning to reverse a process that is trivial to run forwards. Why that turned out to work so much better than the options.

Backwards is the model

The generative process runs the other way: start from pure noise and undo one step at a time until an image appears.

Each reverse step needs to know what noise was added, and that is the entire learned component. The network takes x_t and t and predicts epsilon.

Given the noise, getting back the first is rearranging the equation above:

x_0  =  ( x_t  -  sqrt(1 - abar_t) * epsilon )  /  sqrt(abar_t)

The demo does just this with the *true* noise — the target the model is trained on — and the readout gives the getting back error, which is zero at each step. The forward process is just invertible if you know the noise. Predicting it's the whole job, and it's why the loss is a plain squared error on a noise vector rather than any of it rival.

Why this beats what came before

A stable goal. Predicting noise is plain guided regression. No minimax game, no critic to balance, no mode collapse. Training a diffusion model is boring in a way GAN training never was, and that is the point.

Coverage. GANs can skip parts of the spread. A diffusion model is trained to denoise each example. So It can't quietly drop a mode.

Sharpness. Unlike a VAE, there's no pixel-blending term rewarding a blurred hedge.

The cost is sampling speed. Generation is many sequential passes — at first a thousand, now often ten to fifty with a better sampler — where a GAN needs one. DDIM and distillation methods have shortened this a lot, and it remains the main disadvantage.

The schedule matters

Drag t from 0 to 60 and watch how quickly the signal disappears. The first DDPM used a linear schedule. This Destroys the signal too fast at the end — the last steps are all noise and contribute nothing to learning. The cosine schedule shown here keeps more signal for longer and trains better.

Schedule design is a real part of the field. So is the choice of what the model predicts: the noise, the first, or a mixture called v, all of which are algebraically equal and behave differently .

In practice

Latent diffusion runs the whole process in a VAE's latent space rather than in pixels, cutting the cost enormously. Stable Diffusion is this.

Classifier-free guidance trains the model both with and without a text rule, then extrapolates away from the unconditioned guess at sampling time. It's what makes prompts really steer the output, and turning it up trades range for prompt adherence.

The closed form that makes training cheap

The reason diffusion training is cheap is one equation: the forward process has a closed form, so you can jump to any noise level without modelling the steps before it.

x_t = sqrt(abar_t) · x_0 + sqrt(1 - abar_t) · epsilon

abar_t is the fraction of the first signal still present, falling along a fixed schedule. On the cosine schedule over 60 steps:

step tsignal kept, sqrt(abar)noise, sqrt(1-abar)
01.000.00
150.920.39
300.700.71
450.380.93
600.001.00

Training picks a random t, jumps straight there with one draw of epsilon, and asks the network to predict that epsilon — a plain squared-error regression, no modelling, no adversary. And the forward step is just invertible if you know the noise: rearranging the equation recovers x_0 from x_t and epsilon with zero error. So Predicting the noise is the whole job. The cosine schedule above is chosen over the first linear one just because linear destroys the signal too fast. Its final steps are nearly pure noise and teach almost nothing, while the cosine keeps signal alive longer and trains better.

All that made diffusion win over GANs follows from that squared-error goal. Predicting noise is plain guided regression, so there's no minimax game to balance and no mode collapse. The model is trained to denoise *each* example, so it can't quietly drop part of the spread the way a GAN can. The price is sampling speed: generation is many sequential denoise steps, at first a thousand and now often ten to fifty with a better sampler, where a GAN needs one forward pass. Latent diffusion cuts the cost by running the whole process in a VAE's latent space rather than in pixels. This Is what Stable Diffusion is, and classifier-free guidance is what lets a text prompt really steer the result.

One last point of confusion worth heading off: most diffusion models predict the *noise*, not the image, and some predict the first or a mixture called v. The three are algebraically equal but behave differently in training, and given the network outputs the picture rather than the noise makes the sampling code nonsense.

Where it goes wrong with Diffusion Models

Expecting fast sampling. It's the known weakness.

A schedule that destroys the signal too early. Wasted steps.

Guidance turned up too high. Saturated, oversimplified images and merged range.

given the model predicts the image. Most predict the noise, and confusing the two makes the sampling code nonsense.

Questions people ask about Diffusion Models

How does a diffusion model generate images? It starts from pure noise and removes a little noise at each step with a trained network, until a clean sample remains.

What does the network actually predict? Usually the noise that was added to a noisy input, from which the clean image or the score can be derived.

Why is training cheap even though sampling takes many steps? The forward noising has a closed form, so you can jump to any step directly. Each training step needs only one random timestep.

Why is sampling slow, and how is it sped up? It needs hundreds of sequential network calls. DDIM, better samplers and distillation cut that to a few dozen steps or fewer.

How are diffusion models compared with GANs? They train more stably and cover the data distribution better, at the cost of slower generation. GANs are fast but prone to mode collapse.

What is the noise schedule? The list of how much noise is added at each step. Poor schedules leave signal in the last step or waste steps, so it affects quality.

Destroy the data, then learn to undo it one step at a time

Diffusion adds noise on a schedule until nothing is left, then trains a model to drop a little of it. Both directions are run here on 1-D data, including the closed form that makes training cheap.

example_01.pyNumPy
Output

The next block shows the data dissolving step by step under that schedule.

example_02.pyNumPy
Output

The next block trains a small network to predict the noise that was added at each step.

example_03.pyNumPy
Output

The next block uses the trained model to generate samples from pure noise, then compares them against the real data.

example_04.pyNumPy
Output

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does the network in a diffusion model predict?

  2. Why can training sample a random step t rather than simulating up to it?

  3. What is the main disadvantage against a GAN?

Cheat sheet

Diffusion Models

Add noise to an image step by step, train a network to reverse each step, then sample a new image by running the learned reverse chain.

DEEP LEARNING · vizlearn.in/deep_learning/diffusion_models.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.