Diffusion and flow matching in 2D
Watch noise turn into data on small 2D examples. Add noise to a data set, see the score field that points back to the data, and sample with the reverse SDE, the probability-flow ODE and flow matching. Everything is computed exactly in your browser; nothing is trained.
A diffusion model creates new data (images, sounds, molecules) from pure noise. It learns to undo a fixed process that destroys data by adding noise step by step. On this page you see the whole idea on points in the plane:
- Forward process. Add Gaussian noise to the data until only noise is left.
- Score. At every noise level, the score is a vector field that points toward regions of higher probability, that is, back toward the data.
- Reverse process. Start from noise and follow the score backward in time. You can do this with random steps (the reverse SDE, or DDPM) or with deterministic steps (the probability-flow ODE, or DDIM). Flow matching is a close relative that trains on straight lines from data to noise.
A real model learns the score with a neural network. Here the data distribution is a mixture of small Gaussians around the data points, so the noised density and its score have a closed form. The score in every figure is exact, not learned. This lets you see the sampling methods themselves, without training errors, and see clearly why few sampling steps give blurry results.
1. The playground #
Move the time slider, or press Play. The blue dots are the data ; the noised points turn from blue to aqua as goes from 0 to 1. The blue shading is the noised density , and the violet arrows show the exact score, scaled as so that all arrows have a similar size (arrow length grows with the size, up to a cap). Then choose Reverse: sample. Particles start as noise at and move along magenta paths to samples at . Change the sampler and the number of steps ; the panel reports the distance of the samples to the data. Choose draw to click your own data set.
2. Paths through time #
Pick a process and change the number of steps. Here the data are one-dimensional, so we can draw time on the horizontal axis: data at on the left, noise at on the right. The shading is the exact density , scaled in each column. Magenta: particle paths. For the yellow dashed lines show where each step aims: the denoiser's guess on the left axis. On the far left, the orange histogram of 4000 final samples is compared with the data density. Compare DDIM and Flow matching with the same : the shading of flow matching spreads along straight lines.
3. Few steps or many #
Choose a sampler and a data set. The six panels show the final samples of the same noise points for steps, over the data. The chart shows the distance to the data (sliced Wasserstein-2, explained below) against for all four samplers; the chosen one is orange. The grey band is the distance between two sets of true samples of the same size: inside or below the band, the samples are as good as we can measure.
4. Try this #
- When is the shape gone? In the playground, choose two moons and Forward, and move slowly from 0 to 1. Question: at which can you no longer see two moons, and where do the arrows point at ? What to see: the schedule destroys the shape fast. At the signal scale is and the moons are clear; at , and , and the moons have merged into one cloud. At , , so the score is about : every arrow points to the origin.
- One step gives the average. Choose Reverse, the sampler DDIM and , then . Question: where do the samples land with one step? What to see: with all samples land on one point, the mean of the data (here the origin). With 2 and 4 steps the samples form a blurry cloud between the moons. With 16 steps they lie on the moons (the badge says close to the data). In Figure 2, choose 80 / 20, DDIM and : every path ends at , which is the mean .
- Same start, same end? Set DDIM with and press Sample again twice. Then do the same with DDPM. Question: which sampler is a fixed map from noise to data? What to see: DDIM draws exactly the same paths and samples again; DDPM draws new paths every time from the same starting noise, because it adds fresh noise at every step. In Figure 2 the ODE paths never cross; the SDE paths cross often.
- Too much noise. Choose Euler–Maruyama with , then , then , and compare with DDPM at the same . Question: both methods sample the same reverse SDE; why do they fail in different ways with few steps? What to see: at the Euler–Maruyama points fly out of the frame: the first step has , far too large for an explicit method. DDPM stays in the frame, because each step draws from an exact Gaussian; its error with few steps is blur. At the Euler–Maruyama samples are a noisy band around the moons, and the DDPM samples are still too smooth. At both are close to the data: the distance is near that of true samples. Figure 3 shows that Euler–Maruyama needs the most steps of the four samplers.
- Straight lines in training, curves in sampling. Choose Flow matching, turn on training pairs and sample with . Question: do the paths follow the grey dashed lines from noise to data? What to see: no. Each training pair is joined by a straight line, but these lines cross. The model learns the average velocity of all lines through a point, so the sampling paths bend and never cross. They are still straighter than the DDIM paths: compare both samplers at in Figure 3.
- The exact score copies the data. Choose draw and click five separate points. Set the kernel width to and sample with DDIM and . Question: does the model create new points? What to see: every sample lands on one of your five points. The exact score of a small data set reproduces the training data; this is memorization. Increase and the five points become five blobs. A trained network does not learn this exact score, and that is one reason why real diffusion models can create new data.
5. The math in short #
The forward process and DDPM #
DDPM (denoising diffusion probabilistic models, Ho et al. 2020) adds a little Gaussian noise in each of steps:
A sum of independent Gaussians is Gaussian, so we can jump to any step at once: with and . With many small steps this becomes the variance-preserving (VP) SDE of Song et al. (2021), with continuous time :
Here is Brownian motion. The figures use , so : at almost no signal is left. "Variance preserving" means that data with unit variance keep unit variance. The signal-to-noise ratio is .
DDPM trains a network to predict the noise that was added, with the loss . The best possible prediction is the conditional mean , and the next section shows that this is the score up to a factor.
The score and the denoiser #
The score of the noised density is . The denoiser is the best guess of the clean point (best in mean squared error). Tweedie's formula connects the two:
So "predict the noise", "predict the clean point" and "learn the score" are the same task.
The exact score of these figures. Each data set is a mixture : one small Gaussian of width (the kernel width) around each data point . Noising maps to with . With the responsibilities and their weighted mean :
Near the data (small ) one weight is close to 1, so the score points to the nearest data point. When the noise is large, all are about equal, is the mean of the data, and the score points toward , the scaled mean.
At the noised density is almost . What is the denoiser's guess for a point drawn there?
. At the point carries almost no information about (), so the conditional mean is almost the unconditional mean . The nearest data point would be the best guess only when the noise is small.
The reverse SDE and the probability-flow ODE #
For a forward SDE , the process run backward in time is again an SDE (Anderson 1982):
where is a Brownian motion in reverse time. Song et al. (2021) showed that the deterministic probability-flow ODE
has the same marginal densities for every . Both start from and end at . The SDE paths are random and cross each other; the ODE paths are smooth, never cross, and define a fixed, invertible map from noise to data. For the VP SDE, and .
The four samplers use the time grid and take steps, one score evaluation per step:
- Euler–Maruyama (reverse SDE): , with fresh .
- DDPM (reverse SDE): compute , then draw from the Gaussian of the forward process. The noise of each step then has the right size, even for large steps.
- DDIM (probability-flow ODE): with , set . This is the Euler method for the ODE written in the variables and .
- Flow matching (its own ODE, next section): .
You start DDIM and DDPM twice each from the same noise , with 50 steps. What do you get?
DDIM solves the probability-flow ODE, so the end point is a function of the start point. DDPM adds fresh noise at every step, so the path and the sample change even from the same start. Both have the right distribution of samples when is large.
Flow matching #
Flow matching (Lipman et al. 2023; also called rectified flow, Liu et al. 2023) uses the straight line between a data point and a noise point:
A network is trained with . The best solution is the average velocity of all pairs whose line passes through :
Sampling solves from to . The figures use the convention for data and for noise, as in DDPM and in Stable Diffusion 3; Lipman et al. use the opposite direction.
Two facts are useful. First, an Euler step moves on the straight line from toward the current guess , as the yellow lines in Figure 2 show. Second, this path is also a Gaussian path (with , ). With the exact score, its ODE and the probability-flow ODE of the VP SDE send the same noise point to (almost exactly) the same data point; only the path in between differs. The flow-matching path bends less, so Euler steps make smaller errors.
Flow matching is trained on straight lines from to . Are the sampling paths of the trained model straight?
many training lines with different directions pass through the same point at time . The model learns their average . The resulting paths are smooth curves that do not cross (an ODE), and they are straight only in special cases, for example when the data are a single point.
Why few steps blur the result #
Every DDPM, DDIM and flow-matching step moves toward the guess . This guess is an average over all clean points that could have produced . When the noise is large, many very different clean points are possible, so their average lies between them: between the two moons, or in the middle of the ring.
- With one step from , DDIM, DDPM and flow matching all return : every sample is the mean of the data.
- With a few steps, each step asks the denoiser again at a lower noise level. The guess then depends more on the current point, and the samples separate into the parts of the data. Too few steps leave them between the parts: the result is too smooth, it is blurred. For images, the average of many possible images is a blurry image.
- The ODE samplers have no noise to correct the errors, but their paths are smooth, so they need fewer steps. Euler–Maruyama has the opposite problem: with large steps it adds too much noise and its drift overshoots, so the samples are too noisy or fly away.
This is why fast samplers follow straighter paths (flow matching, rectified flow), use higher-order ODE solvers, or train a model to jump along the ODE in one step (distillation and consistency models).
Distances between point clouds
The panels report the sliced Wasserstein-2 distance between the samples and true samples of the same size . Project both sets onto a direction ; in one dimension the Wasserstein-2 distance pairs the sorted values: . The sliced distance averages over 24 evenly spaced directions and takes the square root. It sees both errors: samples that are too concentrated (blur) and samples that are too spread (noise). With a finite even two sets of true samples have a distance above 0; this is the grey band in Figure 3.
6. Learn more on this site #
- Generative Models: the lessons on DDPM, diffusion parameterizations, reverse SDE and PF-ODE samplers, flow matching, guidance and few-step generation (being written).
- Probability and Statistics: Gaussians, conditional expectation and Bayes' rule, the tools behind the denoiser (being written).
- Calculus, lesson 10: integrals, expectations and the change of variables: how a map moves a density, the idea behind the probability-flow ODE.
- Calculus, lesson 11: Gaussian integrals: why a noised Gaussian mixture is again a Gaussian mixture.
- Calculus, lesson 12: score-function and pathwise gradients: a different "score". There it is the gradient of with respect to the parameters ; here it is the gradient with respect to the point .
- Information Theory, lesson 3: the KL divergence: the divergence that maximum-likelihood training of generative models minimizes.
- Optimization, lesson 5: stochastic gradient descent: a noisy gradient step, the same kind of update as an Euler–Maruyama step of the reverse SDE.
- ML for Science: stochastic differential equations and generative models for weather and physics.