Diffusion Models
Add noise to a known signal, inspect the recovery calculation, and understand what a denoising network must learn.
Jump to a section
Why this matters
A diffusion model learns to undo a controlled corruption process. Training examples start from real data and add noise at known levels. A network receives the corrupted input and a description of its noise level, then learns a useful prediction such as the added noise, the clean signal, or a velocity parameterization.
Generation is a different procedure: start from noise and use the trained model inside a sequence of reverse updates. Knowing how noise was added does not give us the noise in a new generated sample. Learning that conditional structure is the difficult part.
Build the mental model
Think about one noisy observation rather than a finished image. At a high signal level, the observation retains much of the original structure. At a low signal level, many clean signals could plausibly have produced it. The model must use patterns learned from the training distribution, and possibly conditioning such as text, to make a useful prediction.
The worked example below deliberately keeps the original signal and noise available. It lets you verify the algebra and then introduce an error into the noise estimate. It does not train a network or claim to generate images.
Notice three things as you move the controls:
- Keeping the noise draw fixed isolates the effect of the signal level.
- A perfect noise estimate recovers the original signal wherever the signal coefficient is nonzero.
- The same prediction error is amplified more strongly when very little signal remains.
Separate noising from knowing how to denoise
Mix a known signal with fixed Gaussian noise, then inspect how an error in the noise estimate changes the recovered signal.
Reduced motion is on. Use Previous and Next to inspect each state.
x̂₀ = (xₜ − √(1−ᾱ) ε̂) / √ᾱ
Start with a known signal
This 8 × 8 signal and seeded noise are available to us. A trained denoiser would not be given the true noise.
Explore the inputs and calculations
Read the two calculations
xₜ = √ᾱ x₀ + √(1−ᾱ) ε
x̂₀ = (xₜ − √(1−ᾱ) ε̂) / √ᾱ
The estimate is ε̂ᵢ = εᵢ + error × sin(i + 1). At each setting the Gaussian draw stays fixed. Color intensity clamps at |value| = 2; the MSE uses the full, unclipped values.
Why does the same noise-prediction error hurt more when little signal remains?
Reconstructing x₀ divides by √ᾱ. Its error is −√((1−ᾱ)/ᾱ) times the noise-prediction error. That amplification grows as ᾱ becomes small.
Work through the math
For the variance-preserving forward process, a noisy sample can be drawn directly from the clean sample:
Here is a cumulative signal-retention coefficient. The slider controls that coefficient directly; it does not implement a particular timestep schedule.
Given a noise estimate , rearranging gives an estimate of the clean signal:
If , then
At , the amplification factor is 1. At , it is 7. This identity explains why a small error in the predicted noise can cause a much larger error in a direct clean-signal estimate at high noise. A complete reverse sampler uses additional schedule-dependent coefficients and, depending on the method, stochastic noise; repeatedly applying this identity alone is not DDPM sampling.
Read the implementation
The following dependency-free example checks one corruption and reconstruction. It uses known noise so the expected answer is inspectable.
1from math import sqrt
2
3
4def corrupt(clean, noise, alpha_bar):
5 return [sqrt(alpha_bar) * x + sqrt(1 - alpha_bar) * e
6 for x, e in zip(clean, noise)]
7
8
9def estimate_clean(noisy, predicted_noise, alpha_bar):
10 if not 0 < alpha_bar <= 1:
11 raise ValueError("alpha_bar must be in (0, 1]")
12 return [(z - sqrt(1 - alpha_bar) * e) / sqrt(alpha_bar)
13 for z, e in zip(noisy, predicted_noise)]
14
15
16clean, noise = [1.0, -1.0], [0.25, -0.5]
17noisy = corrupt(clean, noise, 0.5)
18recovered = estimate_clean(noisy, noise, 0.5)
19assert all(abs(a - b) < 1e-12 for a, b in zip(clean, recovered))A noise-prediction training loop instead samples real examples and timesteps, creates noisy observations, runs a neural network on those observations and timesteps, and minimizes a prediction loss. The known noise is the training target. It must not be secretly supplied to the model as an input.
Questions and trade-offs
Pixel space or latent space? Latent diffusion runs the generative process in a learned compressed representation. An encoder and decoder connect that representation to images. The smaller state can reduce computation, but the autoencoder introduces its own reconstruction limits.
More sampling steps or a better sampler? Step count alone does not specify quality or cost. The model, noise schedule, numerical method, guidance, and hardware all matter. Compare matched settings rather than assuming a universal quality curve.
Why can guidance change the image? Conditioning changes the predicted direction. Stronger guidance can improve agreement with the condition while also changing diversity and introducing artifacts. Our algebraic example contains no conditioning or guidance network.
Is an x₀ estimate the final generated image? Not generally. It can be an intermediate quantity inside a sampler. The assumptions of the sampler determine how it is used.
Check your understanding
At , a scalar noise-prediction error is . What is the resulting error in the direct clean-signal estimate?
The amplification is , and the sign is negative: the clean estimate is off by . Explain why this calculation does not prove that a trained denoiser can recover a unique original image from arbitrary noise.
Sources and further reading
- Ho et al., Denoising Diffusion Probabilistic Models: forward process, training objective, and reverse sampler.
- Rombach et al., High-Resolution Image Synthesis with Latent Diffusion Models: diffusion in a learned latent representation.
Oliver Perrin
Machine Learning Engineer · Founder, LiminalML
LiminalML brings together concept explanations, mathematical examples, and working visuals. Find more writing by Oliver on the LiminalML Substack.
Keep exploring the idea.
Ask a follow-up in a guided session, work through the self-check, or open the visual in Studio to build on it.
Related concepts
Deep Learning
Transformer Architecture
Follow attention, feed-forward layers, normalization, and residual connections through a Transformer block.
Optimization
Gradient Descent (SGD, Momentum, Adam)
Follow updates across a loss surface, then connect learning rate, momentum, and Adam to the update equations.
Deep Learning
Backpropagation
Trace local derivatives backward through a computation and connect the chain rule to a manual implementation.