Diffusion, Score-Based Models & Flow Matching

How diffusion models generate: a fixed forward process adds Gaussian noise, a network learns to predict that noise, and a sampler runs the process in reverse.

Advanced · Graduate mathematics · about 25 minutes

Reading map and next steps

Intuition

The idea in plain words, before the symbols.

Diffusion models generate by starting from noise and removing it a little at a time.

The forward process is fixed and involves no learning: take a data vector, such as an image, and add a little Gaussian noise, again and again, until it is almost pure Gaussian noise.

The reverse process is what the network learns. Given a noisy sample and its noise level, the network predicts the noise that was added; equivalently, it predicts the clean sample or the score, since the three are related by the formulas below. Sampling starts from pure noise and applies many small denoising steps.

Sohl-Dickstein et al. (2015) introduced this framing. Ho et al. (2020) made it competitive for image generation with the simple noise-prediction loss below.

Study prompts for this section: Intuition
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Mathematics

Definitions, assumptions and the derivation.

A common discrete-time forward process (DDPM-style) is:

q(xt∣x0)=N ⁣(αˉt x0, (1−αˉt)I),q(x_t\mid x_0) = \mathcal N\!\big(\sqrt{\bar\alpha_t}\,x_0,\ (1-\bar\alpha_t)I\big),

where αˉt=∏s=1tαs\bar\alpha_t = \prod_{s=1}^t \alpha_s and αs=1−βs\alpha_s = 1-\beta_s for a noise schedule βs\beta_s.

Equivalently, you can sample:

xt=αˉt x0+1−αˉt ϵ,ϵ∼N(0,I).x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\epsilon,\quad \epsilon\sim\mathcal N(0,I).

Ho et al. (2020) train with the noise-prediction loss, with tt drawn uniformly from 1,…,T1,\dots,T:

L=Ex0,t,ϵ ∥ϵ−ϵθ(xt,t)∥2.\mathcal L = \mathbb E_{x_0,t,\epsilon}\,\big\|\epsilon - \epsilon_\theta(x_t,t)\big\|^2.

This is a reweighted form of the variational bound on the log-likelihood; in their experiments, dropping the weights gave better samples.

The score view connects denoising to the gradient of the log-density, ∇xlog⁡pt(x)\nabla_x\log p_t(x) (Song and Ermon, 2019; Song et al., 2021). Under the Gaussian corruption above, the score of q(xt∣x0)q(x_t\mid x_0) is −ϵ/1−αˉt-\epsilon/\sqrt{1-\bar\alpha_t}, so a trained noise predictor gives a score estimate:

sθ(xt,t)=−ϵθ(xt,t)1−αˉt  ≈  ∇xtlog⁡pt(xt).s_\theta(x_t,t) = -\frac{\epsilon_\theta(x_t,t)}{\sqrt{1-\bar\alpha_t}} \;\approx\; \nabla_{x_t}\log p_t(x_t).

The score matching page derives why regressing on the noise recovers this score.

The network sees only xtx_t and tt, never the clean point or the noise that produced them, and many pairs (x0,ϵ)(x_0,\epsilon) lead to the same xtx_t. At a given xtx_t the squared loss is therefore minimized by the average of their noise labels, weighted by how likely each clean point is given xtx_t, and that average gives the score of the noisy data distribution ptp_t:

ϵ⋆(xt,t)=E[ϵ∣xt],∇xtlog⁡pt(xt)=−E[ϵ∣xt]1−αˉt.\epsilon^\star(x_t,t)=\mathbb E[\epsilon\mid x_t],\qquad \nabla_{x_t}\log p_t(x_t)=-\frac{\mathbb E[\epsilon\mid x_t]}{\sqrt{1-\bar\alpha_t}}.

The second equation is Tweedie's formula written for noise prediction; the score matching page gives it in terms of the clean point, with its source. It concerns the marginal ptp_t, not the Gaussian q(xt∣x0)q(x_t\mid x_0) of one clean point, whose score, −ϵ/1−αˉt-\epsilon/\sqrt{1-\bar\alpha_t}, is set by a single label. The best prediction is usually none of the labels it averages; the practice after the demo works this out for a distribution with two clean values.

Sampling: running the process backwards

Ho et al. (2020, Algorithm 2) start from xT∼N(0,I)x_T\sim\mathcal N(0,I) and, for t=T,…,1t=T,\dots,1, step

xt−1=1αt(xt−βt1−αˉt ϵθ(xt,t))+σtz,z∼N(0,I),x_{t-1} = \frac{1}{\sqrt{\alpha_t}}\left(x_t - \frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\,\epsilon_\theta(x_t,t)\right) + \sigma_t z, \qquad z\sim\mathcal N(0,I),

