Flow Matching (FM)
It learns the velocity field of an ordinary differential equation that carries the noise distribution to the data distribution, by regressing directly on the velocities of simple probability paths (such as straight lines between noise and data), without simulating the equation during training.
Overview
| Name | Flow Matching |
|---|---|
| Year introduced | 2023 |
| Researchers | Yaron Lipman, Ricky T. Q. Chen, Heli Ben-Hamu, Maximilian Nickel, Matt Le |
| Institution | Meta AI / Weizmann Institute |
| Original paper | Flow matching for generative modeling arXiv |
| Data type | Images, Tabular data, Sequences and time series |
| Domains | Scientific computing, Generative AI, Computer vision, Audio and speech |
| Computational cost | One pass for training; tens of evaluations for generation. |
| Data size | As for diffusion. |
Problem it solves
Training continuous normalising flows (CNF) by maximum likelihood requires costly ODE simulation, and diffusion models need many generation steps because their paths are curved.
Background and evolution
What came before: Diffusion Models, Score-Based Generative Models, Neural ordinary differential equations
Core idea
We choose a simple conditional path between a noise sample $x_0$ and a data sample $x_1$, such as $x_t=(1-t)x_0+tx_1$, whose velocity is $x_1-x_0$. We train $v_\theta(x_t,t)$ by MSE regression on this velocity. The authors proved that the gradient of this conditional loss equals the gradient of the loss for the unknown marginal field. To generate, we solve $dx/dt=v_\theta$ from $t=0$ to 1.
Interactive diagram
A simplified illustrative diagram with the dimensions of a small example, not a drawing matching every detail of the original implementation. Click any layer to see its role, dimensions and mathematical operation.
Select a layer in the diagram.
Components
| Component | Role |
|---|---|
| Probability path p_t | Linear/OT or diffusion |
| Conditional velocity u_t | x₁ − x₀ for the linear path |
| Velocity network v_θ(x, t) | |
| ODE solver | Euler/RK at generation |
Equations
1. Conditional flow matching loss
| $u_t$ | Conditional velocity of the path |
2. The optimal-transport (OT) path in the paper
| $\sigma_{min}$ | A small deviation at t=1 |
3. Generation by solving the ODE
| $v_\theta$ | Learned velocity field |
| $t$ | Time from 0 (noise) to 1 (data) |
| $x(t)$ | Position of the sample on the path |
How it works, step by step
- Draw noise, data and t.
- Compute the point on the path and its velocity.
- Regress $v_\theta$ on the velocity.
- Generate by solving the ODE.
Strengths and limitations
Strengths
- Simple, simulation-free training.
- Often fewer generation steps than diffusion.
- A flexible framework for different paths.
Limitations
- The marginal paths are not perfectly straight even with linear conditioning.
- A newer framework whose practices are still evolving.
When to use it, and when not?
Good for
- An alternative to diffusion with faster generation.
- Transport between two general distributions.
Not suited for
- When ready-made diffusion tools are sufficient and required.
Applications
| Domain | Use |
|---|---|
| Generative AI | Image and video generation. |
| Audio and speech | Speech generation. |
| Scientific computing | Generating molecules and proteins. |
| Computer vision | Distribution translation. |
PyTorch code
A simplified teaching code sample that runs on small synthetic data, covering: imports, model definition, data, forward pass, loss, optimizer, training loop, evaluation and inference. It is not the paper's full implementation.
Evaluation here uses two simple metrics computed inside the example: the share of samples close to one of the eight modes, and the number of modes covered. FID is not used because it is specific to images. The example compares 5 and 50 Euler steps.
# Flow Matching with a linear path and an Euler ODE solve
# A simplified educational example in PyTorch (not the paper's full original implementation): small synthetic data,
# runs on the CPU in seconds. The numbers it prints belong to this example only.
# 1) Import the libraries
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.manual_seed(0)
# 2) Define the model
class VelocityNet(nn.Module):
"""v_θ(x_t, t): a velocity field that carries noise to data along a straight path."""
def __init__(self, h=128):
super().__init__()
self.net = nn.Sequential(nn.Linear(3, h), nn.SiLU(), nn.Linear(h, h), nn.SiLU(), nn.Linear(h, h), nn.SiLU(), nn.Linear(h, 2))
def forward(self, x, t):
return self.net(torch.cat([x, t[:, None]], 1))
model = VelocityNet()
# 3) Prepare the data
# Target distribution: a mixture of 8 Gaussians on a circle of radius 2 (a classic test of mode collapse)
CENTERS = torch.stack([torch.tensor([2 * torch.cos(torch.tensor(k * torch.pi / 4)), 2 * torch.sin(torch.tensor(k * torch.pi / 4))]) for k in range(8)])
def sample_ring(n, std=0.1):
k = torch.randint(0, 8, (n,))
return CENTERS[k] + std * torch.randn(n, 2), k
def ring_metrics(x):
"""Quality: share of samples close (<0.3) to one of the centres. Coverage: number of modes that received ≥5% of the good samples."""
d = torch.cdist(x, CENTERS)
near, k = d.min(1)
good = near < 0.3
counts = torch.bincount(k[good], minlength=8).float()
covered = int((counts >= 0.05 * max(1, good.sum().item())).sum())
return good.float().mean().item(), covered
# 4) Conditional flow matching loss (linear path, σ_min = 0): || v_θ(x_t, t) − (x_1 − x_0) ||²
opt = torch.optim.Adam(model.parameters(), lr=2e-3)
# 5) Training loop: no ODE simulation during training, just regression on the target velocity
STEPS, BATCH = 4000, 256
for step in range(STEPS):
x1, _ = sample_ring(BATCH) # data
x0 = torch.randn_like(x1) # noise
t = torch.rand(BATCH)
xt = (1 - t[:, None]) * x0 + t[:, None] * x1 # a point on the straight path
loss = F.mse_loss(model(xt, t), x1 - x0) # Forward Pass
opt.zero_grad(); loss.backward(); opt.step()
if step % 1000 == 0 or step == STEPS - 1:
print(f"step {step:4d} loss={loss.item():.4f}")
# 6) Evaluation: solve dx/dt = v_θ(x, t) with Euler's method from t=0 to 1
@torch.no_grad()
def sample(n, steps=50):
x = torch.randn(n, 2)
for k in range(steps):
t = torch.full((n,), k / steps)
x = x + model(x, t) / steps
return x
for s in (5, 50):
q, cov = ring_metrics(sample(4000, s))
print(f"Euler steps={s:2d}: sample quality (near a mode) = {q:.3f} ; modes covered = {cov}/8")
# 7) Inference
print("generated point:", [round(v, 3) for v in sample(1)[0].tolist()])
This code was run on 2026-11-20 in 9.5 seconds (PyTorch (CPU), Python 3 – local build machine).
Sample output:
Euler steps= 5: sample quality (near a mode) = 0.754 ; modes covered = 8/8 Euler steps=50: sample quality (near a mode) = 0.936 ; modes covered = 8/8
Trainable parameters in the example: model = 33,794.
These results are for small synthetic data and few epochs, and only prove that the code runs; do not compare them with published results.
Try it and go deeper
Related architectures
Diffusion Models
Diffusion Models (DDPM)
They gradually corrupt the data by adding Gaussian noise over T steps, then a network learns to reverse this process by predicting the added noise, so a new example is g…
Score-Based Generative Models
Score-Based Generative Models (SGM)
They learn a "score" network _x p(x) at multiple noise levels, then generate samples with Langevin dynamics that follow the score from noise towards high-density regions…
Neural ordinary differential equations
Neural Ordinary Differential Equations (Neural ODE)
The hidden state is defined by a continuous differential equation whose derivative is given by a neural network, and the output is computed with a numerical solver, so t…
Latent Diffusion Models
Latent Diffusion Models (LDM)
They apply diffusion in a compressed latent space from a pretrained autoencoder instead of pixel space, with a cross-attention mechanism for feeding in conditions (text,…
No results match the filters.
Research ideas
- Flow matching for generating synthetic medical time series, with an evaluation of privacy and utility.
Starting suggestions, not confirmed research gaps; review the recent literature before adopting them.
Test your understanding
What target does the network regress on in flow matching with a linear path?
Why? The derivative of $x_t$ with respect to t.
Does training require simulating the differential equation?
Why? This is its main advantage over classic CNFs.
With $x_0=(1,1)$, $x_1=(3,1)$ and $t=0.25$, what is $x_t$?
Why? $0.75x_0+0.25x_1$.
References
Lipman, Y., Chen, R. T. Q., Ben-Hamu, H., Nickel, M., & Le, M. (2023). Flow matching for generative modeling. International Conference on Learning Representations (ICLR). https://arxiv.org/abs/2210.02747
Liu, X., Gong, C., & Liu, Q. (2023). Flow straight and fast: Learning to generate and transfer data with rectified flow. International Conference on Learning Representations (ICLR). https://arxiv.org/abs/2209.03003
Chen, R. T. Q., Rubanova, Y., Bettencourt, J., & Duvenaud, D. (2018). Neural ordinary differential equations. Advances in Neural Information Processing Systems (NeurIPS) 31. https://arxiv.org/abs/1806.07366
Song, Y., Sohl-Dickstein, J., Kingma, D. P., Kumar, A., Ermon, S., & Poole, B. (2021). Score-based generative modeling through stochastic differential equations. International Conference on Learning Representations (ICLR). https://arxiv.org/abs/2011.13456
Ho, J., Jain, A., & Abbeel, P. (2020). Denoising diffusion probabilistic models. Advances in Neural Information Processing Systems (NeurIPS) 33. https://arxiv.org/abs/2006.11239
