Why robots predict trajectories, not commands
Flow matching is a generative modeling method that learns to transform noise into data along a continuous path. Flow matching was originally introduced in 2023 and has ignited subsequent work, including Discrete Flow Matching and MeanFlow. Current production models that use flow matching include Stable Diffusion 3 and FLUX.
Leveling and Prerequisite Knowledge: This targets advanced undergraduates. Familiarity with Python, exposure to supervised learning are recommended.
Original Flow Matching Paper:
Subsequent Work:
We provide an interactive tutorial (self-contained HTML/JS + markdown source) in which every model trains live in the browser in under a minute. A 2D underwater robot must swim over or under a coral obstacle, a minimal task with two valid solution modes, demonstrated 200 times by an expert. Eight sections, each paired with an interactive widget: (1) the reef world and demonstration dataset; (2) a regression policy that averages the modes and collides; (3) chunked regression, which still averages, motivating distributions over trajectories; (4) the flow matching objective, with its full training loss in ~7 lines of PyTorch; (5) training, watching sampled chunks sharpen from noise into two route bundles across checkpoints; (6) sampling as Euler ODE integration, visualized as a school of samples splitting at the fork, with a step-count slider; (7) the policy in the loop, committing to one route per run; (8) physical execution: open-loop drift under a current field versus replanning, recovery-augmented data, chunk horizon versus inference latency. Complete source (markdown + JS) is included.
A robot task often has more than one correct answer. A fish can pass a coral over the top or underneath. Train a policy on both by regression and you get the average of the two, which is a path into the coral. Flow matching is the fix most current robot policies use: it samples a whole chunk of future actions from a distribution instead of computing one action from a formula.
The robot is a clownfish, in a side view of a reef. It starts at an anemone on the left. Home is a cave on the right. A table coral sits between them at cruising depth. Every policy here trained in under a minute and runs live in your browser.
An expert swam the route 200 times, 100 over the coral and 100 under. Each demonstration is 40 positions, one per control tick, and the action at each tick is the displacement to the next position. This is the whole dataset.
The slider adds a current that pushes the fish toward the seabed, strongest above the coral. Leave it off until the physical execution section.
The simplest policy is a small MLP from state to next action, trained with mean squared error on every tick of every demonstration. The state is the fish's position and its last commanded action. Training loss goes to nearly zero.
The arrows are the predicted action at each point for a fish cruising straight ahead. Above the centre line they follow the experts who went over, and below it the experts who went under. On the centre line the two cancel, and the fish swims into the coral.
Regression is doing its job. Where half the data says up and half says down, the prediction with the lowest squared error is the average, and the average of two valid actions is not a valid action. Real robot data has this everywhere because people solve the same task in different ways. Drag the fish to a start above or below the centre line, where the demonstrations agree, and the policy does fine.
Switch the policy to "Chunk of 8". The network still maps state to output by regression, but the output is the next eight actions. The white path is the chunk predicted at the last replanning point, and the readout gives its clearance from the coral. At the fork it is the average of an over-chunk and an under-chunk, a straight line at the coral's edge. Some runs scrape past, depending on which way the average leans by a few centimetres, but the plan is one no expert swam.
What the task needs is a distribution rather than a function: given this state, one draw from the set of valid futures. That is what generative models provide.
Generative models start from a Gaussian and learn a transformation that carries its samples onto the data. Diffusion learns that transformation by reversing a gradual noising process, which works for action chunks (Diffusion Policy, 2023) but needs a noise schedule and many denoising steps. Flow matching learns the velocity field from noise to data directly.
Take an expert chunk x₁ and a noise chunk x₀ of the same size, and move along the straight line between them:
x_t = (1 − t) · x₀ + t · x₁ for t in [0, 1]
The velocity along this line is constant, x₁ − x₀. Train a network v(x_t, t, state) to predict it:
def fm_loss(policy, state, chunk):
x1 = chunk # expert actions, flattened (B, H*2)
x0 = torch.randn_like(x1) # noise of the same shape
t = torch.rand(len(x1), 1) # one time per example
xt = (1 - t) * x0 + t * x1 # point on the line
v = policy(torch.cat([xt, t, state], 1)) # predicted velocity
return ((v - (x1 - x0)) ** 2).mean() # regress onto the true velocity
That is the whole training loop.
Many noise-data pairs pass through the same x_t at the same t, each with its own velocity, and the network learns their average. The flow matching theorem (Lipman et al. 2022) says that following the averaged field still transports the noise distribution onto the data distribution exactly. Averaging velocities is harmless. Averaging actions, which regression did, was the problem.
The flow policy has the same hidden layers as the regressor and trains on the same 200 demonstrations for about ten seconds. The widget samples 40 chunks at the fork from the network at each saved checkpoint.
At step 0 the chunks are noise. After a few hundred steps they are smooth paths, still blurred near the centre. By the end they form two bundles, over and under, with almost nothing between.
Generating a chunk is numerical integration: draw x₀ from the Gaussian and move it along the learned velocity from t = 0 to t = 1 in a fixed number of Euler steps.
def sample(policy, state, steps=10):
x = torch.randn(1, H * 2)
for k in range(steps):
t = torch.full((1, 1), k / steps)
x = x + policy(torch.cat([x, t, state], 1)) / steps
return x
The step count is a deployment setting. Because the training paths were straight, the learned flow is nearly straight and a few steps are usually enough. Fewer network calls per chunk means lower latency, which is why robotics adopted flow matching over diffusion.
With one step, every chunk moves by the velocity predicted at t = 0, where the network cannot yet tell which mode the noise will become, and the samples land near the average chunk. Two steps already separate the school into two bundles. Five looks clean, and 20 or 50 look the same as five. Drag the fish: the school only splits at the fork, where the experts disagree.
A policy is a sampler in a loop: observe the state, sample a chunk, execute some of its actions, observe again. The default below executes 4 actions of each 8-action chunk.
The regression policy is deterministic and ends in the coral. The flow policy commits to over or under on each run. With one Euler step about one run in five fails, since a one-step sample is the blurred average. With replanning every 8, where each chunk executes in full, it still works, because a chunk is one coherent path rather than eight independent guesses. That is what the title means: a sampled chunk has already decided which way round the coral it is going.
Turn the current on. It pushes the fish toward the seabed between control ticks, whatever the policy commands.
With the defaults each chunk executes in full, and the fish is open loop for 8 ticks at a time. Fish that go under mostly survive. Fish that go over get pushed onto the coral, and at a current of 0.06 about 4 runs in 10 end there. Replanning every 4, 2 or 1 ticks lets the policy correct the drift, and about 9 in 10 get home.
At stronger currents the clean policy stops improving, because the expert never demonstrated being pushed off course. "Demos plus recoveries" adds 300 demonstrations where the expert starts a little off the route and swims back (faint purple). At a current of 0.08 with replanning every tick, the clean policy brings about two thirds of the fish home and the recovery-trained one about five in six. Real datasets include recoveries too, because operators make mistakes and correct them.
Two hardware facts interact with this:
Latency is in control ticks, and a faster control loop makes the same network look slower. With horizon 4 and latency 2 the fish executes 2 fresh actions per chunk. With latency 4 the chunk runs out before its replacement arrives and the fish drifts. With latency 8 every action is stale on arrival and the fish never moves. Horizon 16 covers all of these and pays with 16 ticks of open-loop drift. π₀ makes this trade with 50 actions per chunk at 50 Hz from 10 Euler steps.
This simulation has no actuator dynamics. A real thruster responds about 100 ms after it is commanded, and a stiff controller turns chunk boundaries into jerk. Both are more reasons to predict a trajectory rather than a command.