with σt2=βt\sigma_t^2=\beta_t (one of two variances they found to work about equally well) and z=0z=0 at the last step. This needs one network evaluation per step, and T=1000T=1000 in their experiments.

  • Fewer steps. DDIM (Song, Meng and Ermon, 2021) reuses the same trained network with a deterministic update. It estimates the clean sample, x^0=(xt−1−αˉt ϵθ(xt,t))/αˉt\hat x_0 = \big(x_t-\sqrt{1-\bar\alpha_t}\,\epsilon_\theta(x_t,t)\big)/\sqrt{\bar\alpha_t}, and jumps to an earlier time s<ts<t with xs=αˉs x^0+1−αˉs ϵθ(xt,t)x_s=\sqrt{\bar\alpha_s}\,\hat x_0+\sqrt{1-\bar\alpha_s}\,\epsilon_\theta(x_t,t). Skipping steps this way gave samples 10 to 50 times faster than DDPM sampling in their experiments.
  • Continuous time. Song et al. (2021) treat the noising process as a stochastic differential equation. Its time reversal, or a deterministic "probability flow" ODE with the same marginal distributions, can be integrated with standard numerical solvers using the learned score.
  • Guidance. To sample from a conditional model, for example one conditioned on a text prompt cc, classifier-free guidance (Ho and Salimans, 2021) trains one network both with cc and with cc dropped, then samples with ϵ~=(1+w) ϵθ(xt,c)−w ϵθ(xt)\tilde\epsilon = (1+w)\,\epsilon_\theta(x_t,c) - w\,\epsilon_\theta(x_t). A guidance weight w>0w>0 trades sample diversity for closer agreement with cc.
  • Latent diffusion. Rombach et al. (2022) run the whole process in the latent space of a pretrained autoencoder instead of on pixels, which makes high-resolution generation much cheaper.

Flow matching trains a velocity field instead of a noise predictor. Lipman et al. (2023) show that the diffusion noising paths are one family among the probability paths it can use.

Study prompts for this section: Mathematics
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Code

The same calculation as runnable code, in the notation of the derivation.

import numpy as np

T = 1000
beta = np.linspace(1e-4, 0.02, T)
alpha = 1.0 - beta
alpha_bar = np.concatenate([[1.0], np.cumprod(alpha)])  # alpha_bar[0]=1 (no noise)

x0 = np.array([1.0, -1.0])
for t in [0, 10, 100, 500, 1000]:
    eps = np.random.randn(*x0.shape)
    xt = np.sqrt(alpha_bar[t]) * x0 + np.sqrt(1.0 - alpha_bar[t]) * eps
    print(f"t={t:>3}  xt={np.round(xt, 3)}  noise_std={np.sqrt(1.0 - alpha_bar[t]):.3f}")

The schedule is DDPM's linear one, β\beta from 10−410^{-4} to 0.020.02 over T=1000T=1000 steps. By t=500t=500 the noise standard deviation is already about 0.960.96, so most of the original signal is gone well before the last step.

Study prompts for this section: Code
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Demo

Change an input, predict the result, and see which quantities respond.

ExploreChange one control at a time and name what responds.An optional observation guide follows the demo.

Explore Diffusion, Score-Based Models & Flow Matching

Loading the interactive demo…

Observation guide

Optional and ungraded. Opening a guide saves nothing.

Choose what to inspect in Diffusion, Score-Based Models & Flow Matching, then open its guide.

The demo shows only the forward process. Scrub tt to watch

xt=αˉtx0+1−αˉtϵx_t=\sqrt{\bar\alpha_t}x_0+\sqrt{1-\bar\alpha_t}\epsilon

push a two-blob toy distribution toward a near-Gaussian noise cloud. Before checking, predict whether a fixed structure-match measure crosses its threshold early, in the middle, or late. It does not train a network or run a sampler; score matching and flow matching have their own pages.

Study prompts for this section: Demo
Study prompts

Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.

Practice

One noisy value could have come from either of two clean values. Find the two noise labels, the output that minimizes squared error, and the score; then change the noise level.

Loading the exercise…
In focus

Concept: Diffusion, Score-Based Models & Flow Matching

What is the smallest example of Diffusion, Score-Based Models & Flow Matching you can work through by hand, and what does it show?

BeforeMaximum LikelihoodNextScore Matching & Score-Based Generative Models
DetailsGenerative Models

After the first pass

Go deeper

