Part V · 第 19 章 生成模型 · 19.7Part V · Ch. 19 Generative Models · 19.7

Flow Matching(流匹配)Flow Matching

让每一粒噪声沿一条直线匀速走向一个数据点,再训练一个神经网络去学“经过这里的所有直线的平均速度”:这就是 flow matching。训练只是一个普通的回归,不需要积分 ODE,也不需要噪声调度表;生成时从高斯噪声出发,沿学到的速度场积分几步就得到样本。本节从 19.3 节的连续 flow 和 19.6 节的扩散模型出发,先看概率路径和“取平均”这个核心技巧,在浏览器里现场训练一个速度场,再看为什么步数少了会糊、怎样用最优传输和 reflow 把路径拉直,最后搭一个 π0 风格的 flow matching 动作头,让它生成能绕开障碍物的多模态动作块。 Let every grain of noise travel at constant speed along a straight line to a data point, then train a neural network to learn "the average velocity of all the lines passing through here": that is flow matching. Training is plain regression, with no ODE to integrate and no noise schedule to tune; generation starts from Gaussian noise and integrates the learned velocity field for a few steps. Building on the continuous flows of Lesson 19.3 and the diffusion models of Lesson 19.6, this lesson looks at probability paths and the central "take the average" trick, trains a velocity field live in your browser, shows why too few steps blur the samples and how optimal transport and reflow straighten the paths, and ends with a π0-style flow-matching action head that generates multimodal action chunks around an obstacle.

