Skip to main content
A generative model moves probability mass from a simple density to the data density. The previous sections computed that motion exactly, from the densities. Flow matching asks how the motion can be learned from samples alone. Its answer is a learned velocity field. You start from a path of densities that you choose, find a field that moves mass along that path, and train a network to approximate the field. Sampling then means carrying Gaussian draws along the learned field, the way the wind carries raindrops in the velocity fields section. Flow matching is used for images, video, audio, speech and molecular structures. Meta’s Movie Gen, Stable Diffusion 3 and Flux are examples. This page states the general construction in dd dimensions. The 1D mixture section works through every step on a small example where all the quantities can be computed exactly.

Probability paths

Let p0p_0 be a simple source density on Rd\mathbb{R}^d, usually the standard Gaussian N(0,I)\mathcal{N}(0, I), and let p1=pdatap_1 = p_{\text{data}} be the data density. Time runs from noise at t=0t = 0 to data at t=1t = 1. A probability path is a family of densities ptp_t, one for each t∈[0,1]t \in [0, 1], that starts at p0p_0 and ends at p1p_1. Many paths connect the same two densities, and you choose one. A common choice draws x0∼p0x_0 \sim p_0 and x1∼p1x_1 \sim p_1 independently and moves along the straight line between them, xt=(1−t) x0+t x1.x_t = (1 - t)\, x_0 + t\, x_1 . The density of xtx_t defines ptp_t. You can sample from this path at any tt without knowing ptp_t in closed form. The training loss in the last section of this page relies on that.

The continuity equation

A path says where the mass is at each time. It does not say how the mass gets there. A velocity field vt:Rd→Rd,x↦vt(x),v_t: \mathbb{R}^d \to \mathbb{R}^d, \qquad x \mapsto v_t(x), gives that motion: mass at position xx and time tt moves with velocity vt(x)v_t(x). Probability mass is conserved, so the field and the path must satisfy the continuity equation ∂tpt+∇⋅(pt vt)=0.\partial_t p_t + \nabla \cdot \bigl( p_t \, v_t \bigr) = 0 . The product ptvtp_t v_t is the flux of mass. Where more flux leaves a point than enters it, the density there falls. The dynamic transport section derives this equation by counting the mass in an interval. When a pair (pt,vt)(p_t, v_t) satisfies it, the field vtv_t moves the path ptp_t. In 1D the continuity equation determines the field from the path. In d>1d > 1 it does not. You can add to vtv_t any field wtw_t with ∇⋅(ptwt)=0\nabla \cdot (p_t w_t) = 0, and the path stays the same. Many fields therefore move the same path, and flow matching picks one of them. For the straight-line path above it picks the conditional mean of the pair velocities, described in the last section of this page.

Particles and the flow

The field also moves individual samples. A particle at xtx_t follows the ordinary differential equation dxtdt=vt(xt),x0∼p0.\frac{d x_t}{d t} = v_t(x_t), \qquad x_0 \sim p_0 . For each starting point x0x_0 the ODE has one trajectory {xt}t∈[0,1]\{x_t\}_{t \in [0, 1]}, under mild smoothness conditions on vtv_t. The map ϕt(x0)=xt\phi_t(x_0) = x_t is the flow of the field. When vtv_t satisfies the continuity equation with ptp_t, pushing p0p_0 through the flow gives the path back: pt=(ϕt)♯ p0.p_t = (\phi_t)_\sharp \, p_0 . This is the sampling view of the same object. Draw x0x_0 from p0p_0, carry it along the field, and xtx_t is a sample from ptp_t. At t=1t = 1 it is a sample from p1=pdatap_1 = p_{\text{data}}. Only the starting points are Gaussian. All the randomness comes from the initial draw, and the ODE fixes the rest of each trajectory. The flow ϕ1\phi_1 is a transport map from p0p_0 to p1p_1. It is in general not the optimal transport map of the transport maps and couplings section. In 1D two trajectories of the ODE cannot cross, so ϕ1\phi_1 is increasing. An increasing map between two densities on the line is unique, so ϕ1\phi_1 is the optimal map. In d>1d > 1 there is no such ordering argument.