Optional material for a second reading. Sources are always shown; the other panels open from their headings or from these links, and closing one keeps what you wrote or revealed.
What to look for in each sectionChoose a section and what you expect to see in it, then read short advice.

Section by section

Choose what to look for in each section

How diffusion models generate: a fixed forward process adds Gaussian noise, a network learns to predict that noise, and a sampler runs the process in reverse.

Choose what to look for (no demo yet)01 / Intuition
Illustration of noisy samples following a reverse denoising trajectory toward a structured data manifold.
What to look for

Start with the picture, metaphor, or geometric mechanism.

Choose first

Choose what you expect to see in this section of Diffusion, Score-Based Models & Flow Matching.

Questions to ask of the pictureChoose a part of the picture and what you expect, then read short advice.

Looking at the picture

Questions to ask of the picture

How diffusion models generate: a fixed forward process adds Gaussian noise, a network learns to predict that noise, and a sampler runs the process in reverse.

4 of 4 sections writtenNo live demo yet
Question

Which part of the picture should you look at first?

Choose first

Pick the part of the picture you expect to explain Diffusion, Score-Based Models & Flow Matching.

Sources

References for this page

A reference shows where an idea comes from; it does not vouch for every step on this page.
paper · 2015Deep Unsupervised Learning using Nonequilibrium ThermodynamicsSohl-Dickstein et al.
Why it is cited

Introduces diffusion probabilistic models: a fixed process that gradually adds noise, paired with a learned reverse process (ICML 2015).

Open source
paper · 2020Denoising Diffusion Probabilistic ModelsHo, Jain, and Abbeel
Why it is cited

Gives the closed-form forward process q(x_t|x_0), the simplified noise-prediction loss and the ancestral sampler (Algorithm 2) used on this page (NeurIPS 2020).

Open source
paper · 2019Generative Modeling by Estimating Gradients of the Data DistributionSong and Ermon
Why it is cited

Learns the score of the data distribution at many noise levels with denoising score matching and samples with annealed Langevin dynamics (NeurIPS 2019).

Open source
paper · 2021Score-Based Generative Modeling through Stochastic Differential EquationsSong et al.
Why it is cited

Treats noising as a stochastic differential equation and samples with its time reversal or with the probability-flow ODE, both driven by a learned score (ICLR 2021).

Open source
paper · 2021Denoising Diffusion Implicit ModelsSong, Meng, and Ermon
Why it is cited

Keeps the DDPM training objective but samples with a deterministic, non-Markovian update that can skip steps, reported 10 to 50 times faster (ICLR 2021).

Open source
paper · 2021Classifier-Free Diffusion GuidanceHo and Salimans
Why it is cited

Trains one network with and without the conditioning and mixes the two predictions at sampling time to trade diversity for fidelity (NeurIPS 2021 Workshop on Deep Generative Models and Downstream Applications).

Open source
paper · 2022High-Resolution Image Synthesis with Latent Diffusion ModelsRombach et al.
Why it is cited

Runs diffusion in the latent space of a pretrained autoencoder to reduce the cost of high-resolution synthesis (CVPR 2022).

Open source
book · 2024Deep Learning: Foundations and ConceptsBishop and Bishop
Why it is cited

Springer. Chapter 20 is a textbook treatment of diffusion models, from the forward noising process to the learned reverse process and guided generation.

Open source
What each claim rests on1 claim, each with its sources, where it appears on this page, and its caveats.

Claims and sources

How diffusion models generate: a fixed forward process adds Gaussian noise, a network learns to predict that noise, and a sampler runs the process in reverse.

Checked against the cited passages

The checks were made by this site, not by an independent reviewer.

Diffusion can be taught as gradual corruption plus learned reverse denoising: Sohl-Dickstein frames forward diffusion/reversal; Ho supplies q(x_t|x_0), reparameterized x_t, and epsilon-prediction MSE; Song Score SDE supplies the score-based reverse-time sampling bridge.
What the sources support

Sohl-Dickstein et al. introduce iterative forward corruption with a learned reverse process. Ho et al. give the closed-form q(x_t|x_0), the reparameterized sample x_t and the simplified noise-prediction loss...

Where to see it on this page
Equation 1
q(xt∣x0)=N ⁣(αˉt x0, (1−αˉt)I),q(x_t\mid x_0) = \mathcal N\!\big(\sqrt{\bar\alpha_t}\,x_0,\ (1-\bar\alpha_t)I\big),
Equation 2
xt=αˉt x0+1−αˉt ϵ,ϵ∼N(0,I).x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\epsilon,\quad \epsilon\sim\mathcal N(0,I).
Caveat

