Dive into Deep Learning · §38.6
去噪分数匹配与基于分数的扩散模型
Hyvärinen 与 Vincent 定理 · Tweedie 公式 · 朗之万混合 · DDPM 与 DDIM · 无分类器引导
研究动机
两种生成反向过程——Anderson 反向时间 SDE 与概率流 ODE——都只需要同一个未知量:加噪数据分布的分数 \nabla_{\mathbf{x}}\log p_t(\mathbf{x})。
Hyvärinen 恒等式(2005)
展开 J_{\text{ESM}}(\boldsymbol{\theta}) 中的平方并对交叉项 -\int \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}) \cdot (\nabla\log p(\mathbf{x}))\,p(\mathbf{x})\,d\mathbf{x} = -\int \mathbf{s}_{\boldsymbol{\theta}} \cdot \nabla p\,d\mathbf{x} 做分部积分,可将导数从未知数据密度 p 挪到网络 \mathbf{s}_{\boldsymbol{\theta}} 上:
J_{\text{ESM}}(\boldsymbol{\theta}) = \mathbb{E}_{\mathbf{x} \sim p}\!\left[\frac{1}{2}\|\mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x})\|^2 + \nabla \cdot \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x})\right] + C, \qquad C = \frac{1}{2}\mathbb{E}_p\|\nabla\log p(\mathbf{x})\|^2
合理性检验
对数据 x \sim \mathcal{N}(0, v) 与线性分数模型 s_w(x) = -x/w,隐式目标为 J(w) = \mathbb{E}[\frac{1}{2}s_w^2 + s_w'] = \frac{v}{2w^2} - \frac{1}{w},其导数 -\frac{v}{w^3} + \frac{1}{w^2} = 0 仅凭样本即可还原 w = v:
implicit score matching picks w = 1.98 (v = 2.0)
基础恒等式
对任意条件期望为 \mathbf{m}(X) = \mathbb{E}[Y \mid X] 的随机对 (X, Y),重期望公式消去交叉项:
\mathbb{E}\!\left[\|\mathbf{v}(X) - Y\|^2\right] = \mathbb{E}\!\left[\|\mathbf{v}(X) - \mathbf{m}(X)\|^2\right] + \mathbb{E}\!\left[\|Y - \mathbf{m}(X)\|^2\right]
无迹分数学习
用尺度 \sigma 的高斯噪声扰动每个干净样本 \mathbf{x} \sim p:\tilde{\mathbf{x}} = \mathbf{x} + \sigma\boldsymbol{\epsilon}(\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})),从而 p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}) = \mathcal{N}(\tilde{\mathbf{x}}; \mathbf{x}, \sigma^2\mathbf{I})。虽然边缘分数 \nabla\log p_\sigma(\tilde{\mathbf{x}}) 未知,但对每个训练对,条件分数均有闭式表达:
\nabla_{\tilde{\mathbf{x}}}\log p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}) = \frac{\mathbf{x} - \tilde{\mathbf{x}}}{\sigma^2} = -\frac{\boldsymbol{\epsilon}}{\sigma}
最优去噪
重排 Vincent 恒等式 \nabla\log p_\sigma(\tilde{\mathbf{x}}) = \mathbb{E}\!\left[\frac{\mathbf{x} - \tilde{\mathbf{x}}}{\sigma^2} \,\middle|\, \tilde{\mathbf{x}}\right],即得最小均方误差干净点估计量 \hat{\mathbf{x}}_0(\tilde{\mathbf{x}}) = \mathbb{E}[\mathbf{x} \mid \tilde{\mathbf{x}}] 的 Tweedie 公式:
高斯检验与曲率
对高斯数据 x \sim \mathcal{N}(0, v)(v = 1,噪声 \sigma = 0.5),加噪边缘分布为 p_\sigma = \mathcal{N}(0, 1.25),Tweedie 最优去噪器按 \frac{v}{v+\sigma^2}\tilde{x} = 0.8\,\tilde{x} 收缩,不可约均方误差下界为 \frac{1}{\sigma^2} - \frac{1}{v+\sigma^2} = 4 - 0.8 = 3.2 > 0:
mean squared error of the conditional mean -xt/1.25: 3.186 (floor 3.2)
least-squares slope of x on xt: 0.800 (0.8)
任意加噪密度的曲率上界:将 Tweedie 公式再对 \tilde{\mathbf{x}} 求一次导,即证: \nabla^2\log p_\sigma(\tilde{\mathbf{x}}) = -\frac{\mathbf{I}}{\sigma^2} + \frac{1}{\sigma^4}\operatorname{Cov}[\mathbf{x} \mid \tilde{\mathbf{x}}] \quad \Longrightarrow \quad -\nabla^2\log p_\sigma(\tilde{\mathbf{x}}) \preceq \frac{1}{\sigma^2}\mathbf{I} 经 \sigma 平滑后的任何密度峰,其弯曲程度都绝不会超过平滑它的高斯核 \mathcal{N}(\mathbf{0}, \sigma^2\mathbf{I})。
一维混合实验
从 p = \frac{1}{2}\mathcal{N}(-2, 0.5^2) + \frac{1}{2}\mathcal{N}(2, 0.5^2) 抽取 4{,}096 个样本,每个在 \sigma = 0.5 下加噪四次(共 16{,}384 对),用 np.linalg.lstsq 对 17 个固定基函数拟合 y = -\epsilon/\sigma,即可准确还原精确分数 4\tanh(4\tilde{x}) - 2\tilde{x}:
mean squared error: fit 2.10, exact score 2.11, population floor 2.11
excess risk 0.012; fit at x = 8: +1.6, exact score -12 (no pairs there)
一维混合实验
尽管目标点云 -\epsilon/\sigma 以方差 \approx 2.11(总体下界)纵向散布,其条件均值正是精确分数曲线,最小二乘拟合的超额风险仅为 0.01。
支撑集外方差
在 20 组独立数据与噪声抽样上重复最小二乘拟合,暴露了单噪声水平分数匹配的根本局限:
exact score over 20 draws: mean squared error 2.12, sd 0.047
fit at x = 1 over 20 draws: from +1.9 to +2.1 (exact score +2.0)
fit at x = 8 over 20 draws: from -12.6 to +27.0 (exact score -12.0)
基于分数的 MCMC
给定单一噪声水平下的分数场 \nabla\log p(\mathbf{x}),朗之万动力学沿对数密度攀升,同时注入单位布朗噪声以防止坍缩到峰顶:
d\mathbf{X}_t = \frac{1}{2}\nabla\log p(\mathbf{X}_t)\,dt + d\mathbf{W}_t \;\xrightarrow{\text{Euler–Maruyama, step } h}\; \mathbf{x}_{k+1} = \mathbf{x}_k + \frac{h}{2}\mathbf{s}(\mathbf{x}_k) + \sqrt{h}\,\boldsymbol{\xi}_k
峰间混合瓶颈
使用不对称混合分布 p = 0.8\,\mathcal{N}(-2, 0.5^2) + 0.2\,\mathcal{N}(2, 0.5^2) 的精确分数运行 10{,}000 条朗之万链 2{,}000 步(h = 0.01),会发生什么?
broad start: P(X > 0) = 0.476, E[X^2] = 4.23 (truth 0.2, 4.25)
all chains at -2 (weight 0.8): 0.005 end on the other side
all chains at +2 (weight 0.2): 0.020 end on the other side
噪声条件分数匹配
在连续的高斯加噪谱 \mathbf{x}_t = \alpha_t\mathbf{x}_0 + \sigma_t\boldsymbol{\epsilon} 上训练单个时间条件分数网络 \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}_t, t),同时治好了单噪声水平分数匹配的三大缺陷:
跨全噪声水平训练
以权重调度 w(t) > 0 对噪声水平 t \sim \mathcal{U}[\varepsilon, T] 取平均,得噪声条件 DSM 损失:
\mathcal{L}(\boldsymbol{\theta}) = \frac{1}{2}\mathbb{E}_{t}\!\left[w(t)\,\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\!\left[\left\|\mathbf{s}_{\boldsymbol{\theta}}(\alpha_t\mathbf{x}_0 + \sigma_t\boldsymbol{\epsilon}, t) + \frac{\boldsymbol{\epsilon}}{\sigma_t}\right\|^2\right]\right]
打通离散与连续
去噪扩散概率模型(DDPM)运行一条 N = 1000 步的马尔可夫链,调度为 \beta_1, \ldots, \beta_N \in [10^{-4}, 0.02]:
\mathbf{x}_t = \sqrt{1 - \beta_t}\,\mathbf{x}_{t-1} + \sqrt{\beta_t}\,\boldsymbol{\epsilon}_t \quad \Longrightarrow \quad \mathbf{x}_t = \sqrt{\bar{\alpha}_t}\,\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t}\,\boldsymbol{\epsilon}, \qquad \bar{\alpha}_t = \prod_{s=1}^t (1 - \beta_s)
alpha_bar at t = 10, 100, 1000: 0.998, 0.897, 4e-05; sigma_bar from 0.01 to 157
噪声预测与采样
通过噪声预测网络 \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) = -\boldsymbol{\epsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) / \sigma_t(\sigma_t = \sqrt{1 - \bar{\alpha}_t})参数化分数模型,将 DDPM 的训练与采样同基于分数的 SDE 完全统一起来:
借概率流 ODE 跨步跳跃
DDIM 先预测 \hat{\mathbf{x}}_0 = (\mathbf{x}_t - \sqrt{1-\bar{\alpha}_t}\boldsymbol{\epsilon}_{\boldsymbol{\theta}})/\sqrt{\bar{\alpha}_t},再直接跳到更早的层级 t' < t。在重标度坐标 \bar{\mathbf{x}}_t = \mathbf{x}_t/\sqrt{\bar{\alpha}_t} 与 \bar{\sigma}_t = \sqrt{(1-\bar{\alpha}_t)/\bar{\alpha}_t} 中:
\bar{\mathbf{x}}_{t'} = \bar{\mathbf{x}}_t + (\bar{\sigma}_{t'} - \bar{\sigma}_t)\,\boldsymbol{\epsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t)
分析 DDIM 截断误差
由于 DDIM 是 \bar{\sigma} 时钟下的一阶欧拉求解器,其跨步误差取决于 \hat{\boldsymbol{\epsilon}}(\mathbf{x}_t, t) = \mathbb{E}[\boldsymbol{\epsilon} \mid \mathbf{x}_t] 沿概率流轨迹弯曲的程度:
10 步 vs. 50 步 vs. 999 步
在 \mathcal{N}(0, 1) 数据与标准化双峰混合分布上,使用精确噪声预测 \hat{\epsilon} = -\sqrt{1-\bar{\alpha}_t}\nabla\log p_t,比较 10、50 和 999(每级一步)次等距 DDIM 跨步:
N(0,1) data, 10 strides: every sample times 0.832
N(0,1) data, 50 strides: every sample times 0.964
N(0,1) data, 999 strides: every sample times 0.998
mixture, 10 strides vs 999: mean |gap| 0.084, KS 0.078
mixture, 50 strides vs 999: mean |gap| 0.015, KS 0.018
端到端多层级学习
在不对称 0.8 / 0.2 混合分布上,对 50 步 DDIM 采样器访问的 50 个噪声级分别用岭回归拟合 17 特征线性模型:
exact scores: light mode 0.190, means -0.96 +0.94, widths 0.229 0.234, KS to p0 0.03
fitted scores: light mode 0.199, means -0.95 +0.91, widths 0.232 0.263, KS to p0 0.05
truth: light mode 0.2, means -0.97 +0.97, widths 0.243; KS between the clouds 0.03
多层级回归解决峰权重分配:在单噪声级使用精确分数的朗之万把 48\% 样本留在轻峰之处,基于最小二乘拟合分数的 50 步 DDIM 将真实的 20\% 轻峰质量还原到百分之一二以内,且与精确分数 DDIM 轨迹的 KS 距离仅百分之几。
条件定向引导
由贝叶斯法则,\nabla\log p_t(y \mid \mathbf{x}) = \mathbf{s}_y - \mathbf{s}_\varnothing。无分类器引导(CFG)以引导强度 \gamma \ge 1 放大这一类间差分:
\tilde{\mathbf{s}}_{\boldsymbol{\theta}}(\mathbf{x}, t, y) = \mathbf{s}_\varnothing + \gamma\big(\mathbf{s}_y - \mathbf{s}_\varnothing\big) = (1 - \gamma)\mathbf{s}_\varnothing + \gamma\mathbf{s}_y
引导实验
在引导强度 \gamma \in \{1, 3, 10\} 下运行指向右峰(y = \mathcal{N}(+0.970, 0.243^2))的 1,000 步祖先反向链:
gamma = 1.0: mass right of 0 1.000, mean 0.973, std 0.243
gamma = 3.0: mass right of 0 1.000, mean 1.027, std 0.234
gamma = 10.0: mass right of 0 1.000, mean 1.070, std 0.236
exact conditional: mean 0.970, std 0.243
在 \gamma = 1(精确贝叶斯法则)时,链还原出精确类均值 0.970 与标准差 0.243。在 \gamma = 3 与 \gamma = 10 时,线性外推将样本推离对侧左峰,把终态均值向外推至 1.03 与 1.08。
检验理解
检验你对分数匹配与扩散采样器的理解:
解答:
核心要点
| 方法 / 定理 | 数学公式 | 作用与实践特性 |
|---|---|---|
| Hyvärinen 恒等式(ISM) | \mathbb{E}_p\big[\frac{1}{2}\|\mathbf{s}_{\boldsymbol{\theta}}\|^2 + \nabla\cdot\mathbf{s}_{\boldsymbol{\theta}}\big] + C | 消去未知 \nabla\log p,但计算 \nabla\cdot\mathbf{s}_{\boldsymbol{\theta}} 需 O(d) 次反向传播 |
| Vincent 定理(DSM) | \frac{1}{2}\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\big\|\mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) + \boldsymbol{\epsilon}/\sigma_t\big\|^2 | 无需算迹地回归到 -\boldsymbol{\epsilon}/\sigma_t;最优下界为 \frac{d}{\sigma_t^2} - \mathbb{E}\|\nabla\log p_t\|^2 |
| Tweedie 公式 | \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}] = \tilde{\mathbf{x}} + \sigma^2\nabla\log p_\sigma(\tilde{\mathbf{x}}) | 将任意分数或噪声预测器转化为最小均方误差干净点去噪器 |
| DDPM 前向与损失 | \mathbf{x}_t = \sqrt{\bar{\alpha}_t}\mathbf{x}_0 + \sqrt{1-\bar{\alpha}_t}\boldsymbol{\epsilon},w(t) = \sigma_t^2 | 精确离散 VP-SDE 转移;\sigma_t^2 权重抵消 1/\sigma_t^2 下界增长 |
| DDIM 确定性跨步 | \bar{\mathbf{x}}_{t'} = \bar{\mathbf{x}}_t + (\bar{\sigma}_{t'} - \bar{\sigma}_t)\boldsymbol{\epsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) | (\bar{\sigma}, \bar{\mathbf{x}}) 坐标下 VP 概率流 ODE 的前向欧拉法 |
| 无分类器引导 | \tilde{\mathbf{s}} = (1 - \gamma)\mathbf{s}_\varnothing + \gamma\mathbf{s}_y | 经贝叶斯法则放大类别方向;\gamma > 1 打破 1/\sigma_t^2 曲率上界 |