扩散与 Score-based 模型Diffusion & Score-Based Models
扩散模型的想法朴素得出奇:先把数据一点一点加噪,直到变成纯高斯噪声;再训练一个网络,在每个噪声水平上猜出“加进去的是哪份噪声”;生成时从纯噪声出发,一小步一小步把噪声去掉。训练只是许多噪声水平上的回归,稳定得像监督学习;采出的样本却能覆盖数据的每一个模式。本节先看清前向加噪的闭式解,以及去噪器、后验均值和 score 三者是同一个东西(Tweedie 公式);然后在浏览器里现场训练一个 DDPM,看它的损失下限和 EMA;比较 DDPM 与 DDIM、采样步数和噪声日程;看 classifier-free guidance 怎样用多样性换取对指令的服从;最后对整段动作轨迹去噪,这正是 Diffusion Policy 的引擎。 The idea of a diffusion model is almost embarrassingly simple: add noise to the data a little at a time until it becomes pure Gaussian noise; train a network to guess, at every noise level, which noise was added; to generate, start from pure noise and remove it one small step at a time. Training is just regression over many noise levels, as stable as supervised learning, yet the samples cover every mode of the data. This lesson first looks at the closed form of the forward noising and at why the denoiser, the posterior mean and the score are one and the same thing (Tweedie's formula); then trains a DDPM live in your browser, with its loss floor and EMA; compares DDPM and DDIM, step counts and noise schedules; shows how classifier-free guidance trades diversity for obedience to a command; and finally denoises whole action chunks, which is the engine of Diffusion Policy.
1为什么机器人需要它Why robots need this
让人示教一个机械臂去抓桌上的杯子。同一个画面下,示教者有时抓左边那只,有时抓右边那只;绕开障碍物时,有时从上面绕,有时从下面绕。示教数据里的动作分布是多模态的。用均方误差去回归动作(16.2 节),学到的是条件均值:两只杯子的中点,或者正好穿过障碍物的那条“平均路线”。机器人需要的不是平均,而是一个能从整个动作分布里采样的模型:每次给出一个具体、可行、属于某个模式的动作。
前面几节的生成模型各有短板。VAE(19.1 节)的样本偏模糊;GAN(19.2 节)训练不稳,还会丢模式;normalizing flow(19.3 节)要求网络可逆,结构受限;自回归模型(19.4 节)要把连续动作切成 token;Energy-based 模型(19.5 节)训练和采样都要跑 MCMC。扩散模型把训练变成一个普通的回归问题,把生成变成很多步“去一点噪声”,既稳定又能覆盖所有模式,还能直接扩展到高维:几百万像素的图像,或者 16 步 × 7 维的一整段机械臂动作。
今天的图像、视频生成模型大多是扩散模型或它的近亲;在机器人里,Diffusion Policy(Chi 等,2023)用它生成动作序列,Diffuser(Janner 等,2022)用它生成整条轨迹。本节讲这台“引擎”本身:前向过程、score、训练目标、采样器、噪声日程和引导;22.2 节再讲怎样把它装进策略里。
Teach a robot arm by demonstration to pick up a cup on the table. In the same scene the demonstrator sometimes takes the left cup and sometimes the right one; to get around an obstacle, sometimes over it and sometimes under. The distribution of demonstrated actions is multimodal. Regressing the action with a squared error (Lesson 16.2) learns the conditional mean: the midpoint between the two cups, or the "average route" straight through the obstacle. What the robot needs is not the average but a model it can sample from: each time, one concrete, feasible action that belongs to one of the modes.
The generative models so far each have a weak spot. VAEs (Lesson 19.1) give blurry samples; GANs (Lesson 19.2) are unstable to train and drop modes; normalizing flows (Lesson 19.3) need invertible networks, which constrains the architecture; autoregressive models (Lesson 19.4) have to cut continuous actions into tokens; energy-based models (Lesson 19.5) need MCMC both for training and for sampling. A diffusion model turns training into an ordinary regression problem and generation into many steps of "remove a little noise". It is stable, it covers all modes, and it scales directly to high dimensions: images of millions of pixels, or a whole chunk of arm actions of 16 steps × 7 dimensions.
Most of today's image and video generators are diffusion models or close relatives; in robotics, Diffusion Policy (Chi et al., 2023) uses one to generate action sequences, and Diffuser (Janner et al., 2022) to generate whole trajectories. This lesson is about the engine itself: the forward process, the score, the training objective, the samplers, the noise schedules and guidance. Lesson 22.2 then fits it into a policy.
2直觉:把数据磨成噪声,再学着走回来Intuition: grind the data into noise, then learn the way back
本节的数据是示教者让夹爪去的位置 \((x,y)\),已经归一化到大约单位尺度(第 7 节会说为什么必须归一化)。默认的数据集是“4 个杯子”:四个位置被选中的频率分别是 40%、30%、20%、10%,每个位置周围有一点散布 \(\sigma_{\mathrm d}=0.16\)。另外两个数据集是“圆形盖沿”(沿一圈的 8 个抓取点)和“两条弧形把手”。它们都是高斯混合,所以下面每一个 score、每一个后验都能精确算出来,网络学得好不好也能和真值比。
前向过程每一步都把点往原点缩一点,再加一点高斯噪声:\(\mathbf x_t=\sqrt{1-\beta_t}\,\mathbf x_{t-1}+\sqrt{\beta_t}\,\boldsymbol\varepsilon\)。缩一点是为了让总方差保持在 1 附近,不让噪声越加越大。高斯加高斯还是高斯(4.4 节),所以走了 \(t\) 步之后有一个闭式:\(\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon\),其中 \(\bar\alpha_t\) 从 1 单调降到几乎 0。不需要真的一步步模拟,任何 \(t\) 都能一步跳到。拖动下面的 \(t\),看四个杯子怎样被“磨”成一团标准高斯。
时间方向的约定:本节沿用 DDPM 的写法,\(t\) 数的是加噪的步数:\(t=0\) 是数据 \(\mathbf x_0\),\(t=T=100\) 几乎是纯噪声,生成时 \(t\) 从 \(T\) 倒着走回 0。19.7 节的 flow matching 用的是相反的约定(flow matching 文献和 π0 都这样写):\(t\in[0,1]\),\(t=0\) 是噪声,\(t=1\) 是数据,那里的 \(\mathbf x_0\) 是噪声(本节的 \(\boldsymbol\varepsilon\),约等于 \(\mathbf x_T\)),\(\mathbf x_1\) 才是数据(本节的 \(\mathbf x_0\));两边的时间大致对应为 \(t_{19.7}\approx1-t/T\)。
The data in this lesson are the positions \((x,y)\) a demonstrator sent the gripper to, already normalized to roughly unit scale (Section 7 says why this is necessary). The default dataset is "4 cups": the four positions are chosen 40%, 30%, 20% and 10% of the time, each with a little spread \(\sigma_{\mathrm d}=0.16\). The other two are a "round rim" (8 grasp points around a circle) and "two arc handles". All of them are Gaussian mixtures, so every score and every posterior below can be computed exactly, and what the network learns can be compared with the truth.
Each step of the forward process shrinks the point a little toward the origin and adds a little Gaussian noise: \(\mathbf x_t=\sqrt{1-\beta_t}\,\mathbf x_{t-1}+\sqrt{\beta_t}\,\boldsymbol\varepsilon\). The shrinking keeps the total variance near 1, so the noise does not keep growing. Gaussian plus Gaussian is Gaussian (Lesson 4.4), so after \(t\) steps there is a closed form, \(\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon\), with \(\bar\alpha_t\) falling monotonically from 1 to almost 0. Nothing has to be simulated step by step; any \(t\) can be reached in one jump. Drag \(t\) below and watch the four cups get ground into one standard Gaussian blob.
Time convention: this lesson follows DDPM, where \(t\) counts noising steps: \(t=0\) is the data \(\mathbf x_0\), \(t=T=100\) is almost pure noise, and generation runs \(t\) backwards from \(T\) to 0. The flow matching of Lesson 19.7 uses the opposite convention (as the flow-matching literature and π0 do): \(t\in[0,1]\) with \(t=0\) the noise and \(t=1\) the data, so there \(\mathbf x_0\) is the noise (this lesson's \(\boldsymbol\varepsilon\), roughly \(\mathbf x_T\)) and \(\mathbf x_1\) is the data (this lesson's \(\mathbf x_0\)); the two time axes correspond roughly as \(t_{19.7}\approx1-t/T\).
- 用“4 个杯子”和余弦日程,把 \(t\) 往大拖,直到 \(p_t\) 只剩一个峰(\(t=46\) 起)。四个峰不是一下子合并的:\(t=32\) 时剩 3 个,\(t=36\) 时剩 2 个。从这里开始,单看 \(\mathbf x_t\) 已经很难说它来自哪个杯子。With "4 cups" and the cosine schedule, drag \(t\) up until \(p_t\) has a single peak (from \(t=46\) on). The four peaks do not merge at once: 3 remain at \(t=32\), 2 at \(t=36\). From here on, \(\mathbf x_t\) alone says little about which cup it came from.
- 换成线性日程,找到 \(\bar\alpha_t\) 第一次低于 1% 的那一步(\(t=67\);任意数据集,\(t\le75\) 即可)。余弦日程要到 \(t=94\) 才低于 1%:线性日程把最后三分之一的步数都花在了几乎纯噪声上,峰也早在 \(t=24\) 就合并了。Switch to the linear schedule and find the first step at which \(\bar\alpha_t\) drops below 1% (\(t=67\); any dataset, \(t\le75\) counts). The cosine schedule only gets there at \(t=94\): the linear schedule spends its last third of steps on almost pure noise, and its peaks have already merged at \(t=24\).
走回来:去噪器就是后验均值,它指向 scoreThe way back: the denoiser is the posterior mean, and it points along the score
要倒着走,就得回答:看到一个带噪声的点 \(\mathbf x_t\),它原来最可能在哪?按平方误差算,最好的猜测是后验均值 \(\hat{\mathbf x}_0=\mathbb E[\mathbf x_0\mid\mathbf x_t]\)(最小均方误差估计,即 MMSE;后验见 4.6 节)。18.2 节已经给出了 Tweedie 公式:对高斯破坏,后验均值等于“输入加上噪声方差乘 score”。换成这里的缩放记号是 \(\sqrt{\bar\alpha_t}\,\hat{\mathbf x}_0=\mathbf x_t+(1-\bar\alpha_t)\nabla\log p_t(\mathbf x_t)\)。所以“学会去噪”和“学会 score \(\nabla\log p_t\)”是同一件事,score 就是从 \(\mathbf x_t\) 指向(缩放后的)最佳去噪猜测的方向。
下图把这件事画出来。注意后验 \(p(\mathbf x_0\mid\mathbf x_t)\) 本身是多模态的:噪声大时,一个点可能来自好几个杯子,后验均值就落在杯子之间的空地上,那里根本没有数据。这就是为什么扩散模型不能一步生成:一步去噪只能给出平均,必须走很多小步,每一步只需要局部的方向,后验在这一路上逐渐“下决心”。
To walk backwards we must answer: given a noisy point \(\mathbf x_t\), where did it most likely come from? Under squared error the best guess is the posterior mean \(\hat{\mathbf x}_0=\mathbb E[\mathbf x_0\mid\mathbf x_t]\) (the minimum-mean-squared-error, or MMSE, estimate; posteriors are those of Lesson 4.6). Lesson 18.2 already gave Tweedie's formula: for Gaussian corruption the posterior mean is "the input plus the noise variance times the score". In the scaled notation here it reads \(\sqrt{\bar\alpha_t}\,\hat{\mathbf x}_0=\mathbf x_t+(1-\bar\alpha_t)\nabla\log p_t(\mathbf x_t)\). So "learning to denoise" and "learning the score \(\nabla\log p_t\)" are the same thing, and the score is the direction from \(\mathbf x_t\) toward the (rescaled) best denoising guess.
The figure below draws exactly this. Notice that the posterior \(p(\mathbf x_0\mid\mathbf x_t)\) is itself multimodal: when the noise is large, one point could have come from several cups, and the posterior mean lands in the empty space between them, where there is no data at all. This is why a diffusion model cannot generate in one step: one denoising step can only give the average. It has to take many small steps, each needing only the local direction, and along the way the posterior gradually "makes up its mind".
- 把 \(t\) 调到 60 或以上,把 \(\mathbf x_t\) 拖到杯子 1 和杯子 2 中间偏下的位置(比如 (0, 0.47)):至少两个杯子各占 20% 以上的后验,紫点分成几团,而 \(\hat{\mathbf x}_0\) 离每个杯子都超过 \(3\sigma_{\mathrm d}\)(在 (0, 0.47)、\(t=60\) 时约为 6σ)。最优去噪器给出的是一个“不存在的位置”。Set \(t\) to 60 or more and drag \(\mathbf x_t\) to just below the middle between cups 1 and 2 (for example (0, 0.47)): at least two cups each take more than 20% of the posterior, the purple dots split into clusters, and \(\hat{\mathbf x}_0\) is more than \(3\sigma_{\mathrm d}\) away from every cup (about 6σ at (0, 0.47) and \(t=60\)). The optimal denoiser outputs a "position that does not exist".
- 把 \(t\) 降到 20 或以下,再把 \(\mathbf x_t\) 拖到某个杯子附近:这个杯子拿到 95% 以上的后验,\(\hat{\mathbf x}_0\) 落在它的 \(3\sigma_{\mathrm d}\) 之内。噪声小时,后验已经“下了决心”。Lower \(t\) to 20 or less and drag \(\mathbf x_t\) close to one cup: that cup takes more than 95% of the posterior and \(\hat{\mathbf x}_0\) lands within \(3\sigma_{\mathrm d}\) of it. At low noise the posterior has made up its mind.
扩散模型把“生成一个多模态分布的样本”这件难事,拆成了很多件容易的事:在每个噪声水平上做一次回归(预测噪声,等价于预测后验均值,等价于 score)。每一次回归的答案都是平均,平均本身会落在模式之间;但从高噪声走到低噪声,每一步只往平均的方向挪一小步,再加一点新的随机性,后验就越来越尖,最后落在某一个模式上。均方误差回归之所以“取平均”,扩散模型之所以“不取平均”,区别只在于是一步走完还是很多步走完。
A diffusion model breaks the hard task "draw a sample from a multimodal distribution" into many easy ones: at each noise level, one regression (predict the noise, which is equivalent to predicting the posterior mean, which is equivalent to the score). Each regression answer is an average, and the average itself lies between the modes; but going from high noise to low noise, each step moves only a little toward the average and adds a little fresh randomness, so the posterior gets sharper and sharper and finally lands on one mode. Squared-error regression "averages" and a diffusion model "does not" only because one takes a single step and the other takes many.
3动手:现场训练一个 DDPMPlay: train a DDPM live
现在不再用精确的 score,而是让网络去学。DDPM(Ho 等,2020)的训练循环只有四行:从数据里取一个 \(\mathbf x_0\),均匀地抽一个噪声步 \(t\in\{1,\dots,T\}\),抽一份噪声 \(\boldsymbol\varepsilon\sim\mathcal N(0,I)\),按闭式算出 \(\mathbf x_t\),然后让网络 \(\boldsymbol\varepsilon_\theta(\mathbf x_t,t)\) 回归 \(\boldsymbol\varepsilon\),损失是均方误差。这里 \(T=100\),网络是一个 9 → 64 → 64 → 2 的 ReLU MLP(4930 个参数):输入是 \(\mathbf x_t\) 的两个坐标加上 7 个时间特征(\(\pi t/T\)、\(2\pi t/T\)、\(4\pi t/T\) 的正弦和余弦,以及一个线性斜坡),输出是预测的噪声。优化器是 Adam,学习率 \(2\times10^{-3}\),批量 64(6.3 节)。
生成时用第 4 节要讲的 DDPM 采样器,在 100 个训练噪声水平里等间隔取 50 个,从 300 个纯噪声点出发往回走。为了让你看到训练过程,每一批样本都从当时权重的一份快照开始采样,采完立刻开始下一批。还有两处细节:EMA(指数滑动平均)权重 \(\bar\theta\leftarrow\bar\theta+0.01(\theta-\bar\theta)\),几乎所有扩散模型都用它来采样;损失下限:即使用精确的最优去噪器,损失也不会是 0(第 5 节解释),图里绿色虚线就是精确 \(\boldsymbol\varepsilon^\star\) 在同一个测试集上的损失。
Now we stop using the exact score and let a network learn it. The training loop of DDPM (Ho et al., 2020) is four lines: take an \(\mathbf x_0\) from the data, draw a noise step \(t\in\{1,\dots,T\}\) uniformly, draw noise \(\boldsymbol\varepsilon\sim\mathcal N(0,I)\), compute \(\mathbf x_t\) from the closed form, and regress the network \(\boldsymbol\varepsilon_\theta(\mathbf x_t,t)\) onto \(\boldsymbol\varepsilon\) with a squared-error loss. Here \(T=100\) and the network is a 9 → 64 → 64 → 2 ReLU MLP (4930 parameters): the input is the two coordinates of \(\mathbf x_t\) plus 7 time features (sine and cosine of \(\pi t/T\), \(2\pi t/T\) and \(4\pi t/T\), and a linear ramp), the output is the predicted noise. The optimizer is Adam with learning rate \(2\times10^{-3}\) and batch size 64 (Lesson 6.3).
To generate, we use the DDPM sampler of Section 4, taking 50 of the 100 training noise levels at even spacing and walking back from 300 points of pure noise. So that you can watch training happen, each batch of samples starts from a snapshot of the weights at that moment, and the next batch starts as soon as one finishes. Two more details: EMA (exponential moving average) weights \(\bar\theta\leftarrow\bar\theta+0.01(\theta-\bar\theta)\), which nearly every diffusion model samples with; and the loss floor: even the exact optimal denoiser does not reach zero loss (Section 5 explains why), and the green dashed line is the loss of the exact \(\boldsymbol\varepsilon^\star\) on the same test set.
- 用“4 个杯子”、EMA 权重,按“训练”:大约 1200 到 1800 步后(取决于种子),一批样本里 90% 以上落在数据上,四个杯子的份额与 40/30/20/10 的 TV 小于 0.08。连只占 10% 的杯子 4 也分到了它的一份:没有丢模式。With "4 cups" and EMA weights, press "Train": after roughly 1200 to 1800 steps (depending on the seed) a batch has more than 90% of its samples on the data and the cup shares are within TV 0.08 of 40/30/20/10. Even cup 4, chosen only 10% of the time, gets its share: no mode is dropped.
- 训练到 2000 步以上后切到“原始权重”并继续训练:每批样本的份额明显地上下跳动,几批之内 TV 就会超过 0.1(第 2000 步之后约有 10% 到 45% 的批次如此,取决于种子);用 EMA 权重时,同一阶段几乎没有一批超过 0.1。Train past 2000 steps, then switch to "raw weights" and keep training: the shares jump around visibly from batch to batch, and within a few batches the TV exceeds 0.1 (roughly 10% to 45% of the batches after step 2000 do, depending on the seed); with EMA weights almost no batch in the same phase goes above 0.1.
(1) 测试损失从约 1.35(种子 1)很快降到 0.35(500 步),再慢慢爬向下限 0.295:1500 步时约 0.31。它永远到不了 0,因为同一个 \(\mathbf x_t\) 可能来自许多不同的 \((\mathbf x_0,\boldsymbol\varepsilon)\),噪声猜不准是本性。 (2) 损失接近下限不等于样本好。换成“两条弧形把手”训练 4000 步:测试损失只比下限高约 6%(0.374 对 0.354),却还有约五分之一的样本落在把手之外。细长的结构要求网络在噪声很小的那些步上非常精确,而这些步在平均损失里只占很小的权重。 (3) 原始权重在每一步都被一小批数据推来推去,各个模式之间的“分账”会随之抖动;EMA 把最近约 100 步的权重平均起来,样本的份额就稳了。
(1) The test loss drops fast from about 1.35 (seed 1) to 0.35 (500 steps), then creeps toward the floor of 0.295: about 0.31 at 1500 steps. It never reaches 0, because the same \(\mathbf x_t\) could have come from many different \((\mathbf x_0,\boldsymbol\varepsilon)\); being unable to pin down the noise is in the nature of the problem. (2) A loss near the floor does not mean good samples. Train on "two arc handles" for 4000 steps: the test loss is only about 6% above its floor (0.374 against 0.354), yet about one sample in five still lies off the handles. Thin structures need the network to be very precise at the low-noise steps, and those steps carry little weight in the average loss. (3) The raw weights are pushed around by a small batch of data at every step, so the "split" between the modes jitters with them; EMA averages the weights over roughly the last 100 steps, and the shares settle.
4另外两个视角:采样器与引导Two more views: samplers and guidance
采样器、步数与噪声日程Samplers, step counts and noise schedules
有了 \(\boldsymbol\varepsilon_\theta\),怎样从 \(\mathbf x_T\sim\mathcal N(0,I)\) 走回 \(\mathbf x_0\)?每一步都先用网络猜出 \(\hat{\mathbf x}_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon_\theta)/\sqrt{\bar\alpha_t}\),然后决定下一个点 \(\mathbf x_s\)(\(s<t\))放在哪里。DDPM(祖先采样)把 \(\mathbf x_s\) 从“已知 \(\mathbf x_t\) 和 \(\hat{\mathbf x}_0\) 时 \(\mathbf x_s\) 的高斯后验”里抽出来,每步都加新的噪声;DDIM(Song 等,2021)取同一族里噪声为零的那个成员:\(\mathbf x_s=\sqrt{\bar\alpha_s}\,\hat{\mathbf x}_0+\sqrt{1-\bar\alpha_s}\,\boldsymbol\varepsilon_\theta\),是确定性的。两者用同一个训练好的网络,都可以跳着走:只访问 \(T\) 个训练噪声水平中的 \(N\) 个。网络调用次数就是 \(N\),这是推理耗时的主要来源。
为了把“采样器”和“网络学得好不好”分开,下图用精确的 \(\boldsymbol\varepsilon^\star\) 代替网络:所有误差都来自采样器本身的离散化。
Given \(\boldsymbol\varepsilon_\theta\), how do we walk from \(\mathbf x_T\sim\mathcal N(0,I)\) back to \(\mathbf x_0\)? At each step the network first guesses \(\hat{\mathbf x}_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon_\theta)/\sqrt{\bar\alpha_t}\), and then we decide where to put the next point \(\mathbf x_s\) (\(s<t\)). DDPM (ancestral sampling) draws \(\mathbf x_s\) from the Gaussian posterior of \(\mathbf x_s\) given \(\mathbf x_t\) and \(\hat{\mathbf x}_0\), adding fresh noise at every step; DDIM (Song et al., 2021) takes the zero-noise member of the same family, \(\mathbf x_s=\sqrt{\bar\alpha_s}\,\hat{\mathbf x}_0+\sqrt{1-\bar\alpha_s}\,\boldsymbol\varepsilon_\theta\), which is deterministic. Both use the same trained network, and both can skip: they visit only \(N\) of the \(T\) training noise levels. The number of network calls is \(N\), and that is where most of the inference time goes.
To separate "the sampler" from "how well the network learned", the figure below uses the exact \(\boldsymbol\varepsilon^\star\) in place of a network: every error comes from the sampler's own discretization.
- 用 DDPM、“4 个杯子”、余弦日程,把步数从 100 逐步减到 5(落在数据上的比例从 99% 降到约 92%,丢掉的样本卡在杯子之间),再减到 1:所有 600 个样本落在同一个点上,即数据的均值 (−0.19, 0.29),一个都不在杯子里。一步去噪就是 FIG 2 的后验均值。With DDPM, "4 cups" and the cosine schedule, lower the steps from 100 to 5 (the share on the data falls from 99% to about 92%; the missing samples are stuck between cups), then to 1: all 600 samples land on one single point, the mean of the data (−0.19, 0.29), and not one is in a cup. A single denoising step is the posterior mean of FIG 2.
- 分别在 DDPM 和 DDIM 下各按一次“同一个 x_T 再采一次”:DDPM 的粒子会跳到别处(最大位移约 3,常常换了杯子),DDIM 的结果一模一样(位移 0)。DDIM 把每个起点 \(\mathbf x_T\) 确定地映射到一个样本。Press "Same x_T again" once under DDPM and once under DDIM: under DDPM the particles jump elsewhere (largest displacement about 3, often to another cup), under DDIM the result is identical (displacement 0). DDIM maps each starting point \(\mathbf x_T\) deterministically to one sample.
几个数字(600 个粒子,默认噪声):“4 个杯子”上,余弦日程的 DDPM 在 10 步时还有 98% 的样本落在数据上,5 步 92%,2 步只剩 34%;DDIM 在 5 步时是 93%,10 步 99%。在这个二维的小例子里两者差别不大;在图像上,DDIM 的少步数优势更明显。日程的影响在“圆形盖沿”上最清楚:DDPM 5 步,余弦日程 78%,线性日程只有 32%;10 步时是 94% 对 80%。线性日程把大量步数花在 \(\bar\alpha_t\approx0\) 的纯噪声区(FIG 1),步数一少,真正决定形状的中低噪声段就分不到几步了。
Some numbers (600 particles, the default noise): on "4 cups" with the cosine schedule, DDPM still puts 98% of its samples on the data at 10 steps, 92% at 5 steps and only 34% at 2 steps; DDIM gives 93% at 5 steps and 99% at 10. In this small 2D example the two are close; on images DDIM's advantage at few steps is larger. The schedule matters most on the "round rim": DDPM with 5 steps reaches 78% with the cosine schedule but only 32% with the linear one; at 10 steps it is 94% against 80%. The linear schedule spends many of its steps in the pure-noise region where \(\bar\alpha_t\approx0\) (FIG 1), so with few steps the low and medium noise levels, where the shape is actually decided, get hardly any.
条件生成与 classifier-free guidanceConditioning and classifier-free guidance
机器人要的通常是条件分布:给定指令 \(c\)(“拿马克杯”还是“拿碗”)或者给定观测,生成动作。最直接的做法是把 \(c\) 也输进去,训练 \(\boldsymbol\varepsilon_\theta(\mathbf x_t,t,c)\)。实际中这样学到的条件模型常常“不够听话”:一部分样本无视条件。Classifier-free guidance(Ho 与 Salimans,2021)的办法是:训练时以一定概率(常见 10% 到 20%)把 \(c\) 换成“空条件”\(\varnothing\),于是同一个网络既是条件模型也是无条件模型;采样时把两者外推:
A robot usually needs a conditional distribution: generate an action given a command \(c\) ("pick the mug" or "pick the bowl") or given an observation. The direct way is to feed \(c\) in as well and train \(\boldsymbol\varepsilon_\theta(\mathbf x_t,t,c)\). In practice a conditional model learned this way is often "not obedient enough": part of its samples ignore the condition. Classifier-free guidance (Ho and Salimans, 2021) fixes this as follows: during training, replace \(c\) by an "empty condition" \(\varnothing\) with some probability (10% to 20% is common), so that one network is both the conditional and the unconditional model; at sampling time, extrapolate from one to the other:
\(w=0\) 是无条件模型,\(w=1\) 是条件模型本身,\(w>1\) 是“比条件模型更条件”。下图沿用“4 个杯子”的数据,但把四个物体分成两类:1 号和 4 号是马克杯(紫),2 号和 3 号是碗(绿)。条件模型是精确的,但故意做成“漏标”的:它以权重 \(\lambda\) 忽略指令,即 \(p_\theta(\mathbf x\mid c)=(1-\lambda)\,p(\mathbf x\mid c)+\lambda\,p(\mathbf x)\),模拟一个没学好条件的网络。
\(w=0\) is the unconditional model, \(w=1\) the conditional model itself, and \(w>1\) is "more conditional than the conditional model". The figure below reuses the "4 cups" data but splits the four objects into two classes: objects 1 and 4 are mugs (purple), 2 and 3 are bowls (green). The conditional model is exact but deliberately "leaky": with weight \(\lambda\) it ignores the command, \(p_\theta(\mathbf x\mid c)=(1-\lambda)\,p(\mathbf x\mid c)+\lambda\,p(\mathbf x)\), imitating a network that has not quite learned the condition.
- 保持漏标 \(\lambda\) 不低于 0.4(默认 0.4)。\(w=1\) 时只有约 78%(马克杯)或 82%(碗)的样本听话;把 \(w\) 加大到 4 以内,让 97% 以上的样本落在指令要的物体上(\(w=2.5\) 时约 98%)。Keep the leak \(\lambda\) at 0.4 or higher (the default is 0.4). At \(w=1\) only about 78% (mug) or 82% (bowl) of the samples obey; raise \(w\), staying at 4 or below, until more than 97% land on the requested objects (about 98% at \(w=2.5\)).
- 指令选“拿马克杯”,把 \(w\) 一路加到 6 以上(比如 8):所有样本都听话了,但较少的 4 号马克杯的份额从数据里的 20% 掉到 8% 以下(\(w=8\) 时约 6%)。引导把样本推向“最不像碗”的地方,代价是多样性。Choose "pick the mug" and push \(w\) above 6 (to 8, say): every sample obeys, but the rarer mug no. 4 drops from its 20% in the data to below 8% (about 6% at \(w=8\)). Guidance pushes the samples to where they look "least like a bowl", and pays in diversity.
层级 VAE:前向过程是一个固定的、没有参数的“编码器”,把 \(\mathbf x_0\) 编码成 \(T\) 层隐变量 \(\mathbf x_1,\dots,\mathbf x_T\),解码器就是反向过程;训练目标是 ELBO,展开后正是加权的噪声回归(第 5 节;这就是 19.1 节说的“层数很多、编码器固定的 VAE”)。 Score 与能量:网络学的是 \(\nabla\log p_t\),也就是能量 \(-E\) 的梯度;配分函数在求梯度时消失了,所以不需要像 19.5 节那样对付它,采样就是一串带退火的 Langevin 式更新。 随机微分方程:\(T\to\infty\) 时前向过程是一个 SDE,反向过程是另一个 SDE,DDIM 是与它们同边缘分布的常微分方程(概率流 ODE)的离散化,这条路通向 19.7 节的 flow matching。 去噪:每一步就是一次 18.2 节的去噪自编码器,只是噪声水平由 \(t\) 告诉网络。
Hierarchical VAE: the forward process is a fixed, parameter-free "encoder" that encodes \(\mathbf x_0\) into \(T\) layers of latents \(\mathbf x_1,\dots,\mathbf x_T\), and the decoder is the reverse process; the training objective is an ELBO, which unrolls into exactly a weighted noise regression (Section 5; this is the "VAE with many layers and a fixed encoder" of Lesson 19.1). Score and energy: the network learns \(\nabla\log p_t\), the gradient of the negative energy \(-E\); the partition function vanishes when you take the gradient, so there is no need to fight it as in Lesson 19.5, and sampling is a sequence of annealed Langevin-like updates. Stochastic differential equations: as \(T\to\infty\) the forward process becomes an SDE and the reverse process another SDE, and DDIM is a discretization of the ordinary differential equation with the same marginals (the probability-flow ODE), the road to the flow matching of Lesson 19.7. Denoising: every step is one denoising autoencoder of Lesson 18.2, with the noise level told to the network through \(t\).
5再看数学The math
提示:把鼠标悬停在公式中带底色的符号上,图中对应的元素会被加粗:\(\bar\alpha_t\) 加粗 FIG 1 的信号曲线,score 加粗 FIG 2 的箭头,\(\hat{\mathbf x}_0\) 放大 FIG 2 的绿圈,后验放大 FIG 2 的紫点,损失下限加粗 FIG 3 的绿色虚线,条件 \(c\) 加粗 FIG 5 里被要求的那一类物体的圆,平均动作加粗 FIG 6 的琥珀虚线。Tip: hover the highlighted symbols in the formulas and the matching elements in the figures get bolder: \(\bar\alpha_t\) thickens the signal curve of FIG 1, the score the arrows of FIG 2, \(\hat{\mathbf x}_0\) enlarges the green ring of FIG 2, the posterior the purple dots of FIG 2, the loss floor thickens the green dashed line of FIG 3, the condition \(c\) the circles of the requested class in FIG 5, and the average action the amber dashed path of FIG 6.
前向过程与它的闭式The forward process and its closed form
给定方差日程 \(\beta_1,\dots,\beta_T\in(0,1)\),令 \(\alpha_t=1-\beta_t\)、\(\bar\alpha_t=\prod_{s=1}^t\alpha_s\)。前向过程是一条马尔可夫链:
Given a variance schedule \(\beta_1,\dots,\beta_T\in(0,1)\), let \(\alpha_t=1-\beta_t\) and \(\bar\alpha_t=\prod_{s=1}^t\alpha_s\). The forward process is a Markov chain:
用归纳法:若 \(\mathbf x_{t-1}=\sqrt{\bar\alpha_{t-1}}\mathbf x_0+\sqrt{1-\bar\alpha_{t-1}}\,\boldsymbol\varepsilon'\),则 \(\mathbf x_t=\sqrt{\alpha_t\bar\alpha_{t-1}}\,\mathbf x_0+\sqrt{\alpha_t(1-\bar\alpha_{t-1})}\,\boldsymbol\varepsilon'+\sqrt{\beta_t}\,\boldsymbol\varepsilon''\)。两份独立高斯噪声之和仍是高斯(4.4 节),方差相加:\(\alpha_t-\alpha_t\bar\alpha_{t-1}+1-\alpha_t=1-\bar\alpha_t\)。所以 \(\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon\),\(\boldsymbol\varepsilon\sim\mathcal N(0,I)\)。如果 \(\mathbf x_0\) 的协方差是 \(I\),那么每个 \(\mathbf x_t\) 的协方差也是 \(I\),这就是“方差保持”(variance preserving)的含义。\(\bar\alpha_T\approx0\) 时 \(q(\mathbf x_T\mid\mathbf x_0)\approx\mathcal N(0,I)\),与 \(\mathbf x_0\) 无关,所以生成可以从标准高斯开始。本页的余弦日程有 \(\bar\alpha_T\approx2.4\times10^{-7}\),线性日程 \(\approx2.0\times10^{-5}\)。
By induction: if \(\mathbf x_{t-1}=\sqrt{\bar\alpha_{t-1}}\mathbf x_0+\sqrt{1-\bar\alpha_{t-1}}\,\boldsymbol\varepsilon'\), then \(\mathbf x_t=\sqrt{\alpha_t\bar\alpha_{t-1}}\,\mathbf x_0+\sqrt{\alpha_t(1-\bar\alpha_{t-1})}\,\boldsymbol\varepsilon'+\sqrt{\beta_t}\,\boldsymbol\varepsilon''\). A sum of two independent Gaussian noises is Gaussian (Lesson 4.4) and the variances add: \(\alpha_t-\alpha_t\bar\alpha_{t-1}+1-\alpha_t=1-\bar\alpha_t\). Hence \(\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon\) with \(\boldsymbol\varepsilon\sim\mathcal N(0,I)\). If \(\mathbf x_0\) has covariance \(I\), so does every \(\mathbf x_t\); that is what "variance preserving" means. When \(\bar\alpha_T\approx0\), \(q(\mathbf x_T\mid\mathbf x_0)\approx\mathcal N(0,I)\) regardless of \(\mathbf x_0\), so generation can start from a standard Gaussian. On this page the cosine schedule has \(\bar\alpha_T\approx2.4\times10^{-7}\) and the linear one \(\approx2.0\times10^{-5}\).
反向过程与 ELBO:为什么是预测噪声The reverse process and the ELBO: why predict the noise
真正的反向条件 \(q(\mathbf x_{t-1}\mid\mathbf x_t)\) 依赖整个数据分布,算不出来;但多给一个 \(\mathbf x_0\),用贝叶斯公式和高斯的乘积(4.4 节)就得到闭式:
The true reverse conditional \(q(\mathbf x_{t-1}\mid\mathbf x_t)\) depends on the whole data distribution and cannot be computed; but given \(\mathbf x_0\) as well, Bayes' rule and the product of Gaussians (Lesson 4.4) give a closed form:
模型取同样的形式 \(p_\theta(\mathbf x_{t-1}\mid\mathbf x_t)=\mathcal N(\boldsymbol\mu_\theta(\mathbf x_t,t),\sigma_t^2I)\)。把 \(\mathbf x_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon)/\sqrt{\bar\alpha_t}\) 代进去,\(\tilde{\boldsymbol\mu}_t=\frac{1}{\sqrt{\alpha_t}}\big(\mathbf x_t-\frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\boldsymbol\varepsilon\big)\);于是让网络预测 \(\boldsymbol\varepsilon\),再用同一个式子算 \(\boldsymbol\mu_\theta\)。和 19.1 节一样写出负对数似然的变分上界(ELBO 取负),它分解成每一步的 KL 散度(第一行)。两个高斯之间的 KL 只与均值差的平方有关(方差固定时),代入上面的参数化就是第二行(\(C_t\) 与 \(\theta\) 无关):
The model takes the same form, \(p_\theta(\mathbf x_{t-1}\mid\mathbf x_t)=\mathcal N(\boldsymbol\mu_\theta(\mathbf x_t,t),\sigma_t^2I)\). Substituting \(\mathbf x_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon)/\sqrt{\bar\alpha_t}\) gives \(\tilde{\boldsymbol\mu}_t=\frac{1}{\sqrt{\alpha_t}}\big(\mathbf x_t-\frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\boldsymbol\varepsilon\big)\); so we let the network predict \(\boldsymbol\varepsilon\) and compute \(\boldsymbol\mu_\theta\) from the same expression. Writing the variational upper bound on the negative log-likelihood as in Lesson 19.1 (the negative ELBO), it splits into one KL divergence per step (first line). With the variances fixed, the KL between two Gaussians depends on \(\theta\) only through the squared difference of their means, and substituting the parametrization above gives the second line (\(C_t\) does not depend on \(\theta\)):
(\(C\) 里包括与 \(\theta\) 无关的 \(\mathbf x_T\) 项;\(t=1\) 的重建项另算。)Ho 等发现把每一步的权重全扔掉、只用第三行的 \(L_{\boldsymbol\varepsilon}\)(他们称为 “simple” 损失),样本反而更好:它相对地加重了噪声大的那些步。FIG 3 训练的就是 \(L_{\boldsymbol\varepsilon}\)(除以维数 2)。
(\(C\) includes the \(\mathbf x_T\) term, which does not depend on \(\theta\); the \(t=1\) reconstruction term is handled separately.) Ho et al. found that throwing all the per-step weights away and using the \(L_{\boldsymbol\varepsilon}\) of the third line (their "simple" loss) gives better samples: it puts relatively more weight on the noisier steps. FIG 3 trains exactly \(L_{\boldsymbol\varepsilon}\) (divided by the dimension, 2).
Score 视角:去噪 score matching 与损失下限The score view: denoising score matching and the loss floor
\(q(\mathbf x_t\mid\mathbf x_0)\) 是高斯,它的 score 是 \(\nabla_{\mathbf x_t}\log q(\mathbf x_t\mid\mathbf x_0)=-(\mathbf x_t-\sqrt{\bar\alpha_t}\mathbf x_0)/(1-\bar\alpha_t)=-\boldsymbol\varepsilon/\sqrt{1-\bar\alpha_t}\)。Vincent(2011)证明:对 \(\mathbf x_0\) 和噪声取期望,回归这个“条件 score”与回归边缘密度的 score \(\nabla\log p_t\) 只差一个与网络无关的常数(去噪 score matching;直接的 score matching 是 Hyvärinen 2005 的想法)。所以预测噪声就是预测 score,只差一个尺度:
\(q(\mathbf x_t\mid\mathbf x_0)\) is Gaussian, and its score is \(\nabla_{\mathbf x_t}\log q(\mathbf x_t\mid\mathbf x_0)=-(\mathbf x_t-\sqrt{\bar\alpha_t}\mathbf x_0)/(1-\bar\alpha_t)=-\boldsymbol\varepsilon/\sqrt{1-\bar\alpha_t}\). Vincent (2011) showed that, in expectation over \(\mathbf x_0\) and the noise, regressing onto this "conditional score" differs from regressing onto the score of the marginal density \(\nabla\log p_t\) only by a constant independent of the network (denoising score matching; plain score matching is Hyvärinen's 2005 idea). Predicting the noise is therefore predicting the score, up to a scale:
第一式:平方误差的最优解是条件均值;第二式是 Tweedie 公式,取 \(\mathbf x_t=\sqrt{\bar\alpha_t}\mathbf x_0+\sqrt{1-\bar\alpha_t}\boldsymbol\varepsilon\) 的条件期望即可从第一式得到。对高斯混合,后验 \(\hl{post}{p(\mathbf x_0\mid\mathbf x_t)}\) 是另一个高斯混合,每个分量的权重 \(r_k(\mathbf x_t)\) 就是 FIG 2 读数里的“后验 p(杯子 k | x_t)”,所以页面里的一切都是精确算出来的。
最优网络的损失是 \(\hl{floor}{L^\star=\mathbb E_t\,\mathbb E_{\mathbf x_t}\operatorname{tr}\operatorname{Cov}[\boldsymbol\varepsilon\mid\mathbf x_t]}\):只要从 \(\mathbf x_t\) 推不出唯一的 \(\boldsymbol\varepsilon\),它就大于 0。\(t\) 很小时,\(\mathbf x_t\approx\mathbf x_0\),噪声几乎完全不可辨识,那一步的损失接近 1(每个坐标);\(t\) 很大时,\(\mathbf x_t\approx\boldsymbol\varepsilon\),损失接近 0。FIG 3 的“4 个杯子”上平均起来约 0.295。所以:损失曲线能告诉你网络离最优还有多远,却不能直接和别的数据集、别的日程比较;它也不衡量样本质量。
The first identity holds because the minimizer of a squared error is the conditional mean; the second is Tweedie's formula, obtained from the first by taking the conditional expectation of \(\mathbf x_t=\sqrt{\bar\alpha_t}\mathbf x_0+\sqrt{1-\bar\alpha_t}\boldsymbol\varepsilon\). For a Gaussian mixture the posterior \(\hl{post}{p(\mathbf x_0\mid\mathbf x_t)}\) is another Gaussian mixture whose component weights \(r_k(\mathbf x_t)\) are the "posterior p(cup k | x_t)" of the FIG 2 readout, which is why everything on this page can be computed exactly.
The loss of the optimal network is \(\hl{floor}{L^\star=\mathbb E_t\,\mathbb E_{\mathbf x_t}\operatorname{tr}\operatorname{Cov}[\boldsymbol\varepsilon\mid\mathbf x_t]}\): positive as long as \(\mathbf x_t\) does not determine \(\boldsymbol\varepsilon\) uniquely. At small \(t\), \(\mathbf x_t\approx\mathbf x_0\) and the noise is almost unidentifiable, so the loss of that step is close to 1 (per coordinate); at large \(t\), \(\mathbf x_t\approx\boldsymbol\varepsilon\) and the loss is close to 0. Averaged over \(t\) it is about 0.295 for "4 cups" in FIG 3. So a loss curve tells you how far the network is from optimal, but it cannot be compared directly across datasets or schedules, and it does not measure sample quality.
采样:DDPM 与 DDIMSampling: DDPM and DDIM
采样器访问时刻 \(T=\tau_N>\dots>\tau_0=0\)。从 \(t=\tau_i\) 到 \(s=\tau_{i-1}\):先算 \(\hat{\mathbf x}_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon_\theta)/\sqrt{\bar\alpha_t}\),再
The sampler visits \(T=\tau_N>\dots>\tau_0=0\). From \(t=\tau_i\) to \(s=\tau_{i-1}\): first compute \(\hat{\mathbf x}_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon_\theta)/\sqrt{\bar\alpha_t}\), then
DDPM 一行就是上面的后验 \(q(\mathbf x_s\mid\mathbf x_t,\hat{\mathbf x}_0)\),相邻一步(\(s=t-1\))时它退化为 Ho 等的原始更新 \(\mathbf x_{t-1}=\frac{1}{\sqrt{\alpha_t}}\big(\mathbf x_t-\frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\boldsymbol\varepsilon_\theta\big)+\sigma_t\mathbf z\),\(\sigma_t^2=\tilde\beta_t\)。DDIM 那一行把“当前猜的数据”和“当前猜的噪声”按 \(s\) 时刻的比例重新组合,不加新噪声。Song 等证明,DDIM 和 DDPM 对应的是同一个训练目标(所以可以共用一个网络),区别只在反向过程里注入多少新噪声。注意 \(N=1\) 时两者都输出 \(\hat{\mathbf x}_0(\mathbf x_T)\approx\mathbb E[\mathbf x_0]\):这就是 FIG 4 里“所有样本落在数据均值上”。
The DDPM line is the posterior \(q(\mathbf x_s\mid\mathbf x_t,\hat{\mathbf x}_0)\) from above; for adjacent steps (\(s=t-1\)) it reduces to the original update of Ho et al., \(\mathbf x_{t-1}=\frac{1}{\sqrt{\alpha_t}}\big(\mathbf x_t-\frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\boldsymbol\varepsilon_\theta\big)+\sigma_t\mathbf z\) with \(\sigma_t^2=\tilde\beta_t\). The DDIM line recombines "the current guess of the data" and "the current guess of the noise" in the proportions of time \(s\), adding no fresh noise. Song et al. showed that DDIM and DDPM correspond to the same training objective (so they can share one network) and differ only in how much fresh noise the reverse process injects. Note that at \(N=1\) both output \(\hat{\mathbf x}_0(\mathbf x_T)\approx\mathbb E[\mathbf x_0]\): that is "every sample lands on the data mean" in FIG 4.
连续时间:SDE、概率流 ODE 与 LangevinContinuous time: SDEs, the probability-flow ODE and Langevin
让步数趋于无穷、每步的 \(\beta\) 趋于 0(\(\beta_t\approx\beta(t)\,\mathrm dt\)),前向过程变成一个随机微分方程(Song 等,ICLR 2021,“方差保持” SDE)。Anderson(1982)给出了它的时间反演,Song 等还指出存在一个与它每个时刻边缘分布都相同的确定性 ODE。下面三行依次是前向 SDE、反向 SDE 和这个“概率流 ODE”:
Let the number of steps go to infinity with each \(\beta\) going to 0 (\(\beta_t\approx\beta(t)\,\mathrm dt\)) and the forward process becomes a stochastic differential equation (Song et al., ICLR 2021, the "variance-preserving" SDE). Anderson (1982) gave its time reversal, and Song et al. pointed out a deterministic ODE with the same marginal at every time. Below, the three lines are the forward SDE, the reverse SDE and this "probability-flow ODE":
反向 SDE 里时间倒着走(从 \(t=T\) 到 0),\(\bar{\mathbf w}\) 是反向的布朗运动。DDPM 是反向 SDE 的一种离散化,DDIM 是概率流 ODE 的一种离散化(所以它是确定性的,也是可逆的:从数据可以积分回到对应的 \(\mathbf x_T\))。把 ODE 看成一个随时间变化的向量场(3.6 节),用更好的积分器(2.5 节)就能用更少的步数采样;直接去学这个向量场,就是 19.7 节的 flow matching(它把时间反过来,从噪声 \(t=0\) 积分到数据 \(t=1\),见第 2 节的约定)。另一条历史线索来自 Langevin 动力学:\(\mathbf x\leftarrow\mathbf x+\eta\nabla\log p(\mathbf x)+\sqrt{2\eta}\,\mathbf z\) 在步长足够小、步数足够多时从 \(p\) 采样(19.5 节)。但在数据稀疏的地方 score 学不准,Langevin 也走不动;Song 与 Ermon(2019)的 NCSN 用一串从大到小的噪声水平做“退火”Langevin:大噪声下的 score 在全空间都有定义,把样本先带到数据附近,小噪声再雕出细节。DDPM 和 NCSN 后来被统一为同一个 SDE 框架的两个特例。
In the reverse SDE time runs backwards (from \(t=T\) to 0) and \(\bar{\mathbf w}\) is a reverse-time Brownian motion. DDPM is one discretization of the reverse SDE and DDIM one discretization of the probability-flow ODE (which is why it is deterministic, and invertible: one can integrate from the data back to its \(\mathbf x_T\)). Viewing the ODE as a time-varying vector field (Lesson 3.6), better integrators (Lesson 2.5) sample in fewer steps; learning that vector field directly is the flow matching of Lesson 19.7 (which reverses time and integrates from the noise at \(t=0\) to the data at \(t=1\); see the convention note in Section 2). Another historical thread comes from Langevin dynamics: \(\mathbf x\leftarrow\mathbf x+\eta\nabla\log p(\mathbf x)+\sqrt{2\eta}\,\mathbf z\) samples from \(p\) when the step is small enough and the steps many enough (Lesson 19.5). Where data are sparse, however, the score is learned poorly and Langevin gets stuck; the NCSN of Song and Ermon (2019) runs "annealed" Langevin through a sequence of noise levels from large to small: the score at large noise is defined over the whole space and carries the samples near the data, and the small noise levels then carve the details. DDPM and NCSN were later unified as two special cases of the same SDE framework.
噪声日程与信噪比Noise schedules and the signal-to-noise ratio
日程最好用信噪比 \(\mathrm{SNR}(t)=\bar\alpha_t/(1-\bar\alpha_t)\) 来理解:它从无穷大单调降到几乎 0,日程决定的是在每个 SNR 区间里花多少步。Ho 等的线性日程是 \(T=1000\) 时 \(\beta\) 从 \(10^{-4}\) 线性增到 0.02;本页 \(T=100\),按 Nichol 与 Dhariwal 的做法把端点乘以 \(1000/T\),即从 0.001 到 0.2。他们提出的余弦日程是 \(\bar\alpha_t=f(t)/f(0)\),\(f(t)=\cos^2\!\big(\frac{t/T+s}{1+s}\cdot\frac\pi2\big)\),\(s=0.008\),\(\beta_t\) 截断在 0.999。FIG 1 显示了区别:线性日程在 \(t=27\) 时 \(\bar\alpha_t\) 已低于 0.5,余弦日程要到 \(t=50\)。数据的尺度也是日程的一部分:数据的标准差如果是 10 而不是 1,同一个 \(\bar\alpha_t\) 下的有效信噪比就大了 100 倍,整条日程都偏了。
A schedule is best understood through the signal-to-noise ratio \(\mathrm{SNR}(t)=\bar\alpha_t/(1-\bar\alpha_t)\), which falls monotonically from infinity to almost 0; the schedule decides how many steps are spent in each SNR range. The linear schedule of Ho et al. raises \(\beta\) linearly from \(10^{-4}\) to 0.02 over \(T=1000\) steps; with \(T=100\) on this page the endpoints are multiplied by \(1000/T\), as Nichol and Dhariwal do, giving 0.001 to 0.2. Their cosine schedule is \(\bar\alpha_t=f(t)/f(0)\) with \(f(t)=\cos^2\!\big(\frac{t/T+s}{1+s}\cdot\frac\pi2\big)\), \(s=0.008\), and \(\beta_t\) clipped at 0.999. FIG 1 shows the difference: under the linear schedule \(\bar\alpha_t\) is already below 0.5 at \(t=27\), under the cosine one only at \(t=50\). The scale of the data is part of the schedule too: if the data had standard deviation 10 instead of 1, the effective SNR at the same \(\bar\alpha_t\) would be 100 times larger, and the whole schedule would be off.
引导:贝叶斯公式里的一项Guidance: one term of Bayes' rule
由贝叶斯公式 \(p_t(\mathbf x\mid c)\propto p_t(\mathbf x)\,p_t(c\mid\mathbf x)\),对 \(\mathbf x\) 求梯度,归一化常数消失:
From Bayes' rule \(p_t(\mathbf x\mid c)\propto p_t(\mathbf x)\,p_t(c\mid\mathbf x)\); taking the gradient in \(\mathbf x\), the normalizing constant disappears:
Classifier guidance(Dhariwal 与 Nichol,2021)单独训练一个在带噪输入上工作的分类器 \(p_\phi(c\mid\mathbf x_t)\),把它的梯度乘一个系数加进 score。Classifier-free guidance 不要分类器:按上式,两个 score 之差就是一个“隐式分类器”的梯度,\(w>1\) 等于给它额外的权重 \(w-1\),样本被推向这个分类器最有把握的地方。直观上它像是在 \(p(\mathbf x)\,p(c\mid\mathbf x)^w\) 里采样,但这只是启发式的说法:引导后的 \(\tilde{\mathbf s}\) 一般并不是任何一族加噪分布的精确 score。(Ho 与 Salimans 的原文把系数写成 \((1+w')\varepsilon_c-w'\varepsilon_\varnothing\),即 \(w'=w-1\);本页沿用多数代码库里“\(w=1\) 即不引导”的写法。)FIG 5 里较少的 4 号马克杯被“挤掉”,是因为它本身少见,旁边又是更常见的碗 2,带噪时它附近的 \(p(c\mid\mathbf x)\)(\(c\) 为“马克杯”)就小。
Classifier guidance (Dhariwal and Nichol, 2021) trains a separate classifier \(p_\phi(c\mid\mathbf x_t)\) on noisy inputs and adds its gradient, times a coefficient, to the score. Classifier-free guidance needs no classifier: by the equation above, the difference of the two scores is the gradient of an "implicit classifier", and \(w>1\) gives it extra weight \(w-1\), pushing samples to where that classifier is most certain. Intuitively this is like sampling from \(p(\mathbf x)\,p(c\mid\mathbf x)^w\), but only heuristically: the guided \(\tilde{\mathbf s}\) is in general not the exact score of any family of noised distributions. (Ho and Salimans write the combination as \((1+w')\varepsilon_c-w'\varepsilon_\varnothing\), i.e. \(w'=w-1\); this page follows the convention of most codebases, where \(w=1\) means no guidance.) The rarer mug no. 4 in FIG 5 gets squeezed out because it is rare itself and sits next to the more frequent bowl no. 2, so under noise \(p(c\mid\mathbf x)\) with \(c\) = mug is small around it.
6机器人中的它:对整段动作去噪In robotics: denoising a whole action chunk
前面的“数据”是二维的点。对机器人,被生成的对象换成一整段动作:比如未来 10 个路点的位置,这是一个 20 维的向量。扩散模型并不在乎维数,前向过程、训练目标和采样器一个字都不用改;它对整段动作一起去噪,所以生成的每一段都是一个连贯的选择,而不是每个路点各自取平均。这正是 Diffusion Policy 生成动作块(action chunk)的方式,只是它的去噪网络还要以相机图像和本体感觉为条件(22.2 节)。
下图的示教从左边的起点出发,绕过中间的障碍物到右边:一部分从上方绕,其余从下方绕,每条路线都带一点平滑的随机扰动(\(\sigma=0.1\) 的平滑高斯过程,所以这个 20 维的分布仍然是两个高斯的混合,score 可以精确算出)。把均方误差回归会给出的条件均值 \(\hl{mean}{\mathbb E[\mathbf a]}\) 也画出来作比较。这张图只有一个固定的场景,用的是精确的 score,要看的是:对整段动作一起去噪时,每一段都完整地选定一边,而且两边的频率与示教一致。19.7 节的 FIG 5 会在一个类似的避障场景里补上这里没有的三样东西:一个现场训练的网络、以障碍物位置为条件的动作头,以及积分步数减到 1 时会发生什么。
So far the "data" were 2D points. For a robot, the object being generated becomes a whole chunk of actions: the positions of the next 10 waypoints, say, a 20-dimensional vector. A diffusion model does not care about the dimension; the forward process, the training objective and the samplers do not change by a single symbol. It denoises the whole chunk at once, so each chunk it produces is one coherent choice rather than an average taken waypoint by waypoint. This is exactly how Diffusion Policy generates action chunks, except that its denoising network is also conditioned on camera images and proprioception (Lesson 22.2).
In the figure below the demonstrations start on the left, get around the obstacle in the middle and end on the right: some pass above it and the rest below, each route with a little smooth random perturbation (a smooth Gaussian process with \(\sigma=0.1\), so the 20-dimensional distribution is still a mixture of two Gaussians and its score can be computed exactly). For comparison, the conditional mean \(\hl{mean}{\mathbb E[\mathbf a]}\) that squared-error regression would give is drawn as well. This figure has one fixed scene and uses the exact score; what it shows is that denoising the whole chunk at once makes every chunk commit fully to one side, with the two sides at the frequencies of the demonstrations. FIG 5 of Lesson 19.7 returns to a similar obstacle scene and adds the three things missing here: a network trained live, an action head conditioned on the obstacle's position, and what happens when the number of integration steps drops to 1.
- 保持“从上方绕的比例”在 0.4 到 0.6 之间,按“去噪生成”:200 条动作块里撞上障碍物的不超过 1 条(通常是 0,和示教本身一样罕见),上下大致各一半;而平均路线正好从障碍物中间穿过。Keep the "share of demos passing above" between 0.4 and 0.6 and press "Denoise": at most 1 of the 200 chunks hits the obstacle (usually 0, as rare as in the demonstrations themselves), about half go each way, while the average route goes straight through the middle of the obstacle.
- 把比例调到 0.75 以上(或 0.25 以下)再生成:样本里从上方绕的比例与示教的比例相差不到 0.1,撞上的不超过 1 条。扩散模型连各模式的频率也一起学了。(这时平均路线也恰好擦过障碍物;比例在 0.7 时它还会撞上。)Set the share above 0.75 (or below 0.25) and generate again: the share of samples passing above is within 0.1 of the demonstrations' share, with at most 1 collision. The diffusion model learns the frequencies of the modes too. (At this share the average route also just clears the obstacle; at 0.7 it still hits.)
Diffusion Policy(Chi 等,RSS 2023)以最近几帧观测(图像和本体感觉)为条件,对未来一段动作序列去噪,执行其中前一部分后重新规划(滚动时域);去噪网络有一维时间卷积 U-Net 和 Transformer 两种。论文报告:训练用 100 个噪声水平、推理用 10 步 DDIM,在一块 RTX 3080 上的推理延迟约 0.1 s,这是它能在真机上闭环运行的关键。 Diffuser(Janner 等,ICML 2022)对整条状态-动作轨迹去噪,用回报的梯度做引导,用“补全”(固定起点、终点再去噪其余部分)做目标条件规划。 抓取与位姿生成:SE(3)-DiffusionFields(Urain 等,ICRA 2023)在 \(\SE\) 上用扩散学抓取位姿的分布,并把学到的能量当作运动优化的代价。 通用策略:Octo(Octo Model Team,RSS 2024)在 Transformer 主干上接了一个扩散动作头;3D Diffusion Policy(Ze 等,RSS 2024)把条件换成点云。 速度:机器人的控制回路常常要 10 到 50 Hz,所以推理步数很关键:DDIM 这类少步采样器、蒸馏,以及 19.7 节的 flow matching(π0 的动作头就是它)都在压缩这一步。 图像与视频生成:Stable Diffusion 一类的潜在扩散模型(Rombach 等,CVPR 2022)先用自编码器把图像压到隐空间再扩散,也被用来给机器人数据做增广、合成目标图像或充当视频世界模型。
Diffusion Policy (Chi et al., RSS 2023) conditions on the last few observations (images and proprioception), denoises a sequence of future actions, executes the first part of it and then replans (receding horizon); its denoising network comes in a 1D temporal-convolution U-Net and a transformer version. The paper reports that training with 100 noise levels and running 10 DDIM steps at inference gives about 0.1 s latency on an RTX 3080, which is key to running it closed-loop on a real robot. Diffuser (Janner et al., ICML 2022) denoises whole state-action trajectories, guides them with the gradient of a return, and does goal-conditioned planning by "inpainting" (fix the start and the goal, denoise the rest). Grasp and pose generation: SE(3)-DiffusionFields (Urain et al., ICRA 2023) learns a distribution over grasp poses on \(\SE\) by diffusion and reuses the learned energy as a cost in motion optimization. Generalist policies: Octo (Octo Model Team, RSS 2024) puts a diffusion action head on a transformer backbone; 3D Diffusion Policy (Ze et al., RSS 2024) conditions on point clouds instead. Speed: robot control loops often need 10 to 50 Hz, so the number of inference steps matters: few-step samplers like DDIM, distillation, and the flow matching of Lesson 19.7 (the action head of π0 uses it) all try to shrink it. Image and video generation: latent diffusion models such as Stable Diffusion (Rombach et al., CVPR 2022) first compress images into a latent space with an autoencoder and diffuse there; such models are also used to augment robot data, synthesize goal images, or serve as video world models.
7常见误区Common pitfalls
整套日程默认数据的尺度在 1 附近:\(\mathbf x_T\) 要像 \(\mathcal N(0,I)\),中间各步的信噪比要落在网络学得动的区间。如果关节角用度、位置用毫米,数据标准差是几十上百,\(\bar\alpha_t\) 已经很小时信号仍然盖过噪声,大部分训练步都浪费在“几乎没加噪”的区间,采样的起点也和训练时见过的 \(\mathbf x_T\) 对不上。机器人代码里的标准做法是把每个动作维度线性缩放到 \([-1,1]\),或按训练集减均值除标准差,推理后再变换回去。
The whole schedule assumes data of scale about 1: \(\mathbf x_T\) must look like \(\mathcal N(0,I)\), and the SNR of the intermediate steps must fall in a range the network can learn. With joint angles in degrees or positions in millimetres, the data standard deviation is tens or hundreds, the signal still dominates the noise when \(\bar\alpha_t\) is already small, most training steps are wasted in an "almost noiseless" range, and the sampler's starting point does not match the \(\mathbf x_T\) seen in training. The standard practice in robot code is to rescale every action dimension linearly to \([-1,1]\), or subtract the training mean and divide by the standard deviation, and to undo it after inference.
每少一步,每一步就要跨过更大的一段噪声,而每一步只会朝后验均值走。FIG 4:“4 个杯子”上 DDPM 从 10 步减到 2 步,落在数据上的比例从 98% 降到 34%;1 步时所有样本都是数据均值。对机器人这意味着“平均动作”又回来了,偏偏是它最该避免的东西。少步数要靠专门的方法(DDIM、更高阶的 ODE 积分器、蒸馏、consistency 类模型、flow matching),而且每次改了步数都要重新评估成功率,不能只看速度。
With every step removed, each remaining step must cross a bigger stretch of noise, and each step only moves toward the posterior mean. FIG 4: on "4 cups", going from 10 to 2 DDPM steps drops the share on the data from 98% to 34%; with 1 step every sample is the data mean. For a robot this brings the "average action" back, exactly what it most needs to avoid. Few steps require dedicated methods (DDIM, higher-order ODE integrators, distillation, consistency-type models, flow matching), and every change of step count needs a fresh evaluation of the success rate, not just of the speed.
扩散损失有一个大于 0 的下限,而且不同噪声步的贡献差别很大:损失主要由中高噪声步决定,细节却由低噪声步决定。FIG 3 的“两条弧形把手”上,测试损失只比下限高约 6%,仍有约五分之一的样本不在把手上。要看样本:覆盖率、各模式的频率、离数据的距离(图像领域用 FID 等指标),对机器人最终要看任务成功率。
The diffusion loss has a floor above zero, and different noise steps contribute very differently: the loss is dominated by medium and high noise steps, while the details are decided at the low noise steps. On FIG 3's "two arc handles" the test loss is only about 6% above its floor while about one sample in five is still off the handles. Look at the samples instead: coverage, the frequency of each mode, distance to the data (metrics such as FID for images), and for robots ultimately the task success rate.
FIG 5:\(w\) 从 1 加到 2.5,符合指令的样本从约 80% 升到约 98%;再加到 8,较少的 4 号马克杯只剩约 6%(数据里是 20%)。在图像上,过大的 \(w\) 会让颜色过饱和、细节失真,样本也会被推离数据流形。对机器人,多样性本身就是价值:一个只会从一个方向抓杯子的策略,遇到那一侧被挡住就没办法了。\(w\) 应该按任务指标来调,而不是越大越好。
FIG 5: raising \(w\) from 1 to 2.5 lifts the share of samples obeying the command from about 80% to about 98%; going on to 8 leaves the rarer mug no. 4 with only about 6% (20% in the data). On images an excessive \(w\) gives over-saturated colours and distorted details, and pushes samples off the data manifold. For robots, diversity has value in itself: a policy that can only grasp a cup from one side is helpless when that side is blocked. Tune \(w\) on the task metric, not "the bigger the better".
FIG 3 已经演示:原始权重采出的各模式份额随训练步抖动,EMA 权重稳得多,几乎所有扩散代码库都默认用 EMA 采样(衰减率通常在 0.999 以上,本页的小例子用 0.99)。另一类常见 bug 是训练和采样“对不上”:网络预测的是 \(\boldsymbol\varepsilon\) 还是 \(\hat{\mathbf x}_0\) 还是速度 \(\mathbf v\);时间输入是整数步还是 \([0,1]\) 里的连续时间;采样器跳步时用的 \(\bar\alpha\) 是否与训练日程一致。这些错误不会报错,只会让样本悄悄变差,所以值得像本页这样,先用一个有精确 score 的玩具分布把整条管线验一遍。
FIG 3 showed it: the mode shares of samples from the raw weights jitter with training, while the EMA weights are much steadier, and nearly every diffusion codebase samples with EMA weights by default (decay rates are usually 0.999 or higher; this page's small example uses 0.99). Another common class of bugs is a mismatch between training and sampling: does the network predict \(\boldsymbol\varepsilon\), \(\hat{\mathbf x}_0\) or a velocity \(\mathbf v\); is the time input an integer step or a continuous time in \([0,1]\); does the skipping sampler use the same \(\bar\alpha\) as the training schedule? None of these raise an error, they just quietly degrade the samples, which is why it pays to validate the whole pipeline first on a toy distribution with an exact score, as this page does.
8小练习Check yourself
9延伸阅读Further reading
- J. Sohl-Dickstein, E. Weiss, N. Maheswaranathan & S. Ganguli, Deep Unsupervised Learning using Nonequilibrium Thermodynamics, ICML 2015 — 扩散模型的起点:用一条逐步加噪的马尔可夫链定义生成模型where diffusion models started: a generative model defined by a Markov chain that adds noise step by step
- J. Ho, A. Jain & P. Abbeel, Denoising Diffusion Probabilistic Models, NeurIPS 2020 — DDPM:预测噪声、简化损失,样本质量第一次比肩 GANDDPM: noise prediction and the simplified loss; sample quality on a par with GANs for the first time
- Y. Song & S. Ermon, Generative Modeling by Estimating Gradients of the Data Distribution, NeurIPS 2019 — NCSN:多噪声水平的 score matching 与退火 LangevinNCSN: score matching at many noise levels and annealed Langevin
- Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon & B. Poole, Score-Based Generative Modeling through Stochastic Differential Equations, ICLR 2021 — SDE 统一框架、反向 SDE 与概率流 ODEthe unifying SDE framework, the reverse SDE and the probability-flow ODE
- J. Song, C. Meng & S. Ermon, Denoising Diffusion Implicit Models, ICLR 2021 — DDIM:同一个网络、确定性的少步采样DDIM: the same network, deterministic few-step sampling
- A. Nichol & P. Dhariwal, Improved Denoising Diffusion Probabilistic Models, ICML 2021 — 余弦日程、学习方差the cosine schedule and learned variances
- P. Dhariwal & A. Nichol, Diffusion Models Beat GANs on Image Synthesis, NeurIPS 2021;J. Ho & T. Salimans, Classifier-Free Diffusion Guidance, NeurIPS 2021 Workshop on Deep Generative Models and Downstream Applications — classifier guidance 与 classifier-free guidanceclassifier guidance and classifier-free guidance
- P. Vincent, A Connection Between Score Matching and Denoising Autoencoders, Neural Computation, 2011;A. Hyvärinen, Estimation of Non-Normalized Statistical Models by Score Matching, JMLR, 2005 — score matching 与去噪的联系score matching and its link to denoising
- T. Karras, M. Aittala, T. Aila & S. Laine, Elucidating the Design Space of Diffusion-Based Generative Models, NeurIPS 2022 — 把日程、参数化、采样器拆开逐一分析的工程指南an engineering guide that takes schedules, parametrizations and samplers apart one by one
- C. Chi, S. Feng, Y. Du, Z. Xu, E. Cousineau, B. Burchfiel & S. Song, Diffusion Policy: Visuomotor Policy Learning via Action Diffusion, RSS 2023;M. Janner, Y. Du, J. B. Tenenbaum & S. Levine, Planning with Diffusion for Flexible Behavior Synthesis, ICML 2022 — 扩散用于机器人动作与轨迹diffusion for robot actions and trajectories