The code and demo compute forward noising only; no network is trained and no sampler is run. Sampling, guidance and latent diffusion are summarized from their sources, and nothing here measures image quality.

StatusChecked against the cited passages.

Sohl-Dickstein supports iterative forward corruption plus learned reversal; Ho supports DDPM q(x_t|x_0), reparameterized x_t, and L_simple epsilon-MSE; Song et al. support reverse-time SDE sampling from time-dependent scores. The equations, code and demo on this page show only forward noising; the epsilon-MSE loss and score-SDE sampling rest on the sources and are not simulated here.

Explain it without the pageWrite your own explanation before asking for feedback. Your draft and any hints stay while this page is open.

Practice · Diffusion, Score-Based Models & Flow Matching

Try the idea in your own words

How diffusion models generate: a fixed forward process adds Gaussian noise, a network learns to predict that noise, and a sampler runs the process in reverse.

Concept · Selected for practice

Diffusion, Score-Based Models & Flow Matching

What it rests on: Sources: Deep Unsupervised Learning using Nonequilibrium Thermodynamics; Denoising Diffusion Probabilistic Models; Generative Modeling by Estimatin...

Context and links
Choose a task

Explain the mechanism

For Diffusion, Score-Based Models & Flow Matching: What is the smallest example of Diffusion, Score-Based Models & Flow Matching you can work through by hand, and what does it show? Explain your answer, including what changes, why, and which assumption matters.

No answer yet

A rough first thought is enough. Your draft stays when you change tasks.

Your draft stays on this page and is cleared when you leave or reload.

A little help · Explain

Open one hint at a time. These are suggestions, not your answer or a grade.

0 of 3 hints shown for this question.

    Clearing your answer keeps this help record. Outside help is your own report; this page cannot check it.

    Where am I stuck? (optional)
    Your own description, not an automatic diagnosis

    Choose one, or leave this unspecified. Select it again to clear it.

    Take your draft to a feedback conversation

    No AI feedback runs here. You can copy a prompt to use elsewhere; nothing is sent automatically. Review the text before sharing, and leave out private information.

    Write an attempt before copying a feedback prompt.

    A draft, or an AI reply to it, is not a test of what you have learned. To check that, try a different case later without help.

    Ask about this pagePick one item on this page and copy a prompt about it into an AI assistant you already use. Notes you write here stay in this browser.
    Ask about this pageClose
    ConceptDiffusion, Score-Based Models & Flow MatchingSources: Deep Unsupervised Learning using Nonequilibrium Thermodynamics; Denoising Diffusion Probabilistic Models; Generative Modeling by Estimatin...T = 1000

    Your question

    Choose what your question is about

    Pick the idea, equation, source, code, claim, misconception or demo state you want to ask about. The prompt you copy includes it, so the answer can stay on that item.
    Next local actionNo local draft saved yet

    Open the draft below to save one note and next action in this browser.

    conceptGenerative Models

    Diffusion, Score-Based Models & Flow Matching

    Starting question

    What is the smallest example of Diffusion, Score-Based Models & Flow Matching you can work through by hand, and what does it show?

    SourcesCheck the 8 cited sources listed under Sources.Notes can be saved for this item
    Points of view on this item

    Fixed prompts generated from this item. They are not comments from people or independent reviews.

    Learner’s first stepAsk what would make "Diffusion, Score-Based Models & Flow Matching" feel predictable rather than familiar.
    Assumption

    The 8 cited sources must support this exact item, not just the surrounding topic.

    Source-checking summary

    Connect the definition to one equation, piece of code or demo before widening the discussion.

    Proposed experiment

    Change one input, then check that the same relationship holds in the mathematics, the code and the demo.

    Next action

    You can state the mechanism in your own words

    Evidence4 checks
    ObservationChecking for a saved observation
    ActionReady for one action
    PromptLearner prompt ready to copy
    Go to this item
    01ObservationChecking what this browser has saved
    02EvidenceChecking for a saved observation
    03SourcesCheck the 8 cited sources listed under Sources.
    04Next stepSave one next action
    Local action draftNo local draft saved yetOpen when you are ready to write one next action
    Local action draft

    This draft stays in this browser and is attached to this item only.

    No local draft saved.
    Evidence to inspect
    • What the 8 cited sources say about this exact item
    • The definition, its prerequisites, and a contrasting concept
    • The equation or code that makes the concept concrete
    • One demo state that shows what stays the same as the inputs change
    What would resolve this
    • You can state the mechanism in your own words
    • You can name the prerequisite that would clear up the confusion
    • You can predict how the result changes when one input changes
    Reference idconcept/concept-notebook/generative-models/diffusion concept:generative-models/diffusion