Generation as trajectory integration

Once a network vθ(x,t)v_\theta(x, t) approximates the field, you sample by integrating the ODE numerically. The simplest method is Euler integration with step size Δt\Delta t: xt+Δt=xt+Δt⋅vθ(xt,t).x_{t + \Delta t} = x_t + \Delta t \cdot v_\theta(x_t, t) . This is the same loop used for the storm example. At each step you evaluate the field at the current position and move a short distance in that direction. Start from x0∼N(0,I)x_0 \sim \mathcal{N}(0, I) and continue until t=1t = 1. The distribution of the result x1x_1 approximates pdatap_{\text{data}}. Higher-order solvers (Heun, RK4, adaptive Dormand-Prince) integrate the same field with fewer steps and lower truncation error. You can choose the solver independently of the field.

Views of a velocity field

A velocity field has three common representations:
  1. The vector field view draws the arrow vt(x)v_t(x) at each point xx and time tt. Quiver plots show this view.
  2. The streamline view fixes tt and traces the integral curves of vtv_t.
  3. The particle view releases a cloud of particles at t=0t = 0 and follows them as the field carries them. Trajectory plots show this view.
The same mathematics appears in fluid mechanics, dynamical systems and optical flow in computer vision.

The learned velocity field

For a training sample xtx_t on the straight-line path you know the velocity x1−x0x_1 - x_0 of its own pair. You do not know the field vt(xt)v_t(x_t). Many pairs pass through the same point at the same time with different velocities. Flow matching uses their average, vt(x)=E[X1−X0∣Xt=x].v_t(x) = \mathbb{E}\bigl[X_1 - X_0 \mid X_t = x\bigr] . Flow matching gets this average from least squares regression. For random variables XX and YY, the function that minimizes E[∥Y−f(X)∥2]\mathbb{E}\bigl[\lVert Y - f(X) \rVert^2\bigr] is the conditional mean E[Y∣X=x]\mathbb{E}[Y \mid X = x]. Take the input to be (Xt,t)(X_t, t) with tt uniform on [0,1][0, 1], and the target to be X1−X0X_1 - X_0. The loss L(θ)=E[∥vθ(Xt,t)−(X1−X0)∥2]L(\theta) = \mathbb{E}\Bigl[\bigl\lVert v_\theta(X_t, t) - (X_1 - X_0) \bigr\rVert^2\Bigr] then has the field vtv_t as its minimizer. You can compute every target in it from a sampled pair. Lipman et al. introduced this objective, and the references below cover it in detail. The loss does not reach zero. Write Ut=X1−X0U_t = X_1 - X_0. Then E[∥Ut−vθ(Xt,t)∥2]=E[∥Ut−vt(Xt)∥2]+E[∥vt(Xt)−vθ(Xt,t)∥2],\begin{aligned} \mathbb{E}\bigl[\lVert U_t - v_\theta(X_t, t)\rVert^2\bigr] &= \mathbb{E}\bigl[\lVert U_t - v_t(X_t)\rVert^2\bigr] \\ &\quad + \mathbb{E}\bigl[\lVert v_t(X_t) - v_\theta(X_t, t)\rVert^2\bigr], \end{aligned} with vt(x)=E[Ut∣Xt=x]v_t(x) = \mathbb{E}[U_t \mid X_t = x]. The first term does not depend on θ\theta. It is the variance of the pair velocities around their conditional mean, and it is the smallest value the loss can take. A coupling with less spread in UtU_t at each point, such as optimal pairs, lowers this floor, because fewer different velocities pass through the same (x,t)(x, t). The network regresses a vector-valued function of position and time. The 1D mixture section trains such a network, compares it with the exact field, and repeats the construction with couplings other than independent pairs.

