Noise, Scores & Diffusion Models
After this lesson, you will be able to:
- Write the DDPM forward noising step x_t = √(1 − β_t) x_{t-1} + √(β_t) ε in discrete form and derive its closed form q(x_t | x_0) from sums of Gaussians
- Define the score ∇_x log p(x), explain why following it moves a noisy sample toward higher density, and show why regressing onto −ε/σ learns the score of the noisy data (Tweedie's formula)
- Run a one-dimensional toy: fit a score by denoising score matching, then generate samples with the reverse chain
Before you start
#A Quick Heads-Up Before You Start
This is Part 1 of two linked lessons. It tells the whole diffusion story with discrete steps, Gaussians and a gradient, and you can stop here and still understand how diffusion models train and sample. The next lesson, Stochastic Calculus & SDEs (Advanced), is optional: it rewrites the story in continuous time with stochastic calculus. Do it when you want to read Song et al. 2020 or any paper withdx_tin it.
Why split it this way? The only thing that genuinely needs Itô calculus is the step from "many small noise steps" to "one equation in continuous time". Everything a practitioner does with a diffusion model (train it, sample from it, debug it) can be explained without that step. So these two lessons teach the practitioner's story first and the physicist's language second.
Try it! Open the Python REPL (bottom-right of the screen: click Quick Actions, then Python) and type the short code lines yourself.
#Adding Noise in Discrete Steps
x_0 = 2.0. A diffusion model destroys it slowly, one small step at a time, and each step does two things: shrink the value a little, then add a little Gaussian noise.β = 0.1 and one random noise draw ε = 0.5, one step gives:x_1 = √(1 − 0.1) · 2.0 + √0.1 · 0.5
= 0.9487 · 2.0 + 0.3162 · 0.5
= 1.8974 + 0.1581 = 2.0555
The value moved from 2.0 to 2.0555. Mostly the shrink factor 0.9487 acted, and the noise nudged it up a little. Repeat that many times and the original value fades while the noise piles up.
β_t at each step t:x_t depends only on x_{t-1}, with a Gaussian transition rule over continuous states.x_0 = 2. If you only add noise (no shrinking), the variance grows by β every step: after 10 steps of β = 0.1 it is 1.0, after 100 steps it is 10.0, and it never stops growing. With the shrink factor, the variance obeys v ← (1 − β) v + β, which settles at 1. If the data already has variance 1, the variance stays exactly 1 at every step, which is why this is called a variance-preserving chain. The destination is always N(0, 1), a distribution you know how to sample from.In DDPM, the forward step is x_t = √(1 − β_t) x_{t-1} + √(β_t) ε where ε ~ N(0, I). What does the contraction factor √(1 − β_t) accomplish?
#The closed form: jump straight to step t
α_t = 1 − β_t and ᾱ_t = α_1 · α_2 · … · α_t (the product so far, called alpha-bar). Then:β = 0.1 for 10 steps gives ᾱ = 0.9^10 = 0.3487, so the mean of x_10 is √0.3487 · 2.0 = 1.181 and its variance is 1 − 0.3487 = 0.651. The standard DDPM schedule (β rising from 0.0001 to 0.02 over 1000 steps) ends with ᾱ_1000 = 4.0e-5, so √ᾱ = 0.0064: the original data is scaled by 0.0064 and the rest is pure noise. At the halfway step ᾱ_500 = 0.0786.x_0 = 2 and compares it with the closed form.t, compute x_t from x_0 directly, and train on that one pair.Why does the closed form hold? Every step is a linear map of a Gaussian plus an independent Gaussian, and a chain of such steps stays Gaussian. Open the derivation to see the induction line by line.
#The Score Function (Reframed)
p(x) ∝ exp(−x²/2). Take the log: log p(x) = −x²/2 + constant. Take the derivative: d/dx log p(x) = −x.x = 0.5 that is −0.5; at x = 3 it is −3; at x = 0 it is 0. The sign always points back toward the peak at 0, and the size is how far away you are. That derivative of the log-density is the score:x = 10 is −10. For the heavy-tailed Cauchy density p(x) = 1 / (π(1 + x²)) the score is −2x / (1 + x²), which at x = 3 is −0.6 and at x = 10 is only −0.198. Far from the data, how strongly a model pulls you back depends entirely on how fast the tails of the distribution fall off.Why does following the score undo noise? A noisy sample sits off to the side of where clean data lives. The score of the noisy data points from there toward where noisy points are denser, and that direction is also where the clean sample most likely came from. The next section makes this exact.
#Denoising Score Matching and the Tweedie Line
x_0 and corrupt it with Gaussian noise at some level, using the closed form above:x_t = α x_0 + σ ε, with α = √ᾱ_t, σ = √(1 − ᾱ_t), ε ~ N(0, I)α is not the α_t = 1 − β_t of a single noising step. It is the whole-way factor √ᾱ_t.x_t given a known x_0. Since x_t given x_0 is a Gaussian with mean α x_0 and variance σ², its score is∇_{x_t} log q(x_t | x_0) = −(x_t − α x_0) / σ² = −ε / σε yourself. The marginal score ∇ log p_t(x_t) is the one you actually want: the score of the noisy data over all possible x_0. You cannot compute that directly.s_θ(x_t) by least squares to predict the known target −ε/σ:∇ log q(x_t | x_0) over the clean samples that could have produced x_t. Differentiating the marginal density p_t(x_t) = ∫ q(x_t | x_0) p(x_0) dx_0 under the integral shows that this very average equals ∇ log p_t(x_t). Regressing onto −ε/σ therefore yields exactly the score of the noisy marginal. That is Vincent's result (2011), and it is why diffusion training looks like ordinary supervised learning.x_0 = −2 and x_0 = +2, equally likely, at α = σ = 0.7071 (so σ² = 0.5). Start with x_t = 0, which either value could have produced. From x_0 = −2 the noise draw is ε = +2, and from x_0 = +2 it is ε = −2. The two targets −ε/σ are −2.83 and +2.83, equally likely.x_t = 0 again and again, the model settles on their average, 0. That is the marginal score there: the noisy density has a dip between the two bumps, and the score is flat at the dip.x_t = −1, close to the −2 value. Each posterior weight is proportional to exp(−(x_t − α x_0)² / (2σ²)). That gives 0.8423 for x_0 = −2 and 0.0029 for x_0 = +2, so the weights are 0.9965 and 0.0035. The conditional scores −(x_t − α x_0)/σ² are −0.83 and +4.83. Their weighted average is 0.9965 × (−0.83) + 0.0035 × 4.83 = −0.8087.log p_t at −1 also gives −0.8087. As a second check, Tweedie below gives (−1 + 0.5 × (−0.8087)) / 0.7071 = −1.9861, which is the posterior mean 0.9965 × (−2) + 0.0035 × 2.Open the derivation to follow every line.
α means √ᾱ_t, the keep-factor for the whole jump from x_0. In the one-step reverse update later, the same role is played by √α_t, where α_t = 1 − β_t. Neither is the α in the Metropolis acceptance rule of the Markov chains lesson, which is a probability.−(x_t − α x_0) / σ² inside the posterior average and solve for the clean-sample estimate:Open the derivation to see the solve step by step.
ε instead of the score. They are the same thing in two outfits: s_θ(x_t) = −ε_θ(x_t) / σ. Training on ‖ε − ε_θ(x_t, t)‖² is denoising score matching with a rescaled target, and the single network is told t so it can handle every noise level.E[½‖s_θ(x) − ∇log p(x)‖²] contains the unknown ∇log p. Expanding the square leaves one troublesome piece, the cross term. Integration by parts removes the unknown score from it and leaves E[tr(∇_x s_θ(x))], the trace of the model's own derivative matrix (the Jacobian). The result is L(θ) = E[½‖s_θ(x)‖² + tr(∇_x s_θ(x))] + constant. Open the derivation for the integration by parts.#A Tiny 1-D Toy You Can Run
−2, half near +2, each bump with standard deviation 0.5. After corrupting at ᾱ = 0.5 (so α = σ = 0.7071), the noisy data is again two Gaussian bumps, so its true score has a closed form. That lets us compare a learned score against the truth.The cell below does denoising score matching with a tiny model (13 bump-shaped features, fitted by least squares) and then checks Tweedie's formula against a brute-force average.
x_t = −0.5 the learned value is −1.092 against the exact −1.036, and at x_t = −1.0 it is −0.578 against −0.614. The mean absolute gap over [−3, 3] is 0.0724. The exact score at x_t = 0 is 0, because 0 is the dip between the two bumps, where the two pulls cancel (the learned value is −0.045). Notice that the sign always points toward the nearer bump. The Tweedie line also checks out: at x_t = −1 the brute-force average of the clean samples is −1.845 against the formula's −1.849, and at x_t = 1 it is 1.856 against 1.849. The noisy point −1 most likely came from the bump at −2.Now the generation half. Starting from pure noise, run the chain backwards. One reverse step takes the score and does the Tweedie-style move plus fresh noise:
N(0, 1), so the score at x_t = 0.7 is exactly −0.7, and take β = 0.01, so α_t = 0.99.0.7 + 0.01 × (−0.7) = 0.693. Undoing the shrink divides by √0.99 = 0.995, giving 0.6965. Fresh noise has size √0.01 = 0.1. If the draw is z = −0.8, the step adds −0.08 and lands at x_{t−1} = 0.6165.Where does this update come from? Bayes' rule plus Tweedie's formula give it, and the derivation shows which part is exact and which part is a small-step approximation.
The cell below uses the exact score of the noised data at every step (a trained network would supply an estimate of it instead) and samples 100,000 points from pure noise.
ᾱ of 1.9e-05, so the chain starts from essentially pure noise. About 50.1% of the samples land right of zero. The right bump has mean 1.992 and standard deviation 0.501, and the left bump has mean −1.998 and standard deviation 0.501. The data had centres at ±2 and standard deviation 0.5, so the reverse chain recovered both the locations and the widths from pure noise.That is the entire diffusion model: a forward noising chain, a score (or noise) predictor trained by regression, and a reverse chain that follows the score with fresh noise. Part 1 ends here.
Denoising Diffusion Probabilistic Models
Jonathan Ho, Ajay Jain, Pieter Abbeel (2020)
#Try It Yourself
ᾱ schedule, the closed form and a simulation of the chain. Then you check the closed form against the simulation, see why the shrink factor is needed, and confirm the variance-preserving property on the standard schedule.Tests: Verify alpha_bar_schedule returns a running product the same length as betas; closed_form(2.0, 0.3487) gives mean about 1.181 and variance about 0.651; the simulated mean and variance from 200,000 copies land within about 0.01 of the closed form; the no-shrink variance is about 1 after 10 steps and about 10 after 100; the standard-schedule abar at step 1000 is about 4e-05; and unit-variance data jumped to step 1000 keeps variance near 1.
ᾱ_10 = 0.3487, closed-form mean 1.1810 and variance 0.6513 against simulated 1.1834 and 0.6476 from 200,000 copies. Without the shrink, the variance is 1.00 after 10 steps and 10.02 after 100, growing by β every step, so the shrink is what stops it. On the standard schedule ᾱ is 0.9999 at step 1, 0.0786 at step 500 and 4.04e-05 at step 1000. Unit-variance data jumped straight to step 1000 still has variance 1.0026, which is the variance-preserving property in numbers. With β = 0.1, ᾱ first drops below 0.01 at step 44 (ᾱ = 0.0097), so even a fairly large β needs dozens of steps to bury the data.#A Final Recap
This lesson tied together earlier chapters of the math track:
- Markov chains gave you the discrete forward process
q(x_t | x_{t-1}). - Multivariate Gaussians gave you the closed form
q(x_t | x_0)and the reparameterisation trick (write a sample as mean plus scale times fixed noise) that made everything trainable. - MLE and score functions gave you the idea of a gradient of log-density, which here is taken over the data.
- Information theory supplies the KL divergence and variational bounds that the full diffusion derivation rests on.
- Matrix calculus and gradient descent are how you actually train the score network.
- Random variables, expectation and variance are the language every loss is written in.
If you can derive the DDPM forward closed form from the Markov definition and state why regressing onto −ε/σ learns the score, you have the math needed to follow how any diffusion model trains and samples. The next lesson, Stochastic Calculus & SDEs (Advanced), is optional: it adds the continuous-time language you need to read the papers themselves.
#Quick Check
In the forward process x_t = √(1 − β) x_{t-1} + √β ε with β = 0.1, you start from x_0 = 2 and run 10 steps (so ᾱ = 0.9^10 = 0.3487). What is the distribution of x_10?