第 1 部分:基础数学工具与物理背景
最近在尝试理解扩散模型所依赖的核心数学概念:高斯分布的性质、朗之万动力学(SDE)及其离散化。
1. 核心数学工具:高斯分布 (Gaussian Distribution)
扩散模型(尤其是 DDPM)的全部推导都建立在高斯分布的美妙性质之上。
1.1 定义
一个一维高斯分布(正态分布)由均值 \(\mu\) 和方差 \(\sigma^2\) 定义,其概率密度函数 (PDF) 为: \[ \mathcal{N}(x; \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right) \] 对于 \(D\) 维向量 \(\mathbf{x}\)(例如视频中 \(32 \times 32 = 1024\) 维的图片向量),多维高斯分布由均值向量 \(\boldsymbol{\mu}\) 和协方差矩阵 \(\boldsymbol{\Sigma}\) 定义: \[ \mathcal{N}(\mathbf{x}; \boldsymbol{\mu}, \boldsymbol{\Sigma}) = \frac{1}{\sqrt{(2\pi)^D \det(\boldsymbol{\Sigma})}} \exp\left(-\frac{1}{2}(\mathbf{x} - \boldsymbol{\mu})^T \boldsymbol{\Sigma}^{-1}(\mathbf{x} - \boldsymbol{\mu})\right) \] > 关键简化: 在 DDPM 中,我们通常假设协方差矩阵是对角矩阵 \(\boldsymbol{\Sigma} = \sigma^2 \mathbf{I}\),其中 \(\mathbf{I}\) 是单位矩阵。这意味着向量的每个维度(每个像素)是独立同分布的(IID)。
1.2 关键性质 1:重参数化技巧 (Reparameterization Trick)
问题: 我们如何从 \(\mathcal{N}(\mu, \sigma^2)\) 中采样?直接对这个分布采样是困难的,且在神经网络中无法传递梯度。
技巧: 我们可以从一个标准高斯分布 \(\epsilon \sim \mathcal{N}(0, 1)\) 中采样,然后通过线性变换得到 \(x\): \[ x = \mu + \sigma \cdot \epsilon \] 推导: * 1.: 设 \(\epsilon \sim \mathcal{N}(0, 1)\),即 \(E[\epsilon] = 0, \text{Var}(\epsilon) = 1\)。 * 2.: 我们构造 \(x = \mu + \sigma \epsilon\)。 * 计算 \(x\) 的均值:\(E[x] = E[\mu + \sigma \epsilon] = E[\mu] + \sigma E[\epsilon] = \mu + \sigma \cdot 0 = \mu\)。 * 3.: 计算 \(x\) 的方差:\(\text{Var}(x) = \text{Var}(\mu + \sigma \epsilon) = \text{Var}(\sigma \epsilon) = \sigma^2 \text{Var}(\epsilon) = \sigma^2 \cdot 1 = \sigma^2\)。 * 4.: 由于高斯分布的线性变换仍然是高斯分布,因此 \(x \sim \mathcal{N}(\mu, \sigma^2)\)。
意义: 这使得采样过程可微。\(\mu\) 和 \(\sigma\) 可以是神经网络的输出,\(\epsilon\) 作为外部噪声输入,梯度可以反向传播回 \(\mu\) 和 \(\sigma\)。
1.3 关键性质 2:两个独立高斯分布之和 (Sum of Independent Gaussians)
问题: 两个独立高斯分布相加会怎样?这是前向加噪过程(Forward Process)的核心。
性质: 如果 \(X_1 \sim \mathcal{N}(\mu_1, \sigma_1^2)\) 且 \(X_2 \sim \mathcal{N}(\mu_2, \sigma_2^2)\) 相互独立,那么它们的和 \(Y = X_1 + X_2\) 仍然是高斯分布: \[ Y \sim \mathcal{N}(\mu_1 + \mu_2, \sigma_1^2 + \sigma_2^2) \] 推导: * 均值: \(E[Y] = E[X_1 + X_2] = E[X_1] + E[X_2] = \mu_1 + \mu_2\)。 * 方差: 由于 \(X_1\) 和 \(X_2\) 相互独立,\(\text{Cov}(X_1, X_2) = 0\)。 \(\text{Var}(Y) = \text{Var}(X_1 + X_2) = \text{Var}(X_1) + \text{Var}(X_2) + 2 \text{Cov}(X_1, X_2) = \sigma_1^2 + \sigma_2^2\)。 * (两个独立高斯变量之和仍为高斯分布,这可以通过矩生成函数或特征函数严格证明,这里我们接受这个结论。)
意义: 这使得我们可以计算前向过程中任意 \(t\) 时刻 \(x_t\) 从 \(x_0\) 开始累积加噪的结果。
2. 物理与SDE:朗之万动力学 (Langevin Dynamics)
这是扩散模型的物理原型。
2.1 随机微分方程 (SDE)
朗之万动力学描述了一个粒子(在我们的例子中是图片向量 \(\mathbf{x}\))在势场 \(U(\mathbf{x})\) 中运动,同时受到随机力(如布朗运动)的影响。其 SDE 形式 为: \[ d\mathbf{x}_t = \mathbf{f}(\mathbf{x}_t, t)dt + g(t)d\mathbf{w}_t \] * \(\mathbf{x}_t\):\(t\) 时刻的粒子位置(图片向量)。 * \(\mathbf{f}(\mathbf{x}_t, t)\):漂移项 (Drift)。代表确定性的力,如视频中 提到的 “吸引回原点的线性运动”(例如 \(\mathbf{f}(\mathbf{x}, t) = -\beta \mathbf{x}\),使分布趋向原点)。它对应能量函数的负梯度 \(-\nabla U(\mathbf{x})\)。 * \(g(t)\):扩散项 (Diffusion)。控制随机噪声的强度。 * \(d\mathbf{w}_t\):维纳过程 (Wiener Process) 或布朗运动。它是一个随机项,其增量 \(d\mathbf{w}_t\) 在 \(dt\) 时间内服从高斯分布 \(d\mathbf{w}_t \sim \mathcal{N}(0, \mathbf{I} dt)\)。
2.2 SDE 的离散化:欧拉-丸山法 (Euler-Maruyama)
问题: 计算机无法处理连续时间 \(dt\)。我们如何模拟这个 SDE?
方法: 我们使用欧拉近似法(在 SDE 中称为 Euler-Maruyama)将其离散化为小的时间步 \(\Delta t\)。 \[ \mathbf{x}_{t+\Delta t} - \mathbf{x}_t \approx \mathbf{f}(\mathbf{x}_t, t)\Delta t + g(t) (\mathbf{w}_{t+\Delta t} - \mathbf{w}_t) \] 根据维纳过程的性质,在 \(\Delta t\) 时间内的增量 \((\mathbf{w}_{t+\Delta t} - \mathbf{w}_t)\) 服从 \(\mathcal{N}(0, \mathbf{I} \Delta t)\)。 根据性质 1.2(重参数化),\(\mathcal{N}(0, \mathbf{I} \Delta t)\) 可以写成 \(\sqrt{\Delta t} \cdot \mathbf{z}\),其中 \(\mathbf{z} \sim \mathcal{N}(0, \mathbf{I})\)。
离散迭代公式: \[ \mathbf{x}_{t+\Delta t} \approx \mathbf{x}_t + \mathbf{f}(\mathbf{x}_t, t)\Delta t + g(t) \sqrt{\Delta t} \mathbf{z}_t \] 其中 \(\mathbf{z}_t \sim \mathcal{N}(0, \mathbf{I})\) 是在 \(t\) 时刻采样的标准高斯噪声。
这就是 DDPM 前向加噪过程 (Forward Process) 的数学原型。
3. 核心数学工具:贝叶斯公式 (Bayes’ Theorem)
贝叶斯公式是连接前向过程(加噪)和反向过程(去噪)的桥梁。
对于连续变量(概率密度函数): \[ p(x|y) = \frac{p(y|x) p(x)}{p(y)} \] 其中 \(p(y) = \int p(y|x) p(x) dx\)。
在扩散模型中的应用: * 1.: 我们定义了一个简单的前向加噪过程 \(q(x_t | x_{t-1})\)(易于计算)。 * 2.: 我们想要的是反向去噪过程 \(p(x_{t-1} | x_t)\)(难以计算)。 * 3.: 贝叶斯公式告诉我们:\(p(x_{t-1} | x_t) \propto p(x_t | x_{t-1}) p(x_{t-1})\)。 * 4.: 在 DDPM 中,我们会看到一个更复杂的形式,它利用了 \(x_0\): \[ q(x_{t-1} | x_t, x_0) = \frac{q(x_t | x_{t-1}, x_0) q(x_{t-1} | x_0)}{q(x_t | x_0)} \]
小结
- 高斯分布的性质(重参数化、加法),能精确计算加噪后的分布。
- 朗之万动力学与欧拉近似,为 “逐步加噪” 提供了物理和数学模型。
- 贝叶斯公式,指明了如何从 “加噪” 倒推出 “去噪”。
2. DDPM 前向过程:从图像到噪声 (The Forward Process)
前向过程的目标是模拟大纲 中描述的“图片在向量空间中逐步噪声化的轨迹”。我们定义一个马尔可夫过程,在该过程中,我们从原始数据 \(\mathbf{x}_0 \sim q(\mathbf{x}_0)\)(即真实图片分布)开始,在 \(T\) 个离散的时间步中逐步向其添加高斯噪声。
2.1 单步加噪过程 \(q(\mathbf{x}_t | \mathbf{x}_{t-1})\)
在每个 \(t\) 步,我们向 \(\mathbf{x}_{t-1}\) 添加少量噪声以生成 \(\mathbf{x}_t\)。这个过程被定义为一个高斯转变(这源于我们第 1 部分中对朗之万动力学的离散化):
\[ q(\mathbf{x}_t | \mathbf{x}_{t-1}) = \mathcal{N}(\mathbf{x}_t; \sqrt{1 - \beta_t} \mathbf{x}_{t-1}, \beta_t \mathbf{I}) \]
- \(\{\beta_t\}_{t=1}^T\) 是一个预先设定的方差表 (variance schedule)。它们是 \(T\) 个很小的正常数(例如,\(\beta_1 = 10^{-4}, \beta_T = 0.02\))。
- \(\sqrt{1 - \beta_t} \mathbf{x}_{t-1}\):这是缩放项(大纲)。我们在添加噪声之前先将前一步的图片向量“缩小”一点。
- \(\beta_t \mathbf{I}\):这是噪声项。\(\beta_t\) 是添加的噪声的方差,\(\mathbf{I}\) 是单位矩阵,表示噪声在所有维度(像素)上是独立同分布的。
重参数化技巧的应用: 我们可以使用第 1 部分中的重参数化技巧来显式地写出这个采样过程: \[ \mathbf{x}_t = \sqrt{1 - \beta_t} \mathbf{x}_{t-1} + \sqrt{\beta_t} \boldsymbol{\epsilon}_{t-1} \] 其中 \(\boldsymbol{\epsilon}_{t-1} \sim \mathcal{N}(0, \mathbf{I})\) 是在 \(t-1\) 时刻采样的一个标准高斯噪声。
2.2 累积加噪过程 \(q(\mathbf{x}_t | \mathbf{x}_0)\) (核心推导)
问题: 在训练期间(如大纲 所述),我们希望随机跳到任意 \(t\) 步并生成 \(\mathbf{x}_t\)。如果我们必须从 \(\mathbf{x}_0\) 迭代 \(t\) 次,这将非常缓慢。
目标: 我们需要一个公式,能让我们从 \(\mathbf{x}_0\) 一次性得到 \(\mathbf{x}_t\) 的分布 \(q(\mathbf{x}_t | \mathbf{x}_0)\)。这就是大纲 中提到的“构造出高斯分布的累计变换”。
推导: 1. 定义新变量: 为了简化推导,我们定义 \(\alpha_t = 1 - \beta_t\) 和 \(\bar{\alpha}_t = \prod_{i=1}^t \alpha_i\)。 * \(\alpha_t\) 是每一步的缩放因子。 * \(\bar{\alpha}_t\) 是从第 1 步到第 \(t\) 步的累积缩放因子。
展开迭代 (Step-by-step Expansion): 让我们从 \(\mathbf{x}_t\) 开始,逐步代入 \(\mathbf{x}_{t-1}\): \[ \mathbf{x}_t = \sqrt{\alpha_t} \mathbf{x}_{t-1} + \sqrt{1 - \alpha_t} \boldsymbol{\epsilon}_{t-1} \] 现在,我们代入 \(\mathbf{x}_{t-1} = \sqrt{\alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{1 - \alpha_{t-1}} \boldsymbol{\epsilon}_{t-2}\): \[ \begin{aligned} \mathbf{x}_t &= \sqrt{\alpha_t} (\sqrt{\alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{1 - \alpha_{t-1}} \boldsymbol{\epsilon}_{t-2}) + \sqrt{1 - \alpha_t} \boldsymbol{\epsilon}_{t-1} \\ &= \sqrt{\alpha_t \alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{\alpha_t(1 - \alpha_{t-1})} \boldsymbol{\epsilon}_{t-2} + \sqrt{1 - \alpha_t} \boldsymbol{\epsilon}_{t-1} \end{aligned} \]
合并高斯噪声 (Merging Gaussians): 注意上式的后两项:\(\sqrt{\alpha_t(1 - \alpha_{t-1})} \boldsymbol{\epsilon}_{t-2}\) 和 \(\sqrt{1 - \alpha_t} \boldsymbol{\epsilon}_{t-1}\)。
- \(\boldsymbol{\epsilon}_{t-2}\) 和 \(\boldsymbol{\epsilon}_{t-1}\) 是两个独立的标准高斯分布。
- 我们正在对两个独立的、均值为 0 的高斯分布进行线性组合。
- 根据第 1 部分的性质 1.3 (两个独立高斯分布之和),它们的和仍然是一个均值为 0 的高斯分布。
- 这个新的高斯分布的方差是多少? \[ \begin{aligned} \text{Var}(\text{new\_noise}) &= \text{Var}(\sqrt{\alpha_t(1 - \alpha_{t-1})} \boldsymbol{\epsilon}_{t-2}) + \text{Var}(\sqrt{1 - \alpha_t} \boldsymbol{\epsilon}_{t-1}) \\ &= (\alpha_t(1 - \alpha_{t-1})) \mathbf{I} + (1 - \alpha_t) \mathbf{I} \\ &= (\alpha_t - \alpha_t\alpha_{t-1} + 1 - \alpha_t) \mathbf{I} \\ &= (1 - \alpha_t\alpha_{t-1}) \mathbf{I} \end{aligned} \]
- 根据重参数化技巧,一个方差为 \((1 - \alpha_t\alpha_{t-1}) \mathbf{I}\) 的高斯分布,可以写成 \(\sqrt{1 - \alpha_t\alpha_{t-1}} \cdot \bar{\boldsymbol{\epsilon}}_{t-2}\),其中 \(\bar{\boldsymbol{\epsilon}}_{t-2} \sim \mathcal{N}(0, \mathbf{I})\) 是一个新的标准高斯噪声。
递归与通项公式: 我们将合并后的噪声代入第 2 步的展开式: \[ \mathbf{x}_t = \sqrt{\alpha_t \alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{1 - \alpha_t \alpha_{t-1}} \bar{\boldsymbol{\epsilon}}_{t-2} \]
- \(\mathbf{x}_t\) 和 \(\mathbf{x}_{t-2}\) 的关系,与 \(\mathbf{x}_t\) 和 \(\mathbf{x}_{t-1}\) 的关系(\(\mathbf{x}_t = \sqrt{\alpha_t} \mathbf{x}_{t-1} + \sqrt{1 - \alpha_t} \boldsymbol{\epsilon}_{t-1}\))在形式上是完全一致的!只是 \(\alpha_t\) 变成了 \(\alpha_t \alpha_{t-1}\)。
- 我们可以将这个模式递归地应用 \(t\) 次: \[ \begin{aligned} \mathbf{x}_t &= \sqrt{(\alpha_t \alpha_{t-1} \cdots \alpha_1)} \mathbf{x}_0 + \sqrt{1 - (\alpha_t \alpha_{t-1} \cdots \alpha_1)} \boldsymbol{\epsilon} \\ &= \sqrt{\bar{\alpha}_t} \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon} \end{aligned} \]
- 其中 \(\boldsymbol{\epsilon} \sim \mathcal{N}(0, \mathbf{I})\) 是一个(合并了 \(t\) 次的)标准高斯噪声。
2.3 前向过程的最终公式
我们得到了前向过程中最关键的累积加噪公式: \[ q(\mathbf{x}_t | \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_t; \sqrt{\bar{\alpha}_t} \mathbf{x}_0, (1 - \bar{\alpha}_t) \mathbf{I}) \] 这个公式的重参数化形式为: \[ \mathbf{x}_t = \sqrt{\bar{\alpha}_t} \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon} \] 意义: * 训练效率: 这个公式是 DDPM 训练效率的关键。我们不需要迭代 \(t\) 次来生成 \(\mathbf{x}_t\)。 * 随机训练: 在训练神经网络时,我们可以: 1. 从数据集中拿一张清晰图片 \(\mathbf{x}_0\)。 2. 随机选择一个时间步 \(t\)(例如 \(t=150\))。 3. 从 \(\mathcal{N}(0, \mathbf{I})\) 中采样一个噪声 \(\boldsymbol{\epsilon}\)。 4. 使用上述公式一步计算出 \(\mathbf{x}_t = \sqrt{\bar{\alpha}_t} \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon}\)。 5. 将 \((\mathbf{x}_t, t, \boldsymbol{\epsilon})\) 喂给神经网络进行训练。
当 \(t \to T\) 时(例如 \(T=1000\)),\(\bar{\alpha}_T = \prod_{i=1}^T (1 - \beta_i)\)。由于所有的 \(\beta_i > 0\),\(\bar{\alpha}_T\) 会非常接近 0。 此时: \[ \mathbf{x}_T \approx \sqrt{0} \mathbf{x}_0 + \sqrt{1 - 0} \boldsymbol{\epsilon} = \boldsymbol{\epsilon} \] 这意味着,在 \(T\) 步之后,\(\mathbf{x}_T\) 的分布 \(q(\mathbf{x}_T | \mathbf{x}_0) \approx \mathcal{N}(0, \mathbf{I})\),它几乎完全变成了标准高斯噪声,并且与 \(\mathbf{x}_0\) 无关。
成功地将复杂的图片分布 \(q(\mathbf{x}_0)\) 转化为了简单的标准高斯分布 \(q(\mathbf{x}_T)\)。
小结
核心问题:如何逆转这个过程?如何从一张纯噪声图片 \(\mathbf{x}_T \sim \mathcal{N}(0, \mathbf{I})\) 出发,一步步去噪,最终得到一张清晰的图片 \(\mathbf{x}_0\)?
这需要推导反向去噪过程 (Reverse Process) \(p_\theta(\mathbf{x}_{t-1} | \mathbf{x}_t)\)。
3. 反向过程:从噪声到图像 (The Reverse Process)
我们的目标是学习反向的马尔可夫链,即 \(p_\theta(\mathbf{x}_{t-1} | \mathbf{x}_t)\)。
3.1 棘手的目标 \(p(\mathbf{x}_{t-1} | \mathbf{x}_t)\)
我们想从 \(\mathbf{x}_t\) 推导出 \(\mathbf{x}_{t-1}\)。根据贝叶斯公式: \[ p(\mathbf{x}_{t-1} | \mathbf{x}_t) = \frac{p(\mathbf{x}_t | \mathbf{x}_{t-1}) p(\mathbf{x}_{t-1})}{p(\mathbf{x}_t)} \] * \(p(\mathbf{x}_t | \mathbf{x}_{t-1})\) 就是前向过程 \(q(\mathbf{x}_t | \mathbf{x}_{t-1})\),我们已知。 * \(p(\mathbf{x}_{t-1})\) 是 \(t-1\) 时刻的边缘分布,需要对所有 \(\mathbf{x}_0\) 积分 \(p(\mathbf{x}_{t-1}) = \int q(\mathbf{x}_{t-1} | \mathbf{x}_0) q(\mathbf{x}_0) d\mathbf{x}_0\),这依赖于 \(q(\mathbf{x}_0)\)(真实数据分布),极其困难 (intractable)。 * \(p(\mathbf{x}_t)\) 同样难以计算。
3.2 DDPM 的核心创见:利用 \(\mathbf{x}_0\) (对应)
关键洞察: 虽然 \(p(\mathbf{x}_{t-1} | \mathbf{x}_t)\) 难以计算,但如果我们额外知道 \(\mathbf{x}_0\),这个后验分布 \(q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0)\) 是可计算的!
为什么?因为我们定义了所有 \(q\) 的前向步骤。我们再次使用贝叶斯公式 (对应): \[ q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0) = \frac{q(\mathbf{x}_t | \mathbf{x}_{t-1}, \mathbf{x}_0) \cdot q(\mathbf{x}_{t-1} | \mathbf{x}_0)}{q(\mathbf{x}_t | \mathbf{x}_0)} \] 利用马尔可夫性质 \(q(\mathbf{x}_t | \mathbf{x}_{t-1}, \mathbf{x}_0) = q(\mathbf{x}_t | \mathbf{x}_{t-1})\),我们得到: \[ q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0) \propto q(\mathbf{x}_t | \mathbf{x}_{t-1}) \cdot q(\mathbf{x}_{t-1} | \mathbf{x}_0) \] 我们已知这三个分布都是高斯分布(在第 2 部分已推导): 1. \(q(\mathbf{x}_t | \mathbf{x}_{t-1}) = \mathcal{N}(\mathbf{x}_t; \sqrt{\alpha_t} \mathbf{x}_{t-1}, \beta_t \mathbf{I})\) 2. \(q(\mathbf{x}_{t-1} | \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_{t-1}; \sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_0, (1 - \bar{\alpha}_{t-1}) \mathbf{I})\) 3. \(q(\mathbf{x}_t | \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_t; \sqrt{\bar{\alpha}_t} \mathbf{x}_0, (1 - \bar{\alpha}_t) \mathbf{I})\)
我们正在用 \(\mathbf{x}_{t-1}\) 作为变量,乘以两个高斯分布的概率密度函数 (PDF)。高斯 PDF 的形式是 \(C \cdot \exp(-\frac{(x - \mu)^2}{2\sigma^2})\)。两个高斯 PDF 相乘的结果仍然是一个高斯分布。
通过匹配 \(\mathbf{x}_{t-1}\) 的一次项和二次项系数(一个繁琐但直接的代数过程),我们可以解出这个新高斯分布的均值 \(\tilde{\boldsymbol{\mu}}_t\) 和方差 \(\tilde{\beta}_t\):
\[ q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_{t-1}; \tilde{\boldsymbol{\mu}}_t(\mathbf{x}_t, \mathbf{x}_0), \tilde{\beta}_t \mathbf{I}) \] 其中: * 方差 \(\tilde{\beta}_t\):不依赖于 \(\mathbf{x}_t\) 或 \(\mathbf{x}_0\),它是一个固定的超参数。 \[ \tilde{\beta}_t = \frac{1 - \bar{\alpha}_{t-1}}{1 - \bar{\alpha}_t} \cdot \beta_t \] * 均值 \(\tilde{\boldsymbol{\mu}}_t\):依赖于 \(\mathbf{x}_t\) 和 \(\mathbf{x}_0\)。 \[ \tilde{\boldsymbol{\mu}}_t(\mathbf{x}_t, \mathbf{x}_0) = \frac{\sqrt{\bar{\alpha}_{t-1}}\beta_t}{1 - \bar{\alpha}_t} \mathbf{x}_0 + \frac{\sqrt{\alpha_t}(1 - \bar{\alpha}_{t-1})}{1 - \bar{\alpha}_t} \mathbf{x}_t \]
4. 训练:学习反向过程 (Training)
4.1 神经网络的目标
我们有了一个完美的目标分布 \(q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0)\)。但它有个问题:在推理 (Inference) 时,我们从 \(\mathbf{x}_T\) 开始,并不知道 \(\mathbf{x}_0\)。
因此,我们训练一个神经网络 \(p_\theta\) 来近似这个分布: \[ p_\theta(\mathbf{x}_{t-1} | \mathbf{x}_t) = \mathcal{N}(\mathbf{x}_{t-1}; \boldsymbol{\mu}_\theta(\mathbf{x}_t, t), \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t)) \] 我们的目标是让 \(p_\theta\) 尽可能接近 \(q\)。 * 简化1 (固定方差):DDPM 论文发现,将神经网络的方差 \(\boldsymbol{\Sigma}_\theta\) 固定为 \(\tilde{\beta}_t \mathbf{I}\) 或 \(\beta_t \mathbf{I}\) 效果最好。这极大地简化了问题:神经网络只需要学习均值 \(\boldsymbol{\mu}_\theta\)。 * 简化2 (学习目标):我们训练 \(\boldsymbol{\mu}_\theta(\mathbf{x}_t, t)\) 来预测真实均值 \(\tilde{\boldsymbol{\mu}}_t(\mathbf{x}_t, \mathbf{x}_0)\)。
4.2 DDPM 的关键改进:预测噪声 (对应)
\(\tilde{\boldsymbol{\mu}}_t\) 的公式 \(\frac{\sqrt{\bar{\alpha}_{t-1}}\beta_t}{1 - \bar{\alpha}_t} \mathbf{x}_0 + \frac{\sqrt{\alpha_t}(1 - \bar{\alpha}_{t-1})}{1 - \bar{\alpha}_t} \mathbf{x}_t\) 仍然很复杂。
DDPM 论文提出了一个重要的的重参数化: 我们回顾第 2 部分的前向公式:\(\mathbf{x}_t = \sqrt{\bar{\alpha}_t} \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon}\) 我们可以用它来反解 \(\mathbf{x}_0\)(在 \(\mathbf{x}_t\) 和 \(\boldsymbol{\epsilon}\) 已知的情况下): \[ \mathbf{x}_0 = \frac{1}{\sqrt{\bar{\alpha}_t}} (\mathbf{x}_t - \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon}) \] 现在,我们将这个 \(\mathbf{x}_0\) 的表达式代入上面 \(\tilde{\boldsymbol{\mu}}_t\) 的复杂公式中: \[ \begin{aligned} \tilde{\boldsymbol{\mu}}_t &= \frac{\sqrt{\bar{\alpha}_{t-1}}\beta_t}{1 - \bar{\alpha}_t} \left( \frac{1}{\sqrt{\bar{\alpha}_t}} (\mathbf{x}_t - \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon}) \right) + \frac{\sqrt{\alpha_t}(1 - \bar{\alpha}_{t-1})}{1 - \bar{\alpha}_t} \mathbf{x}_t \\ &= \left( \frac{\sqrt{\bar{\alpha}_{t-1}}\beta_t}{(1 - \bar{\alpha}_t)\sqrt{\bar{\alpha}_t}} + \frac{\sqrt{\alpha_t}(1 - \bar{\alpha}_{t-1})}{1 - \bar{\alpha}_t} \right) \mathbf{x}_t - \left( \frac{\sqrt{\bar{\alpha}_{t-1}}\beta_t \sqrt{1 - \bar{\alpha}_t}}{(1 - \bar{\alpha}_t)\sqrt{\bar{\alpha}_t}} \right) \boldsymbol{\epsilon} \end{aligned} \] (经过一系列基于 \(\bar{\alpha}_t = \alpha_t \bar{\alpha}_{t-1}\) 和 \(\beta_t = 1 - \alpha_t\) 的代数化简) \[ \tilde{\boldsymbol{\mu}}_t = \frac{1}{\sqrt{\alpha_t}} \left( \mathbf{x}_t - \frac{\beta_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon} \right) \] 分析这个优美的公式: * \(\alpha_t\), \(\beta_t\), \(\bar{\alpha}_t\) 都是预先设定的超参数。 * \(\mathbf{x}_t\) 是神经网络的输入。 * 唯一未知的就是 \(\boldsymbol{\epsilon}\) —— 那个在第 2 部分用于从 \(\mathbf{x}_0\) 生成 \(\mathbf{x}_t\) 的原始噪声。
结论(DDPM 核心思想): (对应) 与其让神经网络 \(\boldsymbol{\mu}_\theta\) 预测那个复杂的均值 \(\tilde{\boldsymbol{\mu}}_t\),我们可以让它转而去预测这个噪声 \(\boldsymbol{\epsilon}\)。 我们定义一个神经网络(通常是 U-Net 结构)\(\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\),它的目标就是预测 \(\boldsymbol{\epsilon}\)。
4.3 训练流程与损失函数 (对应-[01:24:40])
- 从数据集中随机抽取一张清晰图像 \(\mathbf{x}_0\)。
- 随机选择一个时间步 \(t\)(从 1 到 \(T\))。(对应 随机训练)
- 随机采样一个标准高斯噪声 \(\boldsymbol{\epsilon} \sim \mathcal{N}(0, \mathbf{I})\)。(对应)
- 使用前向公式一步生成加噪图像:\(\mathbf{x}_t = \sqrt{\bar{\alpha}_t} \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon}\)。
- 将 \((\mathbf{x}_t, t)\) 作为输入,喂给神经网络 \(\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\),得到预测噪声 \(\boldsymbol{\epsilon}_\theta\)。
- 计算损失函数(均方误差 MSE):(对应) \[ L(\theta) = E_{t, \mathbf{x}_0, \boldsymbol{\epsilon}} \left[ || \boldsymbol{\epsilon} - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) ||^2 \right] \]
- 使用梯度下降更新网络参数 \(\theta\)。
5. 推理:逐步去噪生成图像 (Inference)
当训练好 \(\boldsymbol{\epsilon}_\theta\) 后,就可以从纯噪声生成图像了:
- 起始: 从标准高斯分布中采样一张纯噪声图像 \(\mathbf{x}_T \sim \mathcal{N}(0, \mathbf{I})\)。
- 迭代: 从 \(t =
T\) 循环到 \(t = 1\):
- 将当前的 \(\mathbf{x}_t\) 和时间步 \(t\) 输入网络,得到噪声预测:\(\boldsymbol{\epsilon}_\theta = \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\)。(对应)
- 使用 \(\boldsymbol{\epsilon}_\theta\) 作为我们对 \(\boldsymbol{\epsilon}\) 的最佳估计,代入 4.2 中的均值公式,计算 \(t-1\) 步的均值 \(\boldsymbol{\mu}_\theta(\mathbf{x}_t, t)\):(对应) \[ \boldsymbol{\mu}_\theta(\mathbf{x}_t, t) = \frac{1}{\sqrt{\alpha_t}} \left( \mathbf{x}_t - \frac{\beta_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta \right) \]
- 计算 \(t-1\) 步的方差。我们使用固定的方差 \(\sigma_t^2 \mathbf{I} = \tilde{\beta}_t \mathbf{I} = \frac{1 - \bar{\alpha}_{t-1}}{1 - \bar{\alpha}_t} \beta_t \mathbf{I}\)。
- 采样 (Sampling) (对应):从这个高斯分布中采样 \(\mathbf{x}_{t-1}\): \[ \mathbf{x}_{t-1} = \boldsymbol{\mu}_\theta(\mathbf{x}_t, t) + \sigma_t \mathbf{z} \] 其中 \(\mathbf{z} \sim \mathcal{N}(0, \mathbf{I})\) 是一个新采样的随机噪声。 (注意:当 \(t=1\) 时,\(\mathbf{z}\) 设为 0,因为 \(\mathbf{x}_0\) 应该是一个确定性的输出,不再添加噪声)。
- 结束: 当循环结束时,\(\mathbf{x}_0\) 就是生成的清晰图像。(对应)
小结
完整地推导了 DDPM 的核心数学原理: 1. 前向过程 \(q\):使用 \(q(\mathbf{x}_t | \mathbf{x}_0)\) 高效加噪。 2. 反向过程 \(p_\theta\):通过贝叶斯公式推导出 \(q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0)\) 作为理想目标。 3. 训练 \(p_\theta\):通过让 \(\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\) 预测真实噪声 \(\boldsymbol{\epsilon}\) 来简化训练目标 (MSE Loss)。 4. 推理 \(p_\theta\):从 \(\mathbf{x}_T\) 开始,利用 \(\boldsymbol{\epsilon}_\theta\) 预测的均值,逐步采样 \(\mathbf{x}_{t-1}\)。
DDPM 的一个主要缺点是推理速度慢(需要 \(T\) 步,例如 1000 步)。
DDIM (Denoising Diffusion Implicit Models)。
DDPM 的效果很好,但它有两个主要缺点: 1. 推理速度慢: 它是一个马尔可夫过程,从 \(\mathbf{x}_T\) 生成 \(\mathbf{x}_0\) 必须执行 \(T\) 步(例如 1000 步)采样,非常耗时。 2. 推理是随机的: (Stochastic) 在每一步采样 \(\mathbf{x}_{t-1} = \boldsymbol{\mu}_\theta(\mathbf{x}_t, t) + \sigma_t \mathbf{z}\) 时,都需要加入一个新的随机噪声 \(\mathbf{z}\)。这意味着即使从同一个 \(\mathbf{x}_T\) 出发,两次推理也会得到不同的 \(\mathbf{x}_0\)。这对于需要一致性的任务(如图像编辑)来说是个问题。
DDIM(2020年提出)巧妙地解决了这两个问题,并且无需重新训练在 DDPM 上训练好的模型。
这对应于您大纲中的 - 部分。
6. DDIM:推理升级
6.1 DDIM 的核心洞察:重新审视反向过程
DDIM 的出发点是重新审视我们推导出的反向过程。DDPM 假设反向过程是 \(p_\theta(\mathbf{x}_{t-1} | \mathbf{x}_t)\),并用它来近似 \(q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0)\)。
DDIM 注意到,我们训练的神经网络 \(\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\) 实际上是在预测噪声 \(\boldsymbol{\epsilon}\)。 回顾我们的前向公式: \[ \mathbf{x}_t = \sqrt{\bar{\alpha}_t} \mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon} \] 既然我们有了 \(\mathbf{x}_t\)(当前输入)和 \(\boldsymbol{\epsilon}_\theta\)(网络预测的 \(\boldsymbol{\epsilon}\)),我们可以直接反解出对清晰图像 \(\mathbf{x}_0\) 的预测,我们称之为 \(\hat{\mathbf{x}}_0\):
\[ \hat{\mathbf{x}}_0 = \frac{1}{\sqrt{\bar{\alpha}_t}} \left( \mathbf{x}_t - \sqrt{1 - \bar{\alpha}_t} \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \right) \] 这个 \(\hat{\mathbf{x}}_0\) 是给定 \(\mathbf{x}_t\) 时,模型对最终结果 \(\mathbf{x}_0\) 的“最佳猜测”。
6.2 升级 1:确定性推理 (Deterministic Inference) (对应)
DDPM 的采样公式为: \[ \mathbf{x}_{t-1} = \underbrace{\boldsymbol{\mu}_\theta(\mathbf{x}_t, t)}_{\text{均值}} + \underbrace{\sigma_t \mathbf{z}}_{\text{随机噪声}} \] DDIM 提出,这个过程不一定是随机的。DDIM 引入了一个新的参数 \(\eta\) (eta) 来控制随机性。 * 当 \(\eta=1\) 时,DDIM 的采样过程与 DDPM 完全相同(随机)。 * 当 \(\eta=0\) 时,采样过程中的方差 \(\sigma_t\) 被设为 0。
当 \(\eta=0\)(方差 \(\sigma_t=0\))时,采样步骤变为: \[ \mathbf{x}_{t-1} = \boldsymbol{\mu}_\theta(\mathbf{x}_t, t) \] 这是确定性的! 没有随机噪声 \(\mathbf{z}\) 的介入。
这有什么用? 这意味着从一个固定的 \(\mathbf{x}_T\) 出发,无论运行多少次,总会生成完全相同的 \(\mathbf{x}_0\)。这使得扩散模型可用于图像编辑、风格转换等需要保持一致性的任务。
DDIM 论文推导出了一个更通用的采样公式,它不依赖于 \(\boldsymbol{\mu}_\theta\) 而是直接使用 \(\hat{\mathbf{x}}_0\) 和 \(\boldsymbol{\epsilon}_\theta\)。 当 \(\eta=0\) (即 \(\sigma_t = 0\)) 时,从 \(\mathbf{x}_t\) 到 \(\mathbf{x}_{t-1}\) 的确定性采样公式为:
\[ \mathbf{x}_{t-1} = \underbrace{\sqrt{\bar{\alpha}_{t-1}} \hat{\mathbf{x}}_0}_{\text{指向“预测的” } \mathbf{x}_0} + \underbrace{\sqrt{1 - \bar{\alpha}_{t-1}} \cdot \left( \frac{\mathbf{x}_t - \sqrt{\bar{\alpha}_t} \hat{\mathbf{x}}_0}{\sqrt{1 - \bar{\alpha}_t}} \right)}_{\text{指向“当前的” } \mathbf{x}_t \text{ 的方向}} \] (注意:\(\frac{\mathbf{x}_t - \sqrt{\bar{\alpha}_t} \hat{\mathbf{x}}_0}{\sqrt{1 - \bar{\alpha}_t}}\) 正好等于 \(\boldsymbol{\epsilon}_\theta\) ) 所以,确定性(\(\eta=0\))的 DDIM 采样步骤也可以写为: \[ \mathbf{x}_{t-1} = \sqrt{\bar{\alpha}_{t-1}} \hat{\mathbf{x}}_0 + \sqrt{1 - \bar{\alpha}_{t-1}} \cdot \boldsymbol{\epsilon}_\theta \]
6.3 升级 2:跳步采样 (Skip Sampling) (对应)
DDPM 必须一步一步 \(t \to t-1 \to t-2 \dots\) 地采样,因为它是马尔可夫过程。
DDIM 的采样公式(如上所示)是非马尔可夫的。它不依赖于 \(q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0)\),而是直接使用在 \(t\) 时刻预测的 \(\hat{\mathbf{x}}_0\) 来计算 \(\mathbf{x}_{t-1}\)。
关键洞察: 既然我们能从 \(\mathbf{x}_t\) 预测出 \(\hat{\mathbf{x}}_0\),我们不仅能计算 \(\mathbf{x}_{t-1}\),我们能计算任意 \(\mathbf{x}_{\tau}\) (其中 \(\tau < t\))。
这使得跳步采样成为可能。我们不再需要完整的 \(T=1000\) 步,我们可以定义一个更短的子序列,例如 \(S=20\) 步: \((\tau_1, \tau_2, \dots, \tau_S) = (1, 51, 101, \dots, 951)\)
我们的推理循环不再是 for t in (T...1),而是
for i in (S...1): * 当前步:\(\tau_i\) (例如 \(\tau_{20} = 951\)) *
目标步:\(\tau_{i-1}\)
(例如 \(\tau_{19} = 901\))
DDIM 跳步采样(确定性)公式: (对应)
- 输入: 当前噪声图像 \(\mathbf{x}_{\tau_i}\) 和时间步 \(\tau_i\)。
- 预测 \(\hat{\mathbf{x}}_0\): (与之前相同) \[ \hat{\mathbf{x}}_0 = \frac{1}{\sqrt{\bar{\alpha}_{\tau_i}}} \left( \mathbf{x}_{\tau_i} - \sqrt{1 - \bar{\alpha}_{\tau_i}} \boldsymbol{\epsilon}_\theta(\mathbf{x}_{\tau_i}, \tau_i) \right) \]
- 计算 \(\mathbf{x}_{\tau_{i-1}}\): (使用确定性公式,将 \(t-1\) 替换为 \(\tau_{i-1}\)) \[ \mathbf{x}_{\tau_{i-1}} = \sqrt{\bar{\alpha}_{\tau_{i-1}}} \hat{\mathbf{x}}_0 + \sqrt{1 - \bar{\alpha}_{\tau_{i-1}}} \cdot \boldsymbol{\epsilon}_\theta(\mathbf{x}_{\tau_i}, \tau_i) \]
结果: 我们不再需要 1000 步计算,而是通过跳步,仅用 20 步就完成了从 \(\mathbf{x}_T\) 到 \(\mathbf{x}_0\) 的生成。这极大地(例如 50 倍)提升了推理速度。
小结
DDIM 是对 DDPM 的一次重大升级,它通过引入 \(\hat{\mathbf{x}}_0\) 预测和非马尔可夫采样,实现了: 1. 确定性推理(\(\eta=0\)),增强了模型的可控性。 2. 跳步采样,极大缩短了推理时间。 3. 最重要的是,它复用了 DDPM 训练好的 \(\boldsymbol{\epsilon}_\theta\) 模型,无需额外训练。
好的,我们来探讨扩散模型演进的下一个重要阶段:流匹配 (Flow Matching)。
在 DDPM 和 DDIM 中,我们都依赖于一个离散时间的 SDE(随机微分方程)或其确定性版本。我们模拟了 \(T\) 个离散步骤(例如 \(T=1000\)),这在数学上是有效的,但在概念上有些繁琐,并且依赖于 \(\beta_t\) (或 \(\bar{\alpha}_t\)) 这个人工设计的“噪声表”。
流匹配 (Flow Matching, FM) 模型(2022年及后续工作)提出了一种更简洁、更根本的视角:连续时间的常微分方程 (ODE)。
这对应于您大纲中的 - 部分。
7. 流匹配:连续轨迹与速度预测
7.1 核心思想:从 SDE 到 ODE
- DDPM (SDE):将图像 \(\mathbf{x}_0\) 变为噪声 \(\mathbf{x}_T\) 的过程是随机的(\(\mathbf{x}_t = \sqrt{\alpha_t} \mathbf{x}_{t-1} + \sqrt{\beta_t} \boldsymbol{\epsilon}\))。
- 流匹配 (ODE):我们构建一个确定性的、连续的“流”,将纯噪声 \(\mathbf{z}\)(我们这里称为 \(\mathbf{x}_0\))平滑地转变为清晰图像 \(\mathbf{x}_1\)。
我们不再考虑离散步骤 \(t=1, 2, \dots, T\),而是考虑一个连续时间 \(t \in [0, 1]\):
- \(t=0\):\(\mathbf{x}_0\) 是从 \(\mathcal{N}(0, \mathbf{I})\) 采样的纯噪声。
- \(t=1\):\(\mathbf{x}_1\) 是我们想要生成的清晰图像。
这个从 \(\mathbf{x}_0\) 到 \(\mathbf{x}_1\) 的连续演变路径 \(\mathbf{x}_t\) 由一个常微分方程 (ODE) 描述:
\[ \frac{d\mathbf{x}_t}{dt} = \mathbf{v}(\mathbf{x}_t, t) \] * \(\mathbf{v}(\mathbf{x}_t, t)\) 是一个速度向量场 (velocity vector field)。 * 它告诉我们:当一个点位于位置 \(\mathbf{x}_t\) 和时间 \(t\) 时,它应该往哪个方向(向量)以多快的速度(模长)移动。 * 训练目标: 我们的神经网络 \(\mathbf{v}_\theta(\mathbf{x}_t, t)\) 的目标就是学习这个速度场 \(\mathbf{v}\),而不是像 DDPM 那样学习噪声 \(\boldsymbol{\epsilon}\)。
7.2 训练:学习速度场 (对应)
问题: 理论上存在一个理想的速度场 \(\mathbf{v}\) 可以将噪声分布“推向”图像分布,但这个理想的 \(\mathbf{v}\) 非常复杂,我们无法知道。
流匹配的创见: 我们不需要知道那个复杂的理想场。我们可以自己定义无数条简单的路径,然后训练网络来学习这些简单路径的平均速度。
1. 定义简单路径(直线模型): 给定一个噪声 \(\mathbf{x}_0 \sim \mathcal{N}(0, \mathbf{I})\) 和一张真实图像 \(\mathbf{x}_1 \sim q(\text{data})\),连接它们的最简单路径是什么?一条直线。
\[\\mathbf{x}\_t = (1 - t) \\mathbf{x}\_0 + t \\mathbf{x}\_1 \] * 当 \(t=0\) 时,\(\mathbf{x}_t = \mathbf{x}_0\) (噪声)。
- 当 \(t=1\) 时,\(\mathbf{x}_t = \mathbf{x}_1\) (图像)。
2. 计算目标速度: 如果我们的“粒子” \(\mathbf{x}_t\) 沿着这条直线路径运动,它在 \(t\) 时刻的速度 \(\mathbf{v}_t\) 是多少?我们对 \(t\) 求导:
\[ \mathbf{v}_t = \frac{d\mathbf{x}_t}{dt} = \frac{d}{dt} \left( (1 - t) \mathbf{x}_0 + t \mathbf{x}_1 \right) \]\[ \mathbf{v}_t = -\mathbf{x}_0 + \mathbf{x}_1 = \mathbf{x}_1 - \mathbf{x}_0 \]这就是流匹配的训练目标! 沿着这条直线路径,在任何时间 \(t\),目标速度都是恒定的 \(\mathbf{x}_1 - \mathbf{x}_0\)。
3. 训练流程:
- 从数据集中随机抽取一张清晰图像 \(\mathbf{x}_1\)。
- 随机采样一个标准高斯噪声 \(\mathbf{x}_0 \sim \mathcal{N}(0, \mathbf{I})\)。
- 随机选择一个时间 \(t\)(从 \(U(0, 1)\) 均匀采样)。
- 使用直线公式一步计算出路径上的点:\(\mathbf{x}_t = (1 - t) \mathbf{x}_0 + t \mathbf{x}_1\)。
- 将 \((\mathbf{x}_t, t)\) 作为输入,喂给神经网络 \(\mathbf{v}_\theta(\mathbf{x}_t, t)\),得到预测速度 \(\mathbf{v}_\theta\)。
- 计算损失函数(均方误差 MSE): $$
1 | $$L(\\theta) = E\_{t, \\mathbf{x}\_0, \\mathbf{x}\_1} \\left[ || (\\mathbf{x}\_1 - \\mathbf{x}*0) - \\mathbf{v}*\\theta(\\mathbf{x}\_t, t) ||^2 \\right] |
- 使用梯度下降更新网络参数 \(\theta\)。
7.3 推理:求解 ODE (对应)
当我们训练好 \(\mathbf{v}_\theta\) 后,我们就有了一个完整的速度场,它知道在时空中的任何点 \((\mathbf{x}, t)\) 应该如何移动。
问题: 如何从 \(\mathbf{x}_0\) 积分到 \(\mathbf{x}_1\)? 我们需要求解 ODE \(\frac{d\mathbf{x}_t}{dt} = \mathbf{v}_\theta(\mathbf{x}_t, t)\),从 \(t=0\) 求解到 \(t=1\)。
方法: 我们使用数值积分方法,最简单的就是欧拉近似法(我们在第 1 部分 提到过)。
- 起始: 随机采样一张纯噪声图像 \(\mathbf{x}_0 \sim \mathcal{N}(0, \mathbf{I})\)。
- 离散化: 将时间 \([0, 1]\) 分为 \(N\) 步(例如 \(N=20\)),每一步 \(\Delta t = 1/N\)。
- 迭代: 从 \(t =
0\) 循环到 \(t = 1 - \Delta
t\):
- 获取当前位置 \(\mathbf{x}_t\) 和时间 \(t\)。
- 输入网络,得到当前速度:\(\mathbf{v}_\theta = \mathbf{v}_\theta(\mathbf{x}_t, t)\)。
- 欧拉法更新: \[ \]\[\\mathbf{x}\_{t + \\Delta t} = \\mathbf{x}*t + \\mathbf{v}*\\theta \\cdot \\Delta t \] $$$$(新位置 = 旧位置 + 速度 × 时间)
- 结束: 当循环结束时,\(\mathbf{x}_1\) 就是生成的清晰图像。
优势:
- 更简单: 训练目标 \(\mathbf{x}_1 - \mathbf{x}_0\) 非常直观,摆脱了 DDPM 中复杂的 \(\bar{\alpha}_t, \beta_t\) 系数。
- 更高效: ODE 路径通常比 SDE 路径“更直”,因此流匹配通常可以用更少的推理步骤(例如 10-50 步)生成高质量图像。
- 更灵活: 我们可以使用比欧拉法更高级的 ODE 求解器(如 Runge-Kutta)来进一步提高精度和速度。
总结
扩散模型从基础到前沿的全部核心数学:
- 基础 (Part 1):高斯分布、朗之万动力学 (SDE) 和贝叶斯公式。
- DDPM (Part 2-5):
- 前向 (q):\(q(\mathbf{x}_t | \mathbf{x}_0) = \mathcal{N}(\sqrt{\bar{\alpha}_t} \mathbf{x}_0, (1 - \bar{\alpha}_t) \mathbf{I})\)
- 反向 (p):\(q(\mathbf{x}_{t-1} | \mathbf{x}_t, \mathbf{x}_0)\) 的推导。
- 训练:预测噪声 \(L = || \boldsymbol{\epsilon} - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) ||^2\)。
- 推理:\(\mathbf{x}_{t-1} = \boldsymbol{\mu}_\theta(\mathbf{x}_t, t) + \sigma_t \mathbf{z}\) (随机,T 步)。
- DDIM (Part 6):
- 核心:预测 \(\hat{\mathbf{x}}_0\)。
- 推理:\(\mathbf{x}_{\tau_{i-1}} = \dots\) (确定性,可跳步)。
- 流匹配 (Part 7):
- 核心:ODE 连续流 \(\frac{d\mathbf{x}_t}{dt} = \mathbf{v}(\mathbf{x}_t, t)\)。
- 训练:预测速度 \(L = || (\mathbf{x}_1 - \mathbf{x}_0) - \mathbf{v}_\theta(\mathbf{x}_t, t) ||^2\)。
- 推理:\(\mathbf{x}_{t + \Delta t} = \mathbf{x}_t + \mathbf{v}_\theta \cdot \Delta t\) (ODE 求解)。