Score matching on the same path

The score-based section trains a network on the same kind of path with a different regression target. Write a Gaussian path in general form as xt=at x1+bt x0,x0∼N(0,I),x_t = a_t\, x_1 + b_t\, x_0 , \qquad x_0 \sim \mathcal{N}(0, I), with a0=0a_0 = 0, b0=1b_0 = 1, a1=1a_1 = 1 and b1=0b_1 = 0. The straight line above is the case at=ta_t = t, bt=1−tb_t = 1 - t. The score-based section runs time the other way, with the data at t=0t = 0, and calls the noise sample ε\varepsilon. Only the labels differ. Both methods sample a pair, build xtx_t and regress a target computed from that pair. Flow matching regresses the time derivative of xtx_t, which is a˙tx1+b˙tx0\dot a_t x_1 + \dot b_t x_0, or x1−x0x_1 - x_0 on the straight line. Its minimizer is the velocity vtv_t. Denoising score matching regresses −x0/bt-x_0 / b_t, and its minimizer is the score st(x)=∇xlog⁡pt(x)s_t(x) = \nabla_x \log p_t(x). Block diagram. A box for the shared probability path x_t = a_t x_1 + b_t x_0 splits into two columns. Left column: score matching target minus x_0 over b_t, whose minimizer is the score s_t(x). Right column: flow matching target a-dot_t x_1 plus b-dot_t x_0, whose minimizer is the velocity v_t(x). Both columns join in a box for the same probability flow dx/dt = v_t(x). Block diagram. A box for the shared probability path x_t = a_t x_1 + b_t x_0 splits into two columns. Left column: score matching target minus x_0 over b_t, whose minimizer is the score s_t(x). Right column: flow matching target a-dot_t x_1 plus b-dot_t x_0, whose minimizer is the velocity v_t(x). Both columns join in a box for the same probability flow dx/dt = v_t(x). Both methods sample the same path and regress a target computed from one pair (x0,x1)(x_0, x_1). One least squares minimizer is the score and the other is the velocity, and either one gives the same probability flow ODE. The two minimizers carry the same information. Both are conditional means given Xt=xX_t = x. The score gives E[X0∣Xt=x]=−bt st(x)\mathbb{E}[X_0 \mid X_t = x] = -b_t\, s_t(x), and because x=at E[X1∣Xt=x]+bt E[X0∣Xt=x]x = a_t\, \mathbb{E}[X_1 \mid X_t = x] + b_t\, \mathbb{E}[X_0 \mid X_t = x], it also gives E[X1∣Xt=x]\mathbb{E}[X_1 \mid X_t = x] whenever at>0a_t > 0. Substituting both into vt(x)=E[a˙tX1+b˙tX0∣Xt=x]v_t(x) = \mathbb{E}[\dot a_t X_1 + \dot b_t X_0 \mid X_t = x] gives vt(x)=a˙tat x+(a˙t bt2at−b˙t bt)st(x).v_t(x) = \frac{\dot a_t}{a_t}\, x + \Bigl(\frac{\dot a_t\, b_t^2}{a_t} - \dot b_t\, b_t\Bigr) s_t(x) . On the straight line this is vt(x)=(x+(1−t) st(x))/tv_t(x) = \bigl(x + (1 - t)\, s_t(x)\bigr)/t. A trained score network therefore defines the same probability flow ODE as a trained velocity network, up to the error of each fit. What differs is the regression problem. The score target −x0/bt-x_0 / b_t has variance 1/bt21/b_t^2 per coordinate, which grows without bound near the data end, and the conversion to a velocity divides by ata_t, which vanishes at the noise end. On the straight line the flow matching target x1−x0x_1 - x_0 has the same distribution at every tt, and the network learns the velocity with no conversion.

References

PyTorch reference


Found an error on this page? Report an issue.