Skip to content
中文

Diffusion and flow: learn a path from noise

An interactive field guide

A generative model can produce an image through many small corrections. The training target determines what each correction means.

Before this article: Probability and tensors · Autoencoders

An AE maps one input to one compact code, then decodes it. A probabilistic generator must also describe uncertainty: many images can satisfy the same class or text. Denoising models learn local predictions at different noise levels rather than mapping a single code directly to final pixels.

DDPM adds Gaussian noise through a forward process and learns reverse transitions. Flow matching instead regresses a velocity field along chosen probability paths. They are related families, but their targets and solvers are not interchangeable. Here we use a straight interpolation as a fully computable flow example.

Read from left to right. Select a module to connect its role, tensor shape, and calculation. Snippets are teaching pseudocode. Sources provide the paper method or audited implementation.

Construct a noisy pair

The target is known because training has the clean image.

xt = (1-t)*x + t*noise
target = noise - x

Learn a vector field

Condition c can be a class, text, or a reconstruction code.

prediction = model(xt, t, condition)
loss = mean((prediction-target)**2)

Integrate backwards

Use decreasing times for this convention.

state = noise
for t, t_next in schedule:
    state += (t_next-t)*model(state,t,condition)

Inspect the pinned source

xt=(1−t)x+tϵ,v∗=ϵ−x,xt+Δt≈xt+Δt vθ(xt,t,c)x_t=(1-t)x+t\epsilon,\quad v^*=\epsilon-x,\quad x_{t+\Delta t}\approx x_t+\Delta t\,v_\theta(x_t,t,c)

Set clean value x=2 and noise ε=−1. Our convention is t=0 for data and t=1 for noise. Then xₜ=(1−t)x+tε=2−3t and target velocity ε−x=−3. To generate, move time backwards: x at the next time equals the current x plus a negative time step multiplied by predicted velocity.

At t=0.5, xₜ=0.5. With five remaining Euler steps, Δt=−0.1, so the next value is 0.8. The demo knows the true constant velocity. A trained network sees noisy input and conditions, not the clean endpoint; its estimate can be wrong. More steps therefore have no quality benefit in this exact straight-line example.

Change a parameter, inspect the computed values, then compare this small example with the model described above.

Analytical teaching model · controls change the example, not a trained research model.

x = 2, t = 0ε = −1, t = 1

The straight path has known velocity. A learned model must estimate velocity; this demo does not measure its error.

Computed values for the current example
QuantityValue
t0.500
x_t = (1 − t) × 2 + t × (−1)0.500
v = ε − x-3
Δt = −t / steps-0.100
One reverse update0.800

Training samples x, ε and t, constructs xₜ, and regresses the known target with squared error. It need not run the complete sampling trajectory for each training example. Sampling starts from noise and repeatedly calls the learned network. Count these calls as number of function evaluations, or NFE. A stochastic diffusion sampler and a deterministic ODE solver require different update rules.

A model may predict noise, clean data, velocity, or a score. Conversion needs the matching schedule and time convention. Copying a velocity target from one implementation and an update sign from another can reverse the process. In a reconstruction decoder, the compact image code is an extra condition; in unconditional generation there is no observed input image.

Open the derivation

Differentiate the constructed straight path with respect to t while holding the sampled endpoints fixed. This gives the supervised velocity target. A learned field averages the conditional targets compatible with its input and conditions.

ddt[(1−t)x+tϵ]=ϵ−x\frac{d}{dt}[(1-t)x+t\epsilon]=\epsilon-x

Euler approximates an integral over a short interval by the current velocity times the interval. With decreasing time, Δt is negative. For the constant-velocity example, the approximation is exact; in a learned nonlinear field it generally is not.

x(t+Δt)−x(t)=∫tt+Δtv(x(s),s) ds≈Δt v(x(t),t)x(t+\Delta t)-x(t)=\int_t^{t+\Delta t}v(x(s),s)\,ds\approx\Delta t\,v(x(t),t)
Why can training use one sampled time while generation uses many steps?

The target velocity is known for a constructed training pair at any sampled time. At generation, the unknown data endpoint must be reached by integrating the learned field.

Return to the learning map

Diagrams and snippets are teaching reconstructions. Paper reports, implementation observations, and analytical examples are distinguished. No research model runs in this page.