约 45 分钟~45 min 前置:2.5 常微分方程 · 3.6 向量场与流 · 19.3 Normalizing Flow · 19.6 扩散模型Prereqs: 2.5 ODEs · 3.6 Vector fields & flows · 19.3 Normalizing flows · 19.6 Diffusion 5 个交互5 interactives

    1为什么机器人需要它Why robots need this

    机器人策略要输出的往往不是“一个动作”,而是“动作的分布”。同一个场景下,示教者有时从左边绕开障碍,有时从右边;同一个杯子可以从好几个方向抓。19.1 节已经看到,用均方误差回归动作会学到两个模式的平均,正好撞上障碍物。所以我们需要一个能表达多峰分布、又能快速采样的生成模型。

    19.6 节的扩散模型能做到前一半:它把数据一步步加噪成高斯,再学习如何一步步去噪,样本质量很好,Diffusion Policy(22.2 节)就是这样生成动作的。但机器人有一个很硬的约束:时间。一个以 50 Hz 运行的控制器,每 20 ms 就要给出下一个动作;如果采样一次要几十到上百次网络前向,就很难赶上。19.3 节的连续 flow 是另一条路:沿一个向量场积分 ODE 就能采样,可是按最大似然训练它,每一步训练都要把 ODE 积分一遍。

    Flow matching(Lipman 等,2023;同期的 rectified flow 与 stochastic interpolants 是同一类想法)把两者的优点拼在一起:它学的也是一个把噪声“流”成数据的速度场 \(\mathbf v_\theta(\mathbf x,t)\),但训练时不积分,只是对一个随手就能写出来的目标做回归;而且这个目标来自直线路径,所以通常几步积分就够了。π0 这类视觉-语言-动作模型的动作头用的就是它:每次推理只积分 10 步,就生成一整段 50 步的动作块。

    What a robot policy must output is often not "an action" but "a distribution over actions". In the same scene a demonstrator sometimes passes an obstacle on the left and sometimes on the right; the same cup can be grasped from several directions. Lesson 19.1 already showed that regressing actions with a mean squared error learns the average of the two modes, which drives straight into the obstacle. So we need a generative model that can represent multi-peaked distributions and can also sample fast.

    The diffusion models of Lesson 19.6 do the first half: they add noise to the data step by step until it is Gaussian and learn to remove it step by step; sample quality is excellent, and Diffusion Policy (Lesson 22.2) generates actions exactly this way. But robots face a hard constraint: time. A controller running at 50 Hz must produce its next action every 20 ms, and a sampler that needs tens to hundreds of network passes struggles to keep up. The continuous flows of Lesson 19.3 are another road: sampling means integrating an ODE along a vector field, but training them by maximum likelihood integrates the ODE at every training step.

    Flow matching (Lipman et al., 2023; the concurrent rectified flow and stochastic interpolants are the same family of ideas) combines the strengths of both. It also learns a velocity field \(\mathbf v_\theta(\mathbf x,t)\) that "flows" noise into data, but training never integrates anything: it is regression onto a target you can write down directly. And because that target comes from straight paths, a few integration steps are usually enough. The action heads of vision-language-action models such as π0 use exactly this: each inference integrates only 10 steps and produces a whole chunk of 50 future actions.

    2直觉:直线连起来,速度取平均Intuition: join with straight lines, average the velocities

    3.6 节把向量场想成一条河:撒一把沙子,每粒沙子顺着水流漂。现在我们想设计一条河,让 \(t=0\) 时撒下的一团高斯沙子 \(p_0=\mathcal N(\mathbf 0,I)\),到 \(t=1\) 时恰好堆成数据的形状 \(q\)。最笨也最直接的办法:随机配对,每粒噪声 \(\mathbf x_0\sim p_0\) 配一个数据点 \(\mathbf x_1\sim q\),让它沿直线匀速走过去:

    Lesson 3.6 pictured a vector field as a river: sprinkle sand and each grain drifts with the current. Now we want to design a river so that a Gaussian cloud of sand sprinkled at \(t=0\), \(p_0=\mathcal N(\mathbf 0,I)\), has piled up into the shape of the data \(q\) by \(t=1\). The bluntest, most direct recipe: pair at random, give each grain of noise \(\mathbf x_0\sim p_0\) a data point \(\mathbf x_1\sim q\), and let it walk there along a straight line at constant speed:

    $$ \mathbf x_t=(1-t)\,\mathbf x_0+t\,\mathbf x_1,\qquad \frac{d\mathbf x_t}{dt}=\hl{cond}{\mathbf x_1-\mathbf x_0},\qquad t\in[0,1]. $$
    时间方向的约定(与 19.6 节相反)Time convention (the reverse of Lesson 19.6)

    本节沿用 Lipman 等和 π0 的写法:时间从噪声走向数据,\(t=0\) 是噪声,\(t=1\) 是数据;\(\mathbf x_0\) 是噪声,\(\mathbf x_1\) 是数据,生成时从 \(t=0\) 往前积分到 \(t=1\)。19.6 节的 DDPM 正好相反:\(t\) 数的是加噪步数,\(t=0\) 是数据 \(\mathbf x_0\),\(t=T\) 是噪声,生成时 \(t\) 倒着走。对照表:本节的 \(\mathbf x_0\) 对应 19.6 节的噪声 \(\boldsymbol\varepsilon\)(约等于 \(\mathbf x_T\)),本节的 \(\mathbf x_1\) 对应 19.6 节的 \(\mathbf x_0\),本节的 \(t\) 大致对应 19.6 节的 \(1-t/T\)。

    This lesson follows Lipman et al. and π0: time runs from noise to data, \(t=0\) is the noise and \(t=1\) the data; \(\mathbf x_0\) is the noise, \(\mathbf x_1\) the data, and generation integrates forwards from \(t=0\) to \(t=1\). The DDPM of Lesson 19.6 is the other way round: \(t\) counts noising steps, \(t=0\) is the data \(\mathbf x_0\), \(t=T\) the noise, and generation runs \(t\) backwards. To translate: this lesson's \(\mathbf x_0\) is 19.6's noise \(\boldsymbol\varepsilon\) (roughly \(\mathbf x_T\)), this lesson's \(\mathbf x_1\) is 19.6's \(\mathbf x_0\), and this lesson's \(t\) corresponds roughly to 19.6's \(1-t/T\).

    在每个时刻 \(t\),所有沙粒的位置构成一个分布 \(\hl{pt}{p_t}\),从 \(p_0\)(高斯)连续地变到 \(p_1=q\)(数据)。这一串分布叫概率路径。问题在于:这些直线会交叉。两条直线在同一个时刻穿过同一点,却要朝不同方向走,而一个向量场在一个点只能给出一个速度。解决办法就是本节的核心:在每个点 \((\mathbf x,t)\),取所有经过这里的直线速度的平均,

    At each time \(t\) the positions of all grains form a distribution \(\hl{pt}{p_t}\) that moves continuously from \(p_0\) (the Gaussian) to \(p_1=q\) (the data). This family of distributions is called a probability path. The trouble is that the lines cross. Two lines pass through the same point at the same time heading in different directions, yet a vector field can give only one velocity per point. The fix is the heart of this lesson: at every point \((\mathbf x,t)\), take the average of the velocities of all lines that pass through it,

    $$ \hl{marg}{\mathbf u_t(\mathbf x)}=\mathbb E\big[\,\mathbf x_1-\mathbf x_0\;\big|\;\mathbf x_t=\mathbf x\,\big]=\int \frac{\mathbf x_1-\mathbf x}{1-t}\;\hl{post}{p(\mathbf x_1\mid \mathbf x_t=\mathbf x)}\;d\mathbf x_1 . $$

    第二个等号用了 \(\mathbf x_0=(\mathbf x-t\mathbf x_1)/(1-t)\):知道了现在的位置和终点,起点就确定了。\(p(\mathbf x_1\mid\mathbf x_t=\mathbf x)\) 是“一粒此刻在 \(\mathbf x\) 的沙子,终点是哪里”的后验。一个不显然、却是本节最重要的事实是:沿这个平均速度场漂流,整团沙子在每个时刻的分布仍然恰好是 \(p_t\)(第 5 节证明)。单粒沙子的路线变了:它们不再走直线,而是走一条弯曲、互不交叉的曲线,但整体的“人群密度”与直线配对时一模一样。下图在一维里把两者画在一起。横轴是时间,纵轴是位置。

    The second equality uses \(\mathbf x_0=(\mathbf x-t\mathbf x_1)/(1-t)\): once you know where a grain is now and where it ends, its start is fixed. \(p(\mathbf x_1\mid\mathbf x_t=\mathbf x)\) is the posterior "where does a grain that is at \(\mathbf x\) right now end up". The non-obvious fact, and the most important one of this lesson, is this: drifting along this averaged field moves the whole cloud so that its distribution at every time is still exactly \(p_t\) (proved in Section 5). Individual grains take different routes: no longer straight lines but curved paths that never cross, yet the overall "crowd density" is identical to that of the straight-line pairing. The figure puts both side by side in one dimension: time runs horizontally, position vertically.

    FIG 1一维的概率路径:直线会交叉,平均速度的流不会A probability path in 1-D: straight lines cross, the flow of the averaged velocity does not
    底色是 \(p_t(x)\):越青 = 越高(每一列按自己的最大值缩放,所以早期很宽的高斯和末端很窄的峰都看得见)。左边缘的蓝色曲线是 \(p_0=\mathcal N(0,1)\),右边缘的琥珀色曲线是数据分布 \(q\):两个高斯峰,标准差 0.3,琥珀色圆圈可上下拖动以移动峰的位置,滑块调峰 1(起初在下方的那个)的权重 \(\pi_1\)。紫色直线是 60 对随机配对 \((x_0,x_1)\) 的直线路径;蓝色曲线是沿精确的平均速度场 \(u_t(x)\) 积分出的 25 条轨迹(起点取在 \(p_0\) 的等概率分位上)。绿色探针可拖动:从它出发、通到每个峰的紫色线段(粗细 ∝ 后验权重),琥珀色箭头是它们的加权平均 \(u_t(x)\)。左边缘的空心圆是分界点 \(x_0^*=\Phi^{-1}(\pi_{\downarrow})\),\(\pi_{\downarrow}\) 是当前位于下方那个峰的权重,从它出发的轨迹加粗:它以下的沙子都流进下方的峰。 The shading is \(p_t(x)\): more cyan = higher (each column is scaled to its own maximum, so both the wide Gaussian early on and the narrow peaks at the end show up). The blue curve on the left edge is \(p_0=\mathcal N(0,1)\); the amber curve on the right edge is the data distribution \(q\): two Gaussian peaks with standard deviation 0.3. Drag the amber circles up and down to move the peaks; the slider sets the weight \(\pi_1\) of peak 1 (the one that starts lower). The purple lines are the straight paths of 60 random pairs \((x_0,x_1)\); the blue curves are 25 trajectories integrated along the exact averaged field \(u_t(x)\) (starting at equal-probability quantiles of \(p_0\)). The green probe is draggable: the purple segments from it run to each peak (thickness ∝ posterior weight), and the amber arrow is their weighted average \(u_t(x)\). The hollow circle on the left edge is the split point \(x_0^*=\Phi^{-1}(\pi_{\downarrow})\), where \(\pi_{\downarrow}\) is the weight of whichever peak is currently lower, and the trajectory starting there is drawn thicker: all sand below it flows into the lower peak.
    试一试Try this
    • 把绿色探针拖到 \(t\ge0.35\) 的某处,使两个峰的后验权重都至少是 0.25(读数里的 \(w_1,w_2\)):两条紫色线段指向相反的方向,琥珀色的平均速度既不指向上峰也不指向下峰,而是指向两峰之间。Drag the green probe to a place with \(t\ge0.35\) where both peaks have a posterior weight of at least 0.25 (\(w_1,w_2\) in the readout): the two purple segments point in opposite directions, and the amber average points at neither peak but between them.
    • 在显示蓝色轨迹时(“蓝色轨迹”或默认的“两者”),拖动琥珀色圆圈,让原来在下方的峰移到另一个峰的上方。紫色直线照样交叉,蓝色轨迹却重新排好了队,始终互不交叉:一维里,平均速度的流把 \(p_0\) 的分位点一一送到 \(q\) 的同一分位点,这正是 4.9 节的逆 CDF 采样。With the blue curves on screen ("blue curves" or the default "both"), drag the amber circles until the peak that was lower sits above the other one. The purple lines still cross, but the blue trajectories re-sort themselves and never cross: in 1-D the flow of the averaged velocity sends each quantile of \(p_0\) to the same quantile of \(q\), which is exactly the inverse-CDF sampling of Lesson 4.9.
    核心洞见Key insight

    平均速度 \(\mathbf u_t(\mathbf x)\) 没法直接算,因为后验 \(p(\mathbf x_1\mid\mathbf x_t)\) 要对整个数据集求和。但我们不需要算它:16.2 节说过,用平方误差回归一个带噪声的目标,学到的是目标的条件均值。所以只要随机抽 \((\mathbf x_0,\mathbf x_1,t)\),让网络在 \(\mathbf x_t\) 处回归那条直线的速度 \(\mathbf x_1-\mathbf x_0\),它学到的自然就是 \(\mathbb E[\mathbf x_1-\mathbf x_0\mid\mathbf x_t]=\mathbf u_t\)。每个训练目标都“错”(它只是一条直线的速度),平均起来却恰好对。这就是 flow matching 的全部秘密。

    The averaged velocity \(\mathbf u_t(\mathbf x)\) cannot be computed directly, because the posterior \(p(\mathbf x_1\mid\mathbf x_t)\) sums over the whole dataset. But we never need to compute it: as Lesson 16.2 showed, regressing a noisy target with a squared error learns the target's conditional mean. So draw \((\mathbf x_0,\mathbf x_1,t)\) at random and let the network, at \(\mathbf x_t\), regress the velocity of that one line, \(\mathbf x_1-\mathbf x_0\); what it learns is automatically \(\mathbb E[\mathbf x_1-\mathbf x_0\mid\mathbf x_t]=\mathbf u_t\). Every single training target is "wrong" (it is only one line's velocity), yet on average they are exactly right. That is the whole secret of flow matching.

    3动手:现场训练一个速度场Play: train a velocity field live

    把上面的配方原样写成代码,就是条件 flow matching(CFM)的训练循环。每一步:抽一批噪声 \(\mathbf x_0\sim\mathcal N(\mathbf 0,I)\)、一批数据 \(\mathbf x_1\sim q\) 和时间 \(t\sim\mathcal U[0,1]\),算出 \(\mathbf x_t\),让网络在那里预测速度,与直线速度 \(\mathbf x_1-\mathbf x_0\) 比平方误差:

    Write that recipe down as code and you have the training loop of conditional flow matching (CFM). At each step, draw a batch of noise \(\mathbf x_0\sim\mathcal N(\mathbf 0,I)\), a batch of data \(\mathbf x_1\sim q\) and times \(t\sim\mathcal U[0,1]\), form \(\mathbf x_t\), let the network predict a velocity there, and compare it with the line's velocity \(\mathbf x_1-\mathbf x_0\) in squared error:

    $$ \hl{loss}{\mathcal L_{\mathrm{CFM}}(\theta)}=\mathbb E_{t,\,\mathbf x_0,\,\mathbf x_1}\Big\|\,\hl{vth}{\mathbf v_\theta\big((1-t)\mathbf x_0+t\mathbf x_1,\;t\big)}-(\mathbf x_1-\mathbf x_0)\Big\|^2 $$

    没有 ODE、没有雅可比行列式、没有噪声调度表:就是一个回归问题,梯度用 16.3 节的反向传播。下图的网络是一个 \([5,48,48,2]\) 的 GELU MLP,输入是 \((x,y,t,\sin\pi t,\cos\pi t)\),共 2738 个参数;优化器是 Adam(学习率 0.003,2000 步后降到 0.001),每步 64 个样本。数据是二维的高斯混合,这样页面能算出精确的平均速度场 \(\mathbf u_t\)(第 5 节给出公式),于是我们可以直接量出网络离理想答案还有多远:

    No ODE, no Jacobian determinant, no noise schedule: just a regression problem, with gradients from the backpropagation of Lesson 16.3. The network in the figure is a \([5,48,48,2]\) GELU MLP whose input is \((x,y,t,\sin\pi t,\cos\pi t)\), 2738 parameters in all; the optimizer is Adam (learning rate 0.003, lowered to 0.001 after 2000 steps) on batches of 64. The data are 2-D Gaussian mixtures, so the page can compute the exact averaged field \(\mathbf u_t\) (the formula is in Section 5) and measure directly how far the network still is from the ideal answer:

    $$ \mathcal L_{\mathrm{CFM}}(\theta)=\underbrace{\mathbb E\,\big\|\mathbf v_\theta(\mathbf x_t,t)-\mathbf u_t(\mathbf x_t)\big\|^2}_{\hl{ferr}{\mathcal E(\theta)}}+\underbrace{\mathbb E\,\big\|\mathbf u_t(\mathbf x_t)-(\mathbf x_1-\mathbf x_0)\big\|^2}_{\hl{floor}{\mathcal L^{*}}} $$

    第一项是场误差 \(\mathcal E(\theta)\),训练能把它压向零;第二项 \(\mathcal L^*\) 与 \(\theta\) 无关,是直线速度围绕平均值的散布:同一点上直线们彼此越不一致,它越大。所以 CFM 的损失永远不会降到零,就像 16.2 节验证损失有一个噪声地板一样。采样时从 500 个固定的高斯样本出发,用 Euler 法积分 \(\mathbf x\leftarrow\mathbf x+h\,\mathbf v_\theta(\mathbf x,t)\),\(h=1/N\)(2.5、3.6 节)。

    The first term is the field error \(\mathcal E(\theta)\), which training can push towards zero; the second, \(\mathcal L^*\), does not depend on \(\theta\): it is the scatter of the line velocities around their average, larger wherever the lines through a point disagree more. So the CFM loss never reaches zero, just as the validation loss in Lesson 16.2 has a noise floor. Sampling starts from 500 fixed Gaussian samples and integrates \(\mathbf x\leftarrow\mathbf x+h\,\mathbf v_\theta(\mathbf x,t)\) with Euler's method, \(h=1/N\) (Lessons 2.5 and 3.6).

    FIG 2现场训练条件 flow matching:回归直线速度,学到平均速度Conditional flow matching live: regress line velocities, learn the average
    左:琥珀点是 500 个测试数据,紫点是 500 个模型样本(Euler 积分 \(N\) 步),青色小箭头是网络在时刻 \(t\)(滑块)的速度场 \(\mathbf v_\theta(\cdot,t)\),长度按 \(h=0.15\) 缩放。“轨迹”开关画出 30 个样本的积分路径。右(对数纵轴):紫线是 CFM 损失,琥珀线是场误差 \(\mathbb E\|\mathbf v_\theta-\mathbf u_t\|^2\),绿色虚线是地板 \(\mathcal L^*\);三者都在同一组固定的 400 个 \((\mathbf x_0,\mathbf x_1,t)\) 上每 50 步算一次。读数里的 SW 是模型样本与测试数据之间的切片 Wasserstein 距离(16 个方向的一维距离的平均,越小越像);“精确场”一栏是把网络换成精确的 \(\mathbf u_t\)、用同样的 \(N\) 步 Euler 时的 SW,这是这个步数下能达到的最好结果。 Left: amber dots are 500 test points, purple dots 500 model samples (Euler with \(N\) steps), and the small cyan arrows the network's velocity field \(\mathbf v_\theta(\cdot,t)\) at time \(t\) (slider), drawn for a step of \(h=0.15\). The "paths" switch draws the integration paths of 30 samples. Right (log vertical axis): the purple line is the CFM loss, the amber line the field error \(\mathbb E\|\mathbf v_\theta-\mathbf u_t\|^2\), and the green dashed line the floor \(\mathcal L^*\); all three are computed every 50 steps on one fixed set of 400 triples \((\mathbf x_0,\mathbf x_1,t)\). SW in the readout is the sliced Wasserstein distance between the model samples and the test data (the average of 1-D distances along 16 directions; smaller means more alike); the "exact field" entry is the SW obtained by replacing the network with the exact \(\mathbf u_t\) and the same \(N\) Euler steps, the best this step count can do.
    • 在“双月”上按“训练”,直到场误差低于 0.1(默认种子第 900 步左右;开始时是 1.34)。看损失:它从 4.38 降到 3.0 附近就几乎不动了,而地板 \(\mathcal L^*=2.89\)。真正在变好的是场误差和 SW。On "two moons" press "Train" until the field error falls below 0.1 (around step 900 with the default seed; it starts at 1.34). Watch the loss: it drops from 4.38 to about 3.0 and then barely moves, against a floor of \(\mathcal L^*=2.89\). What keeps improving is the field error and the SW.
    • 换到“8 个高斯”,训练到场误差低于 0.15(约 1900 步)。它的地板 \(\mathcal L^*=4.06\),比双月高得多:8 个峰离得远、方向各异,经过同一点的直线彼此更不一致。Switch to "8 Gaussians" and train until the field error is below 0.15 (about 1900 steps). Its floor is \(\mathcal L^*=4.06\), much higher than for the moons: the 8 peaks are far apart and in every direction, so the lines through a point disagree more.
    有意思的细节Things worth noticing

    (1) 把 \(t\) 滑块拉到 0 附近:箭头几乎平行地指向数据的中心。\(t=0\) 时 \(\mathbf x_t=\mathbf x_0\) 与 \(\mathbf x_1\) 独立,后验就是整个数据集,平均速度是 \(\mathbb E[\mathbf x_1]-\mathbf x\)。拉到 0.9 附近:箭头指向最近的那一小段数据,因为这时一个点离哪个数据点近,后验就几乎全押在那里。 (2) 双月训练 4000 步后,场误差约 0.06,20 步 Euler 的 SW 是 0.081,精确场是 0.078:剩下的差距主要来自步数,不是网络。 (3) 打开“轨迹”:路径是弯的。条件路径都是直线,平均之后却不是,这一点决定了要积分多少步(第 4 节)。

    (1) Drag the \(t\) slider near 0: the arrows point almost in parallel towards the centre of the data. At \(t=0\), \(\mathbf x_t=\mathbf x_0\) is independent of \(\mathbf x_1\), so the posterior is the whole dataset and the averaged velocity is \(\mathbb E[\mathbf x_1]-\mathbf x\). Drag it near 0.9: the arrows point to the nearest stretch of data, because by then being close to a data point puts almost all the posterior there. (2) After 4000 steps on the moons the field error is about 0.06, and the SW with 20 Euler steps is 0.081 against 0.078 for the exact field: the remaining gap comes mostly from the step count, not from the network. (3) Turn on "paths": they are curved. Every conditional path is straight, but their average is not, and this decides how many integration steps we need (Section 4).

    4另外两个视角:少步采样,以及把路径拉直Two more views: sampling in few steps, and straightening the paths

    少步采样:步数、积分方法和路径的形状Few-step sampling: step count, solver and the shape of the path

    训练好之后,生成一个样本就是解一个 ODE:\(\dot{\mathbf x}=\mathbf v_\theta(\mathbf x,t)\),从 \(t=0\) 积到 \(t=1\)。每次网络前向(NFE,number of function evaluations)都要花时间,所以问题是:给定一个 NFE 预算,样本有多准?下图用精确的平均场(不是网络)把“学得好不好”和“积分得准不准”分开,只看后者。除了直线路径,也可以选一条余弦路径 \(\mathbf x_t=\sin(\tfrac{\pi t}{2})\mathbf x_1+\cos(\tfrac{\pi t}{2})\mathbf x_0\):它的信号和噪声系数平方和为 1,与 19.6 节扩散模型常用的“方差保持”型路径属于同一类(第 5 节);把时间反过来(\(t=1-\tau/T\)),它就是 19.6 节的余弦日程(略去那里的小偏移 \(s\)),\(\sin\tfrac{\pi t}{2}\approx\sqrt{\bar\alpha_\tau}\)。积分方法可选 Euler(每步 1 次 NFE,一阶)或中点法(每步 2 次 NFE,二阶,3.6 节 FIG 4 里的 Heun 也是二阶)。

    Once trained, generating a sample means solving an ODE, \(\dot{\mathbf x}=\mathbf v_\theta(\mathbf x,t)\), from \(t=0\) to \(t=1\). Every network pass (NFE, number of function evaluations) costs time, so the question is: for a given NFE budget, how accurate is the sample? The figure uses the exact averaged field (not a network) to separate "how well was it learned" from "how accurately is it integrated", and looks only at the latter. Besides the straight path you can pick a cosine path, \(\mathbf x_t=\sin(\tfrac{\pi t}{2})\mathbf x_1+\cos(\tfrac{\pi t}{2})\mathbf x_0\): its signal and noise coefficients have squares summing to 1, the same family as the "variance-preserving" paths common in the diffusion models of Lesson 19.6 (Section 5); with time reversed (\(t=1-\tau/T\)) it is 19.6's cosine schedule (dropping its small offset \(s\)), \(\sin\tfrac{\pi t}{2}\approx\sqrt{\bar\alpha_\tau}\). The solver is Euler (1 NFE per step, first order) or the midpoint method (2 NFE per step, second order; the Heun method of Lesson 3.6's FIG 4 is second order too).

    FIG 3少步采样:一步落到均值,误差随步数怎样下降Few-step sampling: one step lands on the mean, and how the error falls with steps
    左:琥珀点是数据,青色细曲线是 30 条精确的 ODE 轨迹(RK4,100 步),紫色折线是同样起点按所选方法和 NFE 走出的数值路径,紫点是 300 个样本的数值终点,空心的青色圆圈是它们的精确终点。右(双对数坐标):300 个样本的平均终点误差 \(\mathbb E\|\hat{\mathbf x}_1-\varphi_1(\mathbf x_0)\|\) 随 NFE 的变化,四条线是四种组合:直线路径 + Euler(紫)、直线 + 中点(蓝)、余弦 + Euler(琥珀)、余弦 + 中点(红),大圆点是当前选择;\(\varphi_1\) 是 3.6 节的精确流映射。中点法 1 次 NFE 无法完成一步,所以从 NFE = 2 开始。 Left: amber dots are the data, the thin cyan curves 30 exact ODE trajectories (RK4, 100 steps), the purple polylines the numerical paths from the same starts with the chosen solver and NFE, the purple dots the numerical end points of 300 samples, and the hollow cyan circles their exact end points. Right (log-log): the mean end-point error \(\mathbb E\|\hat{\mathbf x}_1-\varphi_1(\mathbf x_0)\|\) over the 300 samples against NFE, one line per combination: straight path + Euler (purple), straight + midpoint (blue), cosine + Euler (amber), cosine + midpoint (red); the large dot marks the current choice; \(\varphi_1\) is the exact flow map of Lesson 3.6. The midpoint method cannot take a step with 1 NFE, so it starts at NFE = 2.
    • 选直线路径、Euler、NFE = 1:300 个样本全部落在同一个点,也就是数据的均值(读数里样本云的半径为 0)。一步 Euler 的终点是 \(\mathbf x_0+\mathbf u_0(\mathbf x_0)=\mathbb E[\mathbf x_1]\),与起点无关;这和 19.6 节 FIG 4 里 1 步 DDPM/DDIM 的结果是同一回事。Choose the straight path, Euler and NFE = 1: all 300 samples land on one point, the mean of the data (the sample cloud's radius in the readout is 0). One Euler step ends at \(\mathbf x_0+\mathbf u_0(\mathbf x_0)=\mathbb E[\mathbf x_1]\), whatever the start; it is the same thing as the 1-step DDPM/DDIM result in FIG 4 of Lesson 19.6.
    • 在“8 个高斯”和直线路径上,分别找出 Euler 和中点法把终点误差压到 0.05 以下所需的最小 NFE(两者都要试到)。Euler 需要 16(误差 0.044),中点法只要 8(0.028):NFE 每翻一倍,Euler 的误差减半,中点法的误差则下降得快得多(0.268 → 0.028 → 0.006)。On "8 Gaussians" with the straight path, find the smallest NFE that brings the end-point error below 0.05, once with Euler and once with the midpoint method (try both). Euler needs 16 (error 0.044), the midpoint method only 8 (0.028): each doubling of NFE halves Euler's error, while the midpoint method's falls much faster (0.268 → 0.028 → 0.006).
    • 还是“8 个高斯”,把预算固定为 NFE = 4,试遍四种组合,找出终点误差最小的那个(它可能不是你预想的)。Still on "8 Gaussians", fix the budget at NFE = 4, try all four combinations and find the one with the smallest end-point error (it may not be the one you expect).

    答案是余弦路径 + Euler(0.141,直线 + Euler 是 0.177,两种中点法都在 0.26 以上)。这值得停下来想一想。直线路径的条件路径是直的,但独立随机配对时,平均之后的边缘轨迹仍然是弯的,FIG 3 的青色曲线就是证据;在这个例子里,余弦路径配上等间距的时间步反而更好积分。真实的采样器还会把时间步排得不均匀,所以“哪条路径更省步数”没有放之四海皆准的答案。直线路径真正的优势在别处:如果配对本身是“不交叉”的,直线路径下的边缘轨迹就真的是直线,一步 Euler 就精确。下一张图就去造这样的配对。

    The answer is cosine path + Euler (0.141; straight + Euler gives 0.177, and both midpoint variants are above 0.26). This deserves a pause. The conditional paths of the straight path are straight, but with independent random pairing the averaged marginal trajectories are still curved, as FIG 3's cyan curves show; in this example the cosine path with evenly spaced time steps is actually easier to integrate. Practical samplers also space their time steps unevenly, so "which path saves steps" has no universal answer. The real advantage of the straight path lies elsewhere: if the pairing itself is "non-crossing", the marginal trajectories of the straight path are straight lines, and a single Euler step is exact. The next figure builds such pairings.

    把路径拉直:小批量最优传输与 reflowStraightening the paths: minibatch optimal transport and reflow

    边缘轨迹之所以弯,是因为训练时的直线互相交叉,平均之后拐了弯。交叉来自随机配对:一粒在左上的噪声,可能被配给右下的数据点。两种办法能让配对“有序”:

    • 小批量最优传输(Tong 等,2024;Pooladian 等,2023):每个批次里有 64 个 \(\mathbf x_0\) 和 64 个 \(\mathbf x_1\),不再随机配,而是求一个让总平方距离 \(\sum_i\|\mathbf x_0^{(i)}-\mathbf x_1^{(\sigma(i))}\|^2\) 最小的一一配对 \(\sigma\)(指派问题,本页用匈牙利算法精确求解)。最优传输的配对线段互不交叉。
    • Reflow(Liu 等,2023 的 rectified flow):先训练一个普通的 flow,然后从噪声 \(\mathbf x_0\) 出发积分它得到 \(\mathbf x_1=\mathrm{ODE}(\mathbf x_0)\),把这些 \((\mathbf x_0,\mathbf x_1)\) 当作新的配对再训练一次。ODE 的轨迹互不交叉,所以新的配对也不交叉,新的流更直。本页为了省时间,直接用精确平均场的 ODE(RK4,10 步)生成 2000 对,也就是 FIG 2 那个网络努力逼近的流。

    The marginal trajectories are curved because the training lines cross each other and bend when averaged. The crossings come from random pairing: a grain of noise at the top left may be paired with a data point at the bottom right. Two ways to make the pairing "orderly":

    • Minibatch optimal transport (Tong et al., 2024; Pooladian et al., 2023): a batch holds 64 samples \(\mathbf x_0\) and 64 samples \(\mathbf x_1\); instead of pairing them at random, find the one-to-one pairing \(\sigma\) that minimizes the total squared distance \(\sum_i\|\mathbf x_0^{(i)}-\mathbf x_1^{(\sigma(i))}\|^2\) (an assignment problem, solved exactly on this page with the Hungarian algorithm). The segments of an optimal-transport pairing never cross.
    • Reflow (the rectified flow of Liu et al., 2023): first train an ordinary flow, then integrate it from noise \(\mathbf x_0\) to get \(\mathbf x_1=\mathrm{ODE}(\mathbf x_0)\), and train again on these \((\mathbf x_0,\mathbf x_1)\) as the new pairs. ODE trajectories never cross, so neither do the new pairs, and the new flow is straighter. To save time, this page generates the 2000 pairs directly from the ODE of the exact averaged field (RK4, 10 steps), which is the very flow FIG 2's network tries to approximate.
    FIG 4同样的回归,不同的配对:独立配对 vs 有序配对The same regression, different pairings: independent vs orderly
    两个同样结构的网络(与 FIG 2 相同)同时训练,只有配对方式不同:左边是独立随机配对,右边是所选的有序配对。琥珀点是数据,紫点是 500 个模型样本(Euler,步数可选),蓝色折线是其中 25 个样本的积分路径。“训练配对”开关画出最近一个批次的 64 对 \((\mathbf x_0,\mathbf x_1)\) 连线(青色):左边纵横交错,右边短而几乎不交叉。读数给出两边的损失(滑动平均)和 SW。Reflow 的 2000 对配对在第一次训练时分几帧生成。 Two networks with the same architecture (as in FIG 2) train side by side and differ only in the pairing: independent random pairs on the left, the chosen orderly pairing on the right. Amber dots are the data, purple dots 500 model samples (Euler, step count selectable), and the blue polylines the integration paths of 25 of them. The "training pairs" switch draws the 64 pairs \((\mathbf x_0,\mathbf x_1)\) of the latest batch as cyan segments: criss-crossing on the left, short and hardly crossing on the right. The readout gives both sides' loss (moving average) and SW. The 2000 reflow pairs are generated over a few frames the first time you train.
    • 右边选“小批量 OT”,Euler 步数保持默认的 1,训练至少 500 步:右边的样本已经是两条月牙(SW < 0.2),左边的样本仍然缩在中心附近(SW > 0.6)。注意两边的损失:独立配对停在 2.8 到 2.9 之间,OT 降到 0.25 左右,因为配对越确定,直线越少交叉,地板越低。On the right choose "minibatch OT", keep the Euler steps at the default 1 and train for at least 500 steps: the right-hand samples already form two moons (SW < 0.2), while the left-hand ones are still huddled near the centre (SW > 0.6). Compare the losses: independent pairing stalls between 2.8 and 2.9, OT drops to about 0.25, because the more determined the pairing, the fewer the crossings and the lower the floor.
    • 右边换成“Reflow”并训练,直到右边 1 步 Euler 的 SW 低于 0.1(默认种子约 300 步)。蓝色路径几乎是直线:一次网络前向就够了。Switch the right-hand side to "Reflow" and train until its 1-step Euler SW falls below 0.1 (about 300 steps with the default seed). The blue paths are nearly straight: one network pass is enough.
    一个想法,几种看法One idea, several views

    回归:在 \((\mathbf x_t,t)\) 处回归直线速度,学到条件均值(16.2 节)。 流体:设计一个速度场,使密度按连续性方程从 \(p_0\) 变到 \(q\)(3.6 节的“沙子守恒”)。 传输:flow 定义了一个从噪声到数据的映射 \(\mathbf x_0\mapsto\mathbf x_1\);配对越接近最优传输,映射越接近直线位移,积分越省步。 连续 normalizing flow:训练好的 \(\mathbf v_\theta\) 就是 19.3 节 FIG 5 那样的 ODE flow,可以用散度积分求精确似然,只是训练它不再需要最大似然。 扩散:换一条高斯路径,同一个回归就变成了 19.6 节的去噪(第 5 节)。

    Regression: at \((\mathbf x_t,t)\), regress line velocities and learn their conditional mean (Lesson 16.2). Fluid: design a velocity field so that the density moves from \(p_0\) to \(q\) by the continuity equation ("sand is conserved", Lesson 3.6). Transport: the flow defines a map from noise to data, \(\mathbf x_0\mapsto\mathbf x_1\); the closer the pairing is to optimal transport, the closer the map is to straight-line displacement, and the fewer steps integration needs. Continuous normalizing flow: the trained \(\mathbf v_\theta\) is an ODE flow just like FIG 5 of Lesson 19.3, with an exact likelihood from the divergence integral, only no longer trained by maximum likelihood. Diffusion: swap in another Gaussian path and the same regression becomes the denoising of Lesson 19.6 (Section 5).

    5再看数学The math

    提示:把鼠标悬停在公式中带底色的符号上,图中对应的元素会被高亮(FIG 1 的密度底色、紫色直线、蓝色轨迹与琥珀色箭头、探针的后验短线,FIG 2 的速度箭头、损失曲线、场误差曲线与地板线,FIG 3 的数值路径与精确轨迹,FIG 4 的训练配对,FIG 5 的采样动作块与观测)。Tip: hover the highlighted symbols in the formulas; matching elements in the figures light up (FIG 1's density shading, purple lines, blue trajectories and amber arrow, and the probe's posterior segments; FIG 2's velocity arrows, loss curve, field-error curve and floor line; FIG 3's numerical paths and exact trajectories; FIG 4's training pairs; FIG 5's sampled action chunks and observation).

    概率路径与连续性方程Probability paths and the continuity equation

    记 \(\mathbf z=(\mathbf x_0,\mathbf x_1)\) 为一对配对,按某个联合分布 \(\pi(\mathbf z)\) 抽取;独立配对时 \(\pi=p_0\otimes q\)。每一对给出一条条件路径:\(\mathbf x_t=\psi_t(\mathbf z)=(1-t)\mathbf x_0+t\mathbf x_1\),速度 \(\hl{cond}{\mathbf u_t(\mathbf x\mid\mathbf z)=\mathbf x_1-\mathbf x_0}\)。对所有配对平均,就得到边缘的概率路径 \(\hl{pt}{p_t}\) 和边缘速度场:

    Let \(\mathbf z=(\mathbf x_0,\mathbf x_1)\) be a pair drawn from some joint distribution \(\pi(\mathbf z)\); with independent pairing \(\pi=p_0\otimes q\). Each pair defines a conditional path \(\mathbf x_t=\psi_t(\mathbf z)=(1-t)\mathbf x_0+t\mathbf x_1\) with velocity \(\hl{cond}{\mathbf u_t(\mathbf x\mid\mathbf z)=\mathbf x_1-\mathbf x_0}\). Averaging over all pairs gives the marginal probability path \(\hl{pt}{p_t}\) and the marginal velocity field:

    $$ p_t(\mathbf x)=\int p_t(\mathbf x\mid\mathbf z)\,\pi(\mathbf z)\,d\mathbf z,\qquad \hl{marg}{\mathbf u_t(\mathbf x)}=\int \mathbf u_t(\mathbf x\mid\mathbf z)\,\frac{p_t(\mathbf x\mid\mathbf z)\,\pi(\mathbf z)}{p_t(\mathbf x)}\,d\mathbf z=\mathbb E\big[\mathbf u_t(\mathbf x_t\mid\mathbf z)\,\big|\,\mathbf x_t=\mathbf x\big]. $$

    这里 \(p_t(\mathbf x\mid\mathbf z)\) 是集中在 \(\psi_t(\mathbf z)\) 一点上的分布;分式就是后验 \(p(\mathbf z\mid\mathbf x_t=\mathbf x)\)。一个速度场 \(\mathbf v\) “生成”一条概率路径,是指沿它漂流的样本在每个时刻的分布都是 \(p_t\),等价地,二者满足连续性方程(质量守恒,3.6 节的散度在这里出现):

    Here \(p_t(\mathbf x\mid\mathbf z)\) is the distribution concentrated on the single point \(\psi_t(\mathbf z)\), and the fraction is the posterior \(p(\mathbf z\mid\mathbf x_t=\mathbf x)\). A velocity field \(\mathbf v\) "generates" a probability path if samples drifting along it are distributed as \(p_t\) at every time; equivalently, the two satisfy the continuity equation (conservation of mass, where the divergence of Lesson 3.6 appears):

    $$ \frac{\partial p_t}{\partial t}+\nabla\!\cdot\big(p_t\,\mathbf v_t\big)=0\qquad\Longleftrightarrow\qquad \frac{d}{dt}\log p_t\big(\mathbf x(t)\big)=-\nabla\!\cdot\mathbf v_t\big(\mathbf x(t)\big)\qquad\big(\dot{\mathbf x}=\mathbf v_t(\mathbf x)\big). $$

    右边那个形式沿着轨迹 \(\dot{\mathbf x}=\mathbf v_t(\mathbf x)\) 成立,它正是 19.3 节 FIG 5 上方的瞬时换元公式;把左式展开成 \(\partial_tp+\mathbf v\cdot\nabla p+p\,\nabla\cdot\mathbf v=0\),前两项合起来就是沿轨迹的全导数。

    定理 1(边缘场生成边缘路径)。每条条件路径都满足自己的连续性方程 \(\partial_tp_t(\mathbf x\mid\mathbf z)+\nabla\cdot\big(p_t(\mathbf x\mid\mathbf z)\,\mathbf u_t(\mathbf x\mid\mathbf z)\big)=0\)(一个点沿直线匀速移动,当然质量守恒)。对 \(\pi(\mathbf z)\) 积分,对时间和空间的导数都可以拿到积分外面:

    The form on the right holds along trajectories \(\dot{\mathbf x}=\mathbf v_t(\mathbf x)\) and is exactly the instantaneous change of variables above FIG 5 of Lesson 19.3; expand the left form into \(\partial_tp+\mathbf v\cdot\nabla p+p\,\nabla\cdot\mathbf v=0\), and the first two terms together are the total derivative along a trajectory.

    Theorem 1 (the marginal field generates the marginal path). Each conditional path satisfies its own continuity equation, \(\partial_tp_t(\mathbf x\mid\mathbf z)+\nabla\cdot\big(p_t(\mathbf x\mid\mathbf z)\,\mathbf u_t(\mathbf x\mid\mathbf z)\big)=0\) (a point moving along a straight line at constant speed obviously conserves mass). Integrate over \(\pi(\mathbf z)\); the time and space derivatives come out of the integral:

    $$ \frac{\partial p_t(\mathbf x)}{\partial t}=-\nabla\!\cdot\!\int p_t(\mathbf x\mid\mathbf z)\,\mathbf u_t(\mathbf x\mid\mathbf z)\,\pi(\mathbf z)\,d\mathbf z=-\nabla\!\cdot\big(p_t(\mathbf x)\,\mathbf u_t(\mathbf x)\big), $$

    最后一步就是 \(\mathbf u_t\) 的定义(分子乘上 \(p_t\))。所以 \(\mathbf u_t\) 满足 \(p_t\) 的连续性方程:从 \(p_0\) 出发沿 \(\mathbf u_t\) 积分,在 \(t=1\) 恰好得到 \(q\)。这就是 FIG 1 里紫色直线和蓝色曲线给出同一片底色的原因。

    and the last step is just the definition of \(\mathbf u_t\) (multiply its numerator by \(p_t\)). So \(\mathbf u_t\) satisfies the continuity equation of \(p_t\): start from \(p_0\), integrate along \(\mathbf u_t\), and at \(t=1\) you get exactly \(q\). That is why the purple lines and the blue curves of FIG 1 produce the same shading.

    为什么回归条件速度就够了Why regressing the conditional velocity is enough

    理想的目标是 flow matching 损失 \(\mathcal L_{\mathrm{FM}}(\theta)=\mathbb E_{t,\,\mathbf x\sim p_t}\|\mathbf v_\theta(\mathbf x,t)-\mathbf u_t(\mathbf x)\|^2\),可惜 \(\mathbf u_t\) 算不出来。定理 2(条件 flow matching):\(\mathcal L_{\mathrm{CFM}}\) 与 \(\mathcal L_{\mathrm{FM}}\) 只差一个与 \(\theta\) 无关的常数,所以梯度相同。证明只需把两个平方展开:

    The ideal objective is the flow matching loss \(\mathcal L_{\mathrm{FM}}(\theta)=\mathbb E_{t,\,\mathbf x\sim p_t}\|\mathbf v_\theta(\mathbf x,t)-\mathbf u_t(\mathbf x)\|^2\), but \(\mathbf u_t\) cannot be computed. Theorem 2 (conditional flow matching): \(\mathcal L_{\mathrm{CFM}}\) and \(\mathcal L_{\mathrm{FM}}\) differ only by a constant independent of \(\theta\), so their gradients are equal. The proof just expands the two squares:

    $$ \begin{aligned} \hl{loss}{\mathcal L_{\mathrm{CFM}}}&=\mathbb E\,\|\mathbf v_\theta(\mathbf x_t,t)\|^2-2\,\mathbb E\,\big\langle \mathbf v_\theta(\mathbf x_t,t),\,\mathbf x_1-\mathbf x_0\big\rangle+\mathbb E\,\|\mathbf x_1-\mathbf x_0\|^2,\\ \mathbb E\,\big\langle \mathbf v_\theta(\mathbf x_t,t),\,\mathbf x_1-\mathbf x_0\big\rangle&=\mathbb E\,\big\langle \mathbf v_\theta(\mathbf x_t,t),\,\mathbb E[\mathbf x_1-\mathbf x_0\mid\mathbf x_t]\big\rangle=\mathbb E\,\big\langle \mathbf v_\theta(\mathbf x_t,t),\,\mathbf u_t(\mathbf x_t)\big\rangle,\\ \Longrightarrow\quad \mathcal L_{\mathrm{CFM}}&=\mathcal L_{\mathrm{FM}}+\hl{floor}{\mathbb E\,\big\|\mathbf u_t(\mathbf x_t)-(\mathbf x_1-\mathbf x_0)\big\|^2}. \end{aligned} $$

    第二行用了条件期望的塔性质(先固定 \(\mathbf x_t\) 对配对求平均)。最后一项就是 FIG 2 的地板 \(\mathcal L^*\),它等于“给定 \(\mathbf x_t\) 时直线速度的方差”的期望。它在配对越确定时越小:FIG 4 里独立配对的损失停在 2.8 到 2.9 之间,OT 降到 0.25 左右,reflow 只有 0.01 左右,但三个网络的“好坏”并不能从这三个数比出来。Lipman 等的原始论文是对 \(\mathbf x_1\) 单独条件化,用高斯条件路径 \(p_t(\mathbf x\mid\mathbf x_1)=\mathcal N\big(t\mathbf x_1,(1-(1-\sigma_{\min})t)^2I\big)\),条件速度 \(\big(\mathbf x_1-(1-\sigma_{\min})\mathbf x\big)/\big(1-(1-\sigma_{\min})t\big)\);\(\sigma_{\min}\to0\) 时它就是本节的 \(\mathbf x_1-\mathbf x_0\)。

    The second line uses the tower property of conditional expectation (fix \(\mathbf x_t\) and average over the pairs first). The last term is FIG 2's floor \(\mathcal L^*\), the expected variance of the line velocity given \(\mathbf x_t\). It shrinks as the pairing becomes more determined: in FIG 4 the loss of independent pairing stalls between 2.8 and 2.9, OT drops to about 0.25 and reflow to about 0.01, yet those three numbers say nothing about which network is better. The original paper by Lipman et al. conditions on \(\mathbf x_1\) alone, with the Gaussian conditional path \(p_t(\mathbf x\mid\mathbf x_1)=\mathcal N\big(t\mathbf x_1,(1-(1-\sigma_{\min})t)^2I\big)\) and conditional velocity \(\big(\mathbf x_1-(1-\sigma_{\min})\mathbf x\big)/\big(1-(1-\sigma_{\min})t\big)\); as \(\sigma_{\min}\to0\) this is the \(\mathbf x_1-\mathbf x_0\) of this lesson.

    高斯混合的精确场(本页的“标准答案”)The exact field for a Gaussian mixture (this page's "answer key")

    取一般的高斯路径 \(\mathbf x_t=\alpha_t\mathbf x_1+\sigma_t\mathbf x_0\)(直线路径 \(\alpha_t=t,\ \sigma_t=1-t\);余弦路径 \(\alpha_t=\sin\tfrac{\pi t}{2},\ \sigma_t=\cos\tfrac{\pi t}{2}\)),条件速度 \(\dot\alpha_t\mathbf x_1+\dot\sigma_t\mathbf x_0\)。若数据是 \(q=\sum_k\pi_k\,\mathcal N(\boldsymbol\mu_k,s_k^2I)\),给定分量 \(k\) 时 \(\mathbf x_t\) 与速度是联合高斯的,4.4 节的条件高斯公式直接给出:

    Take a general Gaussian path \(\mathbf x_t=\alpha_t\mathbf x_1+\sigma_t\mathbf x_0\) (straight: \(\alpha_t=t,\ \sigma_t=1-t\); cosine: \(\alpha_t=\sin\tfrac{\pi t}{2},\ \sigma_t=\cos\tfrac{\pi t}{2}\)) with conditional velocity \(\dot\alpha_t\mathbf x_1+\dot\sigma_t\mathbf x_0\). If the data are \(q=\sum_k\pi_k\,\mathcal N(\boldsymbol\mu_k,s_k^2I)\), then given component \(k\), \(\mathbf x_t\) and the velocity are jointly Gaussian, and the conditional-Gaussian formula of Lesson 4.4 gives directly:

    $$ \mathbf u_t(\mathbf x)=\sum_k \hl{post}{w_k(\mathbf x,t)}\Big[\dot\alpha_t\boldsymbol\mu_k+c_k(t)\,(\mathbf x-\alpha_t\boldsymbol\mu_k)\Big],\qquad c_k=\frac{\dot\alpha_t\alpha_t s_k^2+\dot\sigma_t\sigma_t}{\alpha_t^2s_k^2+\sigma_t^2},\qquad w_k\propto\pi_k\,\mathcal N\big(\mathbf x;\alpha_t\boldsymbol\mu_k,(\alpha_t^2s_k^2+\sigma_t^2)I\big). $$

    \(w_k\) 是 FIG 1 读数里的后验权重。代入直线路径和 \(t=0\):\(c_k=-1\)、\(w_k=\pi_k\),于是 \(\mathbf u_0(\mathbf x)=\mathbb E[\mathbf x_1]-\mathbf x\),这就是 FIG 3 里“一步 Euler 落到均值”的来源。本页的双月和螺旋其实也是高斯混合(28 个和 26 个标准差 0.12 的小高斯沿曲线排开),所以 FIG 2 的场误差、FIG 3 的参考轨迹和 FIG 4 的 reflow 配对都能精确算出。

    \(w_k\) is the posterior weight in FIG 1's readout. Plug in the straight path at \(t=0\): \(c_k=-1\) and \(w_k=\pi_k\), so \(\mathbf u_0(\mathbf x)=\mathbb E[\mathbf x_1]-\mathbf x\), which is where FIG 3's "one Euler step lands on the mean" comes from. The moons and the spiral on this page are in fact Gaussian mixtures too (28 and 26 small Gaussians with standard deviation 0.12 strung along curves), which is why FIG 2's field error, FIG 3's reference trajectories and FIG 4's reflow pairs can all be computed exactly.

    与扩散模型的关系The relation to diffusion models

    19.6 节的 DDPM 用的也是一条高斯路径:信号系数 \(\alpha\) 与噪声系数 \(\sigma\) 满足 \(\alpha^2+\sigma^2=1\)(方差保持),只是时间方向相反(DDPM 的第 0 步是数据,见第 2 节的约定对照),网络预测的是噪声 \(\boldsymbol\varepsilon\)(就是这里的 \(\mathbf x_0\))。在高斯路径上,知道了 \(\mathbf x_t\),下面三个量中任意一个都能换算出另外两个,所以“预测速度”“预测噪声”“预测干净数据”携带的信息相同。对直线路径:

    The DDPM of Lesson 19.6 also uses a Gaussian path: its signal coefficient \(\alpha\) and noise coefficient \(\sigma\) satisfy \(\alpha^2+\sigma^2=1\) (variance preserving), only time runs the other way (DDPM's step 0 is the data; see the convention note in Section 2), and the network predicts the noise \(\boldsymbol\varepsilon\) (the \(\mathbf x_0\) of this lesson). On a Gaussian path, once \(\mathbf x_t\) is known, any one of the three quantities below determines the other two, so "predict the velocity", "predict the noise" and "predict the clean data" carry the same information. For the straight path:

    $$ \hat{\mathbf x}_1=\mathbf x_t+(1-t)\,\hat{\mathbf v},\qquad \hat{\boldsymbol\varepsilon}=\mathbf x_t-t\,\hat{\mathbf v},\qquad \nabla_{\mathbf x}\log p_t(\mathbf x_t)=-\frac{\hat{\boldsymbol\varepsilon}}{1-t}=-\frac{\mathbf x_t-t\,\hat{\mathbf v}}{1-t},\qquad \hat{\mathbf v}=\hat{\mathbf x}_1-\hat{\boldsymbol\varepsilon}, $$

    其中“帽子”都表示给定 \(\mathbf x_t\) 的条件均值,\(\hat{\mathbf v}=\mathbf u_t(\mathbf x_t)\)。第三个式子把速度和 19.6 节的得分(score)联系起来(Tweedie 公式)。19.6 节 DDIM 的确定性采样器,就是在离散地积分这类 ODE(Song 等 2021 称之为概率流 ODE)。所以 flow matching 与扩散不是两类不同的模型,而是同一个框架里的不同选择:哪条路径(\(\alpha_t,\sigma_t\))、网络输出什么(这会改变损失在不同 \(t\) 上的权重)、怎样抽 \(t\)、以及用 ODE 还是 SDE 采样。直线路径的好处是目标最简单(\(\mathbf x_1-\mathbf x_0\),处处有界,不需要调度表),并且在配对有序时轨迹真正变直。

    where every hat denotes a conditional mean given \(\mathbf x_t\), and \(\hat{\mathbf v}=\mathbf u_t(\mathbf x_t)\). The third formula ties the velocity to the score of Lesson 19.6 (Tweedie's formula). The deterministic DDIM sampler of Lesson 19.6 integrates an ODE of exactly this kind in discrete steps (Song et al., 2021, call it the probability-flow ODE). So flow matching and diffusion are not two different kinds of model but different choices within one framework: which path (\(\alpha_t,\sigma_t\)), what the network outputs (which changes how the loss is weighted across \(t\)), how \(t\) is sampled, and whether sampling uses an ODE or an SDE. The straight path's merits are the simplest target (\(\mathbf x_1-\mathbf x_0\), bounded everywhere, no schedule) and trajectories that really become straight when the pairing is orderly.

    采样误差、直线与最优传输Sampling error, straight lines and optimal transport

    Euler 法每步的局部误差是 \(O(h^2)\),累积 \(1/h\) 步后,与精确的流映射 \(\hl{ode}{\varphi_1(\mathbf x_0)}\)(3.6 节)之间的全局误差是 \(O(h)\);中点法是 \(O(h^2)\)(2.5 节)。误差的常数取决于轨迹有多弯:如果每条轨迹都是匀速直线,\(\mathbf v\) 沿轨迹不变,Euler 一步就精确,\(\hl{euler}{\mathbf x_1=\mathbf x_0+\mathbf v_\theta(\mathbf x_0,0)}\)。什么样的配对能做到?Benamou 与 Brenier(2000)证明,二次代价的最优传输可以写成“动能最小的流”:

    Euler's method has a local error of \(O(h^2)\) per step and, after \(1/h\) steps, a global error of \(O(h)\) relative to the exact flow map \(\hl{ode}{\varphi_1(\mathbf x_0)}\) (Lesson 3.6); the midpoint method's is \(O(h^2)\) (Lesson 2.5). The constant depends on how curved the trajectories are: if every trajectory is a straight line traversed at constant speed, \(\mathbf v\) is constant along it and a single Euler step is exact, \(\hl{euler}{\mathbf x_1=\mathbf x_0+\mathbf v_\theta(\mathbf x_0,0)}\). Which pairing achieves this? Benamou and Brenier (2000) showed that optimal transport with quadratic cost can be written as "the flow of least kinetic energy":

    $$ \min_{\pi}\ \mathbb E_{(\mathbf x_0,\mathbf x_1)\sim\pi}\|\mathbf x_1-\mathbf x_0\|^2\;=\;\min_{p_t,\mathbf v_t\,:\ \partial_tp_t+\nabla\cdot(p_t\mathbf v_t)=0}\ \int_0^1\!\!\int p_t(\mathbf x)\,\|\mathbf v_t(\mathbf x)\|^2\,d\mathbf x\,dt . $$

    最优的流沿直线匀速搬运,粒子互不相撞。所以用最优传输配对再走直线,边缘轨迹就是直线(FIG 4 右边那些青色的配对线段 \(\hl{pair}{(\mathbf x_0,\mathbf x_1)}\) 短而几乎不交叉)。小批量 OT 只在每个批次内部求最优,是近似,批次越大越接近;reflow 用一个已有的 ODE 映射作配对,Liu 等证明这样做不会增加任何凸代价下的传输代价,并且反复 reflow 会让轨迹越来越直。代价是要多训练一次,并且配对来自模型本身,模型的误差会被带进新的数据里。

    The optimal flow carries mass along straight lines at constant speed, and particles never collide. So pair by optimal transport, walk straight lines, and the marginal trajectories are straight (the cyan pair segments \(\hl{pair}{(\mathbf x_0,\mathbf x_1)}\) on the right of FIG 4 are short and hardly cross). Minibatch OT optimizes only within each batch, an approximation that improves with batch size; reflow uses an existing ODE map as the pairing, which Liu et al. proved never increases the transport cost under any convex cost, and repeated reflow makes the trajectories straighter and straighter. The price is another round of training, and the pairs come from the model itself, so its errors are baked into the new data.

    条件生成、引导与似然Conditioning, guidance and likelihood

    条件生成:把条件 \(\mathbf c\)(文本、图像特征、机器人观测)作为网络的额外输入,\(\mathbf v_\theta(\mathbf x,t,\mathbf c)\),训练时配对取自同一个 \(\mathbf c\) 下的数据,其余不变;第 6 节的动作头就是这样。无分类器引导(19.6 节)原样适用于速度:\(\tilde{\mathbf v}=\mathbf v_\theta(\mathbf x,t,\varnothing)+w\,\big(\mathbf v_\theta(\mathbf x,t,\mathbf c)-\mathbf v_\theta(\mathbf x,t,\varnothing)\big)\)。似然:训练好的流是一个连续 normalizing flow,19.3 节的公式 \(\log p_1(\mathbf x_1)=\log p_0(\mathbf x_0)-\int_0^1\nabla\cdot\mathbf v_\theta\,dt\) 照样成立,需要时可以倒着积分求密度。时间的抽样:本页用 \(t\sim\mathcal U[0,1]\);Stable Diffusion 3(Esser 等,2024)用 logit-正态分布把训练集中在中间的 \(t\),π0 用一个偏向噪声一端的 Beta 型分布。这只改变损失在不同时刻上的权重,不改变最优解 \(\mathbf u_t\)。

    Conditional generation: feed the condition \(\mathbf c\) (text, image features, robot observations) to the network as an extra input, \(\mathbf v_\theta(\mathbf x,t,\mathbf c)\), and draw each training pair from data under the same \(\mathbf c\); nothing else changes. The action head of Section 6 does exactly this. Classifier-free guidance (Lesson 19.6) carries over to velocities as is: \(\tilde{\mathbf v}=\mathbf v_\theta(\mathbf x,t,\varnothing)+w\,\big(\mathbf v_\theta(\mathbf x,t,\mathbf c)-\mathbf v_\theta(\mathbf x,t,\varnothing)\big)\). Likelihood: the trained flow is a continuous normalizing flow, so the formula of Lesson 19.3, \(\log p_1(\mathbf x_1)=\log p_0(\mathbf x_0)-\int_0^1\nabla\cdot\mathbf v_\theta\,dt\), still holds, and densities can be computed by integrating backwards when needed. Sampling the time: this page uses \(t\sim\mathcal U[0,1]\); Stable Diffusion 3 (Esser et al., 2024) uses a logit-normal distribution that concentrates training on intermediate \(t\), and π0 uses a Beta-shaped distribution that leans towards the noisy end. This only changes how the loss is weighted over time, not the optimum \(\mathbf u_t\).

    6机器人中的它:π0 风格的 flow matching 动作头In robotics: a π0-style flow-matching action head

    π0(Black 等,Physical Intelligence,2024)是一个视觉-语言-动作模型:一个预训练的视觉-语言模型(PaliGemma)读入多路相机图像、语言指令和机器人本体状态,旁边接一个约 3 亿参数的动作专家,用 flow matching 生成一整个动作块(未来 50 步的连续动作)。推理时从高斯噪声出发积分 10 步(步长 0.1);观测部分的注意力键值只算一次并缓存,10 步里只重复运行较小的动作专家。这样它能以高达 50 Hz 的频率控制双臂等机器人完成叠衣服这类灵巧任务。与 17.5 节的离散动作 token 相比,这条路线直接输出连续动作,不需要动作 tokenizer。

    下图是一个能在浏览器里训练的缩小版。一个点机器人以恒定速度从左向右开,经过一个圆形障碍物(半径 0.55),观测 \(\hl{obs}{\mathbf o=(y_s,y_o)}\) 是起点高度和障碍物高度,动作块是未来 \(H=8\) 步的横向位置(相对起点)。示教数据里,人从障碍物上方绕过的概率是 \(\sigma(-6y_o)\):障碍物越靠上,越多人从下方走。速度网络 \(\mathbf v_\theta(\mathbf a_t,t,\mathbf o)\) 是 \([13,64,64,8]\) 的 GELU MLP(8 维动作 + 2 维观测 + 3 个时间特征),共 5576 个参数;作为对照,一个 \([2,64,64,8]\) 的网络直接用均方误差回归动作块,也就是普通的行为克隆(22.1 节)。两者同时训练,Adam,每步 64 条示教。

    19.6 节的 FIG 6 已经在一个固定的避障场景里,用精确的 score 对整段动作去噪,展示了每段动作都完整地选定一边、两边的频率与示教一致。这里不再重复那一点,而是看三样新东西:速度场是网络现场学出来的;它以观测为条件,障碍物一动,两种走法的比例就跟着变;积分步数可以一直减到 1,这时 flow matching 退化成均方误差回归。

    π0 (Black et al., Physical Intelligence, 2024) is a vision-language-action model: a pretrained vision-language model (PaliGemma) reads several camera images, a language instruction and the robot's proprioceptive state, and next to it sits an action expert of about 300 million parameters that generates a whole action chunk (the next 50 continuous actions) by flow matching. At inference it integrates 10 steps (step size 0.1) from Gaussian noise; the attention keys and values of the observation part are computed once and cached, so only the smaller action expert reruns during the 10 steps. This lets it control robots such as bimanual arms at up to 50 Hz in dexterous tasks like folding laundry. Compared with the discrete action tokens of Lesson 17.5, this route outputs continuous actions directly and needs no action tokenizer.

    The figure below is a scaled-down version you can train in the browser. A point robot drives from left to right at constant speed past a disc obstacle (radius 0.55); the observation \(\hl{obs}{\mathbf o=(y_s,y_o)}\) is the start height and the obstacle height, and the action chunk is the lateral position at the next \(H=8\) steps (relative to the start). In the demonstrations a person passes above the obstacle with probability \(\sigma(-6y_o)\): the higher the obstacle, the more people go below it. The velocity network \(\mathbf v_\theta(\mathbf a_t,t,\mathbf o)\) is a \([13,64,64,8]\) GELU MLP (8 action dimensions + 2 observation dimensions + 3 time features), 5576 parameters; for comparison, a \([2,64,64,8]\) network regresses the action chunk directly with a mean squared error, i.e. plain behaviour cloning (Lesson 22.1). Both train together with Adam on 64 demonstrations per step.

    FIG 6 of Lesson 19.6 already denoised whole action chunks around an obstacle in one fixed scene with the exact score, and showed that each chunk commits fully to one side, with the two sides at the demonstrated frequencies. Rather than repeat that, this figure adds three new things: the velocity field is learned live by a network; it is conditioned on the observation, so the ratio of the two ways round changes as the obstacle moves; and the number of integration steps can go all the way down to 1, where flow matching collapses into mean-squared-error regression.

    $$ \mathcal L(\theta)=\mathbb E_{t,\;\boldsymbol\varepsilon\sim\mathcal N(\mathbf 0,I),\;(\hl{obs}{\mathbf o},\mathbf a)\sim\mathcal D}\Big\|\mathbf v_\theta\big((1-t)\boldsymbol\varepsilon+t\,\mathbf a,\;t,\;\mathbf o\big)-(\mathbf a-\boldsymbol\varepsilon)\Big\|^2,\qquad \hl{chunk}{\mathbf a^{(k+1)}=\mathbf a^{(k)}+\tfrac1N\,\mathbf v_\theta\big(\mathbf a^{(k)},\tfrac kN,\mathbf o\big)},\ \ \mathbf a^{(0)}=\boldsymbol\varepsilon $$
    FIG 5flow matching 动作头:多模态的动作块 vs 均方误差回归A flow-matching action head: multimodal action chunks vs mean-squared-error regression
    灰色圆盘是障碍物,拖动它中心的蓝色手柄上下移动(\(y_o\)),拖动绿色手柄改变起点高度(\(y_s\)),青色十字是终点。琥珀色细线是当前观测下的 20 条示教(来自数据生成器,供对照),紫色折线是 flow matching 动作头从 20 个固定噪声积分出的动作块,红色虚线是均方误差回归给出的动作块。读数由 200 个样本算出:碰撞率、从上方绕过的比例(与示教的真实比例 \(\sigma(-6y_o)\) 对照),以及均方误差那条线是否撞上障碍。“积分步数”就是采样时的 NFE。 The grey disc is the obstacle: drag the blue handle at its centre up and down (\(y_o\)), and drag the green handle to change the start height (\(y_s\)); the cyan cross is the goal. The thin amber lines are 20 demonstrations for the current observation (from the data generator, for reference), the purple polylines action chunks integrated by the flow-matching head from 20 fixed noise draws, and the red dashed line the chunk given by mean-squared-error regression. The readout uses 200 samples: the collision rate, the fraction passing above (against the true demonstration fraction \(\sigma(-6y_o)\)), and whether the mean-squared-error line hits the obstacle. "Integration steps" is the NFE used for sampling.
    • 保持障碍物在中间(\(|y_o|\le0.1\)),按“训练”,直到 10 步积分的碰撞率低于 10%(默认种子约 650 步;开始时约 91%)。紫色动作块分成上下两束,红色的均方误差线从中间直穿障碍物。Keep the obstacle in the middle (\(|y_o|\le0.1\)) and press "Train" until the collision rate with 10 integration steps falls below 10% (about 650 steps with the default seed; it starts near 91%). The purple chunks split into an upper and a lower bundle, while the red mean-squared-error line goes straight through the obstacle.
    • 训练到碰撞率低于 10% 之后,把积分步数改成 1,障碍物仍在中间:碰撞率跳到 90% 以上,紫色线全都挤到红线附近。一步 Euler 从 \(t=0\) 出发,得到的是 \(\boldsymbol\varepsilon+\mathbf v_\theta(\boldsymbol\varepsilon,0,\mathbf o)\approx\mathbb E[\mathbf a\mid\mathbf o]\):一步的 flow matching 就是均方误差回归。Once the collision rate is below 10%, set the integration steps to 1 with the obstacle still in the middle: the collision rate jumps above 90% and all purple lines crowd around the red one. One Euler step from \(t=0\) gives \(\boldsymbol\varepsilon+\mathbf v_\theta(\boldsymbol\varepsilon,0,\mathbf o)\approx\mathbb E[\mathbf a\mid\mathbf o]\): one-step flow matching is mean-squared-error regression.
    • 训练到碰撞率低于 10% 之后,用 10 步积分,把障碍物拖到 \(|y_o|\ge0.3\):示教里只有约 14% 或更少(或 86% 以上)从上方走,模型样本的比例与之相差不到 8 个百分点(刚过 10% 时可能还差一些,再多训练几百步即可)。动作头学到的不只是“有两种走法”,还有各自的概率。Once the collision rate is below 10%, with 10 integration steps drag the obstacle to \(|y_o|\ge0.3\): now only about 14% or fewer of the demonstrations pass above (or 86% or more), and the model's fraction is within 8 percentage points of that (just after crossing 10% it may still be further off; train a few hundred more steps). The head learns not only that there are two ways round, but also how likely each is.
    真实系统里In real systems

    π0 与后续模型:π0 的动作专家与视觉-语言骨干共享 Transformer 的注意力,但有自己的权重,观测 token 不看动作 token(分块的注意力掩码,17.3 节);它的后继 π0.5 保留了 flow matching 动作专家。NVIDIA 的 GR00T N1(2025)用一个以 flow matching 训练的扩散 Transformer 作为人形机器人的动作模块。 为什么是 flow matching:与 Diffusion Policy(Chi 等,2023,22.2 节)的 DDPM/DDIM 相比,它的训练目标更简单,没有噪声调度表要调,确定性的 ODE 采样用 10 步左右就够;但两者在数学上很接近(第 5 节),具体选哪个常常取决于工程习惯。 动作块:一次生成多步动作并按“滚动时域”执行(22.2 节),既摊薄了推理延迟,也让多模态动作在一个块内保持一致,不会在两个模式之间来回跳。 加速:需要更少步数时,可以用 reflow 或蒸馏,例如 Consistency Policy(Prasad 等,2024)把扩散策略蒸馏成一到几步的策略。 数据侧的约定:动作要按数据集统计量归一化到单位尺度,使高斯噪声和动作的尺度相当;部署时必须用同一套统计量反归一化(17.5 节对动作 token 也有同样的要求)。

    π0 and its successors: π0's action expert shares the transformer's attention with the vision-language backbone but has its own weights, and the observation tokens do not attend to the action tokens (a block-wise attention mask, Lesson 17.3); its successor π0.5 keeps a flow-matching action expert. NVIDIA's GR00T N1 (2025) uses a diffusion transformer trained with flow matching as the action module of a humanoid. Why flow matching: compared with the DDPM/DDIM of Diffusion Policy (Chi et al., 2023; Lesson 22.2), the training target is simpler, there is no noise schedule to tune, and deterministic ODE sampling needs only about 10 steps; but mathematically the two are close (Section 5), and the choice often comes down to engineering habit. Action chunks: generating several steps at once and executing them in a receding-horizon fashion (Lesson 22.2) both amortizes the inference latency and keeps a multimodal action consistent within a chunk, instead of jumping back and forth between modes. Speed-ups: when even fewer steps are needed, reflow or distillation help; Consistency Policy (Prasad et al., 2024), for example, distils a diffusion policy into one with one to a few steps. Data conventions: actions must be normalized to unit scale with dataset statistics so that the Gaussian noise and the actions have comparable scales, and deployment must de-normalize with exactly the same statistics (Lesson 17.5 asks the same of action tokens).

    7常见误区Common pitfalls

    误区 1:时间方向和符号的约定混用Pitfall 1: mixing up the conventions for time and sign

    本节(和 Lipman 等)约定 \(t=0\) 是噪声、\(t=1\) 是数据,目标是 \(\mathbf x_1-\mathbf x_0\);DDPM 的步数从数据(第 0 步)数到噪声,有的代码库把噪声放在 \(t=1\),目标写成 \(\boldsymbol\varepsilon-\mathbf x\)。照着一篇论文的公式、用另一份代码的约定,就会朝错误的方向积分,样本一路跑出画面,或者训练目标和采样器的时间轴对不上。最便宜的检查:训练完后看 \(t\) 接近噪声一端时的场,它应当大致指向数据的均值(FIG 2 把 \(t\) 拉到 0 时的样子),并且一步 Euler 应当把所有样本送到 \(\mathbb E[\mathbf x_1]\) 附近(FIG 3)。

    This lesson (like Lipman et al.) puts noise at \(t=0\) and data at \(t=1\) with target \(\mathbf x_1-\mathbf x_0\); DDPM counts steps from the data (step 0) to the noise, and some codebases put the noise at \(t=1\) and write the target as \(\boldsymbol\varepsilon-\mathbf x\). Take the formulas from one paper and the convention from another codebase, and you integrate in the wrong direction (the samples run off the screen) or the training target and the sampler disagree about the time axis. The cheapest check: after training, look at the field near the noise end; it should point roughly at the mean of the data (FIG 2 with \(t\) dragged to 0), and one Euler step should send every sample near \(\mathbb E[\mathbf x_1]\) (FIG 3).

    误区 2:拿损失值判断模型好坏Pitfall 2: judging the model by its loss value

    CFM 损失有一个地板 \(\mathcal L^*\),它由数据和配对决定,与网络无关。FIG 2 里双月的损失从 4.38 降到 3.0 附近就停了,看上去“没怎么学”,其实场误差从 1.34 降到了 0.06;FIG 4 里 reflow 的损失比独立配对低两个数量级,却不代表它的网络更准,只是它的地板低。比较模型要看样本(SW、FID 这类分布距离,机器人上则是任务成功率),或者像本页这样在有精确答案的玩具问题上量场误差。

    The CFM loss has a floor \(\mathcal L^*\) set by the data and the pairing, not by the network. In FIG 2 the moons' loss falls from 4.38 to about 3.0 and stops, which looks like "hardly learning", yet the field error has dropped from 1.34 to 0.06; in FIG 4 reflow's loss is two orders of magnitude below that of independent pairing, which does not make its network more accurate, only its floor lower. Compare models by their samples (distribution distances such as SW or FID, and task success rates on robots), or, as on this page, by the field error on a toy problem with a known answer.

    误区 3:以为“直线路径”意味着一步就够Pitfall 3: assuming "straight paths" means one step is enough

    直的是条件路径;独立配对时,平均出来的边缘轨迹是弯的。一步 Euler 的结果是条件均值 \(\mathbb E[\mathbf x_1]\)(无条件时)或 \(\mathbb E[\mathbf a\mid\mathbf o]\)(有条件时),也就是均方误差回归的答案,多模态全部丢掉:FIG 3 的样本云缩成一个点,FIG 5 的碰撞率超过 90%。想用一两步,就要先把轨迹拉直(OT 配对、reflow)或做蒸馏,并在任务指标上验证。

    What is straight is the conditional path; with independent pairing the averaged marginal trajectories are curved. One Euler step returns the conditional mean, \(\mathbb E[\mathbf x_1]\) (unconditionally) or \(\mathbb E[\mathbf a\mid\mathbf o]\) (with a condition), which is exactly the answer of mean-squared-error regression with every mode thrown away: FIG 3's sample cloud shrinks to a point and FIG 5's collision rate exceeds 90%. To use one or two steps, straighten the trajectories first (OT pairing, reflow) or distil, and verify on the task metric.

    误区 4:在条件生成里跨条件做 OT 配对Pitfall 4: OT pairing across conditions in conditional generation

    小批量 OT 把每个噪声配给离它最近的数据点。如果批次里混着不同的观测 \(\mathbf o\),OT 会把为观测 A 准备的噪声配给观测 B 的动作,网络学到的就不再是 \(p(\mathbf a\mid\mathbf o)\)。条件生成时,要么只在同一个条件内部做 OT,要么就用独立配对;reflow 则要用同一个条件下的 ODE 生成配对。另外,OT 配对是在整个批次上求的,批次很小时它只是一个粗糙的近似。

    Minibatch OT pairs each noise sample with the nearest data point. If a batch mixes different observations \(\mathbf o\), OT pairs noise meant for observation A with an action of observation B, and the network no longer learns \(p(\mathbf a\mid\mathbf o)\). For conditional generation, either do OT only within one condition or use independent pairing; reflow must likewise generate its pairs with the ODE under the same condition. Also, the OT pairing is solved over one batch, so with small batches it is only a rough approximation.

    误区 5:数据尺度和 \(t\to1\) 的数值问题Pitfall 5: data scale and the numerics near \(t\to1\)

    噪声是标准高斯,所以数据(动作)也要归一化到差不多的尺度:如果关节角以弧度计、夹爪以毫米计,同一份噪声对它们的意义完全不同,路径的形状也被扭曲。另一个陷阱是用 \(\mathbf u_t(\mathbf x\mid\mathbf x_1)=(\mathbf x_1-\mathbf x)/(1-t)\) 这个形式算目标:\(t\) 接近 1 时分母趋于零,数值噪声被放大。直接用 \(\mathbf x_1-\mathbf x_0\)(本页的做法)就没有这个问题;如果网络改为预测 \(\hat{\mathbf x}_1\) 再换算成速度,同样会在 \(t\to1\) 除以 \(1-t\),要截断 \(t\) 的范围或对损失加权。

    The noise is a standard Gaussian, so the data (actions) must be normalized to a similar scale: with joint angles in radians and the gripper in millimetres, the same noise means something completely different for each, and the shape of the path gets distorted. Another trap is computing the target in the form \(\mathbf u_t(\mathbf x\mid\mathbf x_1)=(\mathbf x_1-\mathbf x)/(1-t)\): as \(t\) approaches 1 the denominator goes to zero and numerical noise is amplified. Using \(\mathbf x_1-\mathbf x_0\) directly (as this page does) avoids it; if the network instead predicts \(\hat{\mathbf x}_1\) and converts it to a velocity, it divides by \(1-t\) as \(t\to1\) too, so clip the range of \(t\) or weight the loss.

    8小练习Check yourself

    1. 一对训练样本:\(\mathbf x_0=(1,-1)\),\(\mathbf x_1=(3,1)\),\(t=0.25\)。网络的输入位置 \(\mathbf x_t\) 和回归目标分别是什么?1. One training pair: \(\mathbf x_0=(1,-1)\), \(\mathbf x_1=(3,1)\), \(t=0.25\). What are the network's input position \(\mathbf x_t\) and its regression target?
    \(\mathbf x_t=0.75\,(1,-1)+0.25\,(3,1)=(1.5,-0.5)\);直线的速度与 \(t\) 无关,是 \(\mathbf x_1-\mathbf x_0=(2,2)\)。选项 A 把 \(t\) 和 \(1-t\) 的位置弄反了;选项 C 把速度乘了 \(t\)(那是位移的一部分,不是速度);选项 D 回归的是数据点本身,那是“预测 \(\mathbf x_1\)”的参数化,需要再换算才是速度。\(\mathbf x_t=0.75\,(1,-1)+0.25\,(3,1)=(1.5,-0.5)\); the line's velocity does not depend on \(t\): \(\mathbf x_1-\mathbf x_0=(2,2)\). Option A swaps \(t\) and \(1-t\); option C multiplies the velocity by \(t\) (that is part of the displacement, not a velocity); option D regresses the data point itself, the "predict \(\mathbf x_1\)" parametrization, which needs a conversion before it is a velocity.
    2. 训练了很久,CFM 损失停在约 2.9,不再下降。最可能的解释是什么?2. After long training the CFM loss sits at about 2.9 and will not go lower. What is the most likely explanation?
    \(\mathcal L_{\mathrm{CFM}}=\mathcal L_{\mathrm{FM}}+\mathcal L^*\),而 \(\mathcal L^*=\mathbb E\|\mathbf u_t(\mathbf x_t)-(\mathbf x_1-\mathbf x_0)\|^2\) 与 \(\theta\) 无关。即使网络完美(\(\mathbf v_\theta=\mathbf u_t\)),损失也等于 \(\mathcal L^*\),FIG 2 里双月的 \(\mathcal L^*=2.89\)。选项 A、B 追求损失为零,而在独立配对下这不可能;选项 D 会让网络只学会一个时刻的场,无法积分。\(\mathcal L_{\mathrm{CFM}}=\mathcal L_{\mathrm{FM}}+\mathcal L^*\), and \(\mathcal L^*=\mathbb E\|\mathbf u_t(\mathbf x_t)-(\mathbf x_1-\mathbf x_0)\|^2\) does not depend on \(\theta\). Even a perfect network (\(\mathbf v_\theta=\mathbf u_t\)) has loss \(\mathcal L^*\); for FIG 2's moons \(\mathcal L^*=2.89\). Options A and B chase a zero loss, which is impossible with independent pairing; option D would teach the network the field at one time only, which cannot be integrated.
    3. 用独立配对和直线路径训练出精确的 \(\mathbf u_t\),从 \(\mathbf x_0\sim\mathcal N(\mathbf 0,I)\) 出发只做一步 Euler(\(h=1\))。得到的样本是什么?3. With independent pairing and the straight path, the exact \(\mathbf u_t\) has been learned. Starting from \(\mathbf x_0\sim\mathcal N(\mathbf 0,I)\), you take a single Euler step (\(h=1\)). What samples do you get?
    \(t=0\) 时 \(\mathbf x_t=\mathbf x_0\) 与 \(\mathbf x_1\) 独立,后验就是整个数据分布,\(\mathbf u_0(\mathbf x)=\mathbb E[\mathbf x_1]-\mathbf x\)。于是 \(\mathbf x_0+\mathbf u_0(\mathbf x_0)=\mathbb E[\mathbf x_1]\),与起点无关(FIG 3 的任务)。选项 B 混淆了条件路径与边缘轨迹;选项 D 是余弦路径在数据均值为零时的情况(\(\dot\alpha_0\mathbb E[\mathbf x_1]=0\)),不是直线路径。At \(t=0\), \(\mathbf x_t=\mathbf x_0\) is independent of \(\mathbf x_1\), so the posterior is the whole data distribution and \(\mathbf u_0(\mathbf x)=\mathbb E[\mathbf x_1]-\mathbf x\). Hence \(\mathbf x_0+\mathbf u_0(\mathbf x_0)=\mathbb E[\mathbf x_1]\), independent of the start (FIG 3's task). Option B confuses the conditional paths with the marginal trajectories; option D describes the cosine path when the data mean is zero (\(\dot\alpha_0\mathbb E[\mathbf x_1]=0\)), not the straight path.
    4. 在直线路径上,网络在 \((\mathbf x_t,t)\) 处输出速度 \(\hat{\mathbf v}\)。它隐含的“去噪后的数据”估计 \(\hat{\mathbf x}_1\) 是什么?4. On the straight path, the network outputs the velocity \(\hat{\mathbf v}\) at \((\mathbf x_t,t)\). What estimate \(\hat{\mathbf x}_1\) of the "denoised data" does it imply?
    沿一条直线,从 \(\mathbf x_t\) 以速度 \(\mathbf x_1-\mathbf x_0\) 再走剩下的 \(1-t\) 时间就到 \(\mathbf x_1\):\(\mathbf x_1=\mathbf x_t+(1-t)(\mathbf x_1-\mathbf x_0)\)。取条件均值即得 \(\hat{\mathbf x}_1=\mathbf x_t+(1-t)\hat{\mathbf v}\)。同理往回走 \(t\) 时间得到噪声估计 \(\hat{\boldsymbol\varepsilon}=\mathbf x_t-t\hat{\mathbf v}\)(选项 C 的形式是往回走,但时长用错了)。这就是速度、噪声预测和 19.6 节得分之间的换算。Along a line, starting at \(\mathbf x_t\) and moving at velocity \(\mathbf x_1-\mathbf x_0\) for the remaining time \(1-t\) reaches \(\mathbf x_1\): \(\mathbf x_1=\mathbf x_t+(1-t)(\mathbf x_1-\mathbf x_0)\). Taking conditional means gives \(\hat{\mathbf x}_1=\mathbf x_t+(1-t)\hat{\mathbf v}\). Walking back for time \(t\) likewise gives the noise estimate \(\hat{\boldsymbol\varepsilon}=\mathbf x_t-t\hat{\mathbf v}\) (option C walks backwards but for the wrong duration). These are the conversions between velocity, noise prediction and the score of Lesson 19.6.
    5. 一个 flow matching 动作头用 10 步积分时表现很好,但为了提高控制频率,你想只用 1 到 2 步。哪种做法最直接地针对问题的根源?5. A flow-matching action head works well with 10 integration steps, but to raise the control rate you want only 1 or 2. Which change most directly addresses the root of the problem?
    少步失败不是因为场学得不准,而是因为精确的边缘轨迹本身是弯的:即使网络完美,一步 Euler 也只给出条件均值(FIG 3、FIG 5)。只有让轨迹变直(FIG 4)或用蒸馏直接学“一步走到哪里”,才能在少步时保留多模态。选项 A 不改变轨迹的弯曲;选项 C 正好丢掉多模态,FIG 5 里的红线就是后果;选项 D 让网络只学 \(t=0\) 的场,而那个场只指向均值。Few-step sampling fails not because the field is learned badly but because the exact marginal trajectories are themselves curved: even with a perfect network, one Euler step gives only the conditional mean (FIG 3, FIG 5). Only straighter trajectories (FIG 4), or distillation that learns "where one step should land" directly, keep the modes with few steps. Option A leaves the curvature unchanged; option C throws the modes away, with FIG 5's red line as the consequence; option D trains only the field at \(t=0\), which merely points at the mean.

    9延伸阅读Further reading

    • Y. Lipman, R. T. Q. Chen, H. Ben-Hamu, M. Nickel & M. Le, Flow Matching for Generative Modeling, ICLR 2023 — 条件 flow matching 与它和 FM 梯度相同的定理,高斯条件路径,“最优传输”直线路径conditional flow matching and the theorem that its gradient equals FM's, Gaussian conditional paths, the "optimal transport" straight path
    • X. Liu, C. Gong & Q. Liu, Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow, ICLR 2023 — rectified flow 与 reflowrectified flow and reflow
    • M. S. Albergo & E. Vanden-Eijnden, Building Normalizing Flows with Stochastic Interpolants, ICLR 2023 — 同一想法的随机插值视角the same idea from the viewpoint of stochastic interpolants
    • A. Tong et al., Improving and Generalizing Flow-Based Generative Models with Minibatch Optimal Transport, Transactions on Machine Learning Research, 2024; A.-A. Pooladian et al., Multisample Flow Matching: Straightening Flows with Minibatch Couplings, ICML 2023 — 小批量 OT 配对minibatch OT pairing
    • Y. Lipman et al., Flow Matching Guide and Code, arXiv, 2024 — 系统的教程与参考实现a systematic tutorial with reference code
    • Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon & B. Poole, Score-Based Generative Modeling through Stochastic Differential Equations, ICLR 2021 — 概率流 ODE,扩散与 flow 的桥梁the probability-flow ODE, the bridge between diffusion and flows
    • J.-D. Benamou & Y. Brenier, A Computational Fluid Mechanics Solution to the Monge–Kantorovich Mass Transfer Problem, Numerische Mathematik, 2000 — 最优传输的动态(流体)形式the dynamic (fluid) formulation of optimal transport
    • P. Esser et al., Scaling Rectified Flow Transformers for High-Resolution Image Synthesis, ICML 2024 — Stable Diffusion 3:大规模直线路径模型与时间抽样分布Stable Diffusion 3: straight-path models at scale and the choice of time distribution
    • K. Black et al., π0: A Vision-Language-Action Flow Model for General Robot Control, arXiv, 2024 — 第 6 节的 flow matching 动作专家the flow-matching action expert of Section 6
    • C. Chi, S. Feng, Y. Du, Z. Xu, E. Cousineau, B. Burchfiel & S. Song, Diffusion Policy: Visuomotor Policy Learning via Action Diffusion, RSS 2023 — 扩散版的动作生成,22.2 节详解the diffusion version of action generation, covered in Lesson 22.2
    • NVIDIA, GR00T N1: An Open Foundation Model for Generalist Humanoid Robots, arXiv, 2025 — 以 flow matching 训练的扩散 Transformer 动作模块an action module built as a diffusion transformer trained with flow matching