Skip to content

6.6 Flow matching

ready≈ 11.5 hoursNeeds: 6.5Unlocks: 6.7
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Flow-matching playground)
  4. 4 Do
  5. 5 Check

Skip the noise process. 6.5 defined a noising process and learned to reverse it. Flow matching asks for an ODE dx/dt = ut(x) that carries N(0, I) at t = 0 to the data at t = 1 (the reverse of 6.5’s DDPM convention, where x0 is data and t runs toward noise; 6.4 and the rest of Part 6 use this one), and builds it from simple pieces (Lipman et al. 2022, sections 2–4). For one data point x1, the straight path xt = t x1 + (1 − t) ε goes from noise to that point with constant velocity x1 − ε. That is a conditional path; there is one per data point.

From conditional to marginal. Many such lines pass through any point x at time t. Their average velocity, ut(x) = E[x1 − ε | xt = x], is the marginal velocity field, and following it moves the whole noise distribution onto the whole data distribution at every t (Holderrieth and Erives 2025, the sections on conditional and marginal probability paths). Rectified flow reached the same objective from the transport side (Liu, Gong and Liu 2022).

Learning it. We cannot compute that average for real data, but we do not need to. Regress the conditional velocity: draw t, ε and a data point, and minimise ‖vθ(xt, t) − (x1 − ε)‖². A squared loss is minimised by the conditional mean, so vθ learns ut (Lipman et al.’s Theorem 2, section 3): the same move as denoising score matching in 6.5. In fact for this path the score is (t ut(x) − x) / (1 − t), so the two models carry the same information. Sampling is 6.4’s Euler method, and because the paths are close to straight, few steps are needed.

Checking it. For a Gaussian mixture, ut has a closed form; the widget and tests use it, and the notebook then trains the same loss on MNIST with a small U-Net, as in the 6.S184 lab.

The marginal velocity, and why regressing the conditional one learns itstep 1 of 6
  1. xt=t x1+(1−t) ε,dxtdt=x1−εx_t = t\,x_1 + (1 - t)\,\varepsilon, \qquad \frac{d x_t}{dt} = x_1 - \varepsilon

★ Flow-matching playground

Conditional and marginal velocity fields, and ODE sampling

Static view on small screens. Open this page on a laptop to interact.

data show
30 paths that follow the marginal velocity u_t from noise (t = 0) to data (t = 1); dots: all 300 particles at time t. Dashed: 2 standard deviations of each blob
the marginal velocity ut(x) = E[x1 − ε | xt = x] at time 0.99: the average direction of every training line passing through x. This is what the network learns
after 20 Euler steps, share nearest each blob: 52% / 28% / 20% (true 50% / 30% / 20%) straightness of the marginal paths (start-to-end distance ÷ length): 0.795 the 30 drawn training lines cross each other 115 times; ODE paths never cross

Training only ever sees straight lines from noise to data, chosen at random, so they cross all over the place. At a crossing point the network cannot know which line it is on, and the squared loss teaches it the average direction: the marginal velocity on the right. Following that field is an ODE, and ODE paths never cross, so they bend instead, routing each region of the noise to a region of the data in the right proportions. Every path starts out towards the data's overall mean, because at t = 0 the field is E[x1] − x, and then turns towards its blob. With one Euler step every point lands exactly on that mean; about five steps already give the right shares and blob widths within about 10%.

Two square plots. The left one shows either 30 conditional training paths (straight lines from a noise point to an independent data point) or 30 paths that follow the exact marginal velocity from noise to data, plus 300 particles at time t, over dashed circles marking the data’s blobs. The right one draws the marginal velocity field at time t as arrows. Buttons choose the data (three blobs or a ring of eight), the view, and the solver (Euler or Heun), play t from 0 to 1, and draw new noise; sliders set t and the number of sampling steps. The readout gives the share of samples nearest each blob against the true weights, the straightness of the marginal paths, and how often the drawn training lines cross. Keyboard: Tab to the buttons and sliders.

Flow matching: edit exercises/stubs/m6_6_flow_matching.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 6.6
Hints · mixture_marginal_velocity 0/3 revealed
Hints · fm_loss 0/3 revealed
Hints · sample_ode 0/3 revealed

Then run the lab. Open notebooks/6.6-flow-matching.ipynb (a GPU helps, but a CPU finishes in about 15 minutes) and paste in your 6.6 file. It trains on the three-blob mixture and compares the learned velocity with the exact one, samples with Euler and Heun at 2 to 100 steps, then trains a small U-Net on MNIST with the same loss and checks the samples with a classifier.

1. The training targets x₁ − ε are straight-line velocities that cross each other. Why do the sampling paths not cross?
2. What does the flow matching loss converge to, if the network is flexible enough?
3. With one Euler step from t = 0, where does every sample land?
4. How is a flow matching model related to a diffusion model?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.