# 祖先采样

**祖先采样**（Ancestral sampling）是用训练好的扩散模型生成数据的标准方式：从纯高斯噪声出发，模型反复从学习到的反向转移分布中抽样，一次一个时间步地对样本去噪，直到出现一个数据样本。这个名字来自每次抽样都以先前状态为条件，追踪训练时所用[马尔可夫链](https://www.edgepedia.org/zh/markov-chain)的反向过程。<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup>

| 关键事实 | 细节 |
|---|---|
| 定义 | 从学习到的反向链 \( p_\theta(\mathbf{x}_{0:T}) = p(\mathbf{x}_T) \prod_{t=1}^T p_\theta(\mathbf{x}_{t-1} \mid \mathbf{x}_t) \) 迭代抽样，起始于 \( \mathbf{x}_T \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) \)<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup> |
| 随机性 | 每一步抽取 \( \mathbf{x}_{t-1} = \mu_\theta(\mathbf{x}_t, t) + \sigma_t \mathbf{z} \)，其中 \( \mathbf{z} \) 为新鲜高斯噪声，因此从同一 \( \mathbf{x}_T \) 出发的两次运行会得到不同结果<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup> |
| DDPM 中的代价 | 当 \( T = 1000 \) 时，生成一个样本需要 1000 次顺序神经网络评估<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup> |
| 确定性对应物 | DDIM 使用同一训练好的模型，实际耗时比 DDPM 祖先采样快 10 倍到 50 倍<sup>[2](https://arxiv.org/abs/2010.02502)</sup> |
| 连续视角 | DDPM 采样对应于离散化反向 SDE；DDIM 对应于反向概率流 ODE，二者共享同一训练目标<sup>[3](https://arxiv.org/abs/2605.22586)</sup> |
| 实际行为 | 祖先采样器在每一步都加入噪声，因此输出会随步数变化，不会稳定到固定图像<sup>[4](https://www.generativelabs.com/insights/exploring-samplers-in-stable-diffusion-types-approaches-and-practical-differences)</sup> |

## 工作原理

扩散模型按两个方向训练。前向过程用噪声逐渐破坏数据，直到 \( t = T \) 时，样本成为纯高斯噪声 \( \mathbf{x}_T = \mathcal{N}(\mathbf{0}, \mathbf{I}) \)。生成时则运行反向过程，每一步去除少量噪声，以回到数据样本 \( \mathbf{x}_0 \)。<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup>

每一步，模型评估样本在当时剩余的噪声项 \( \epsilon_\theta(\mathbf{x}_t, t) \)，并用这个估计计算反向转移均值的近似值，

\[ \mu_\theta(\mathbf{x}_t, t) = \frac{1}{\sqrt{\alpha_t}} \left( \mathbf{x}_t - \frac{\beta_t}{\sqrt{1 - \bar{\alpha}_t}} \epsilon_\theta(\mathbf{x}_t, t) \right), \]

之后抽取下一个样本为

\[ \mathbf{x}_{t-1} = \mu_\theta(\mathbf{x}_t, t) + \sigma_t \mathbf{z}, \qquad \mathbf{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}). \]

正是注入的 \( \mathbf{z} \) 使过程具有随机性：从同一起始噪声 \( \mathbf{x}_T \) 生成的两个样本会产生不同结果，而在 \( T \) 步中反复注入噪声便得到最终样本 \( \mathbf{x}_0 \)。<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup> 这一过程正是乔纳森·霍（Jonathan Ho）、阿杰·贾因（Ajay Jain）和[彼得·阿贝尔](https://www.edgepedia.org/zh/pieter-abbeel)（Pieter Abbeel）的原始 DDPM 论文在通过反转学习到的马尔可夫扩散过程生成图像时所执行的操作。<sup>[5](https://arxiv.org/abs/2006.11239)</sup>

成本是顺序网络评估的次数。如果 \( T = 1000 \)，生成一个样本需要连续进行 1000 次神经网络计算，这在早期扩散模型中是一个显著瓶颈。<sup>[1](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)</sup> 后续工作降低了这一成本：Improved DDPM 表明，学习反向过程的方差可以在样本质量差异可忽略的情况下，将前向传递次数减少一个数量级。<sup>[6](https://proceedings.mlr.press/v139/nichol21a)</sup>

## 与 SDE 和 ODE 的关系

在连续时间视角下，祖先采样是对随机微分方程的离散化。由[杨松](https://www.edgepedia.org/zh/yang-song)（Yang Song）及其同事统一起来的[基于分数的生成模型](https://www.edgepedia.org/zh/score-based-generative-models)，用反向时间 SDE 描述生成过程，该 SDE 将高斯先验变换回数据分布，并由学习到的分数 \( \nabla \log p_t(x) \) 驱动。<sup>[7](https://arxiv.org/abs/2011.13456)</sup> 杨松等人还证明了存在一个概率流 ODE，其轨迹与反向时间 SDE 具有相同的边缘分布。<sup>[8](https://doi.org/10.1145/3626235)</sup>

两类采样器直接对应这些动力学：DDPM 风格的祖先采样对应于离散反向 SDE 采样，而 DDIM 对应于反向 ODE 采样，二者共享同一训练目标。<sup>[3](https://arxiv.org/abs/2605.22586)</sup> 实际差异正源于这一区别。确定性的 ODE 采样器沿一条固定轨迹从噪声到数据，因此相同种子总是产生相同图像，并且误差会沿路径累积。祖先采样器在每一步重新注入噪声，这给过程以纠正早期错误的机会，却使轨迹依赖于历史。<sup>[3](https://arxiv.org/abs/2605.22586)</sup>

## 确定性替代方法

祖先采样的主要替代方案是确定性采样器家族。由宋佳明（Jiaming Song）、孟晨琳（Chenlin Meng）和[斯特凡诺·埃尔蒙](https://www.edgepedia.org/zh/stefano-ermon)（Stefano Ermon）提出的 DDIM 构造了非马尔可夫扩散过程，其训练目标与 DDPM 相同，但反向过程可以采样得快得多；在经验上，它以实际耗时快 10 倍到 50 倍的速度产生高质量样本，并支持在潜空间中进行语义上有意义的插值。<sup>[2](https://arxiv.org/abs/2010.02502)</sup> 当反向噪声项设为零时，DDIM 成为从 \( \mathbf{x}_T \) 到 \( \mathbf{x}_0 \) 的确定性映射。<sup>[2](https://arxiv.org/abs/2010.02502)</sup>

专用 ODE 求解器进一步推进了这一方向。DPM-Solver 是用于扩散 ODE 的高阶求解器，仅需 10 到 20 次函数评估就能生成高质量样本，在 CIFAR-10 上 10 次评估达到 4.70 FID，20 次评估达到 2.87 FID。<sup>[9](https://arxiv.org/abs/2206.00927)</sup> DPM-Solver++ 将其扩展到引导采样，可在 15 到 20 步内为像素空间和潜空间扩散模型生成高质量样本。<sup>[10](https://arxiv.org/abs/2211.01095)</sup>

## 实际行为与噪声的作用

**不收敛。** 因为祖先采样器在每一步都加入新鲜噪声，图像会随着步数变化而不断变化。[Stable Diffusion](https://www.edgepedia.org/zh/stable-diffusion) 的实践指南指出，对祖先采样器而言，更多步数不是“更好”，而是“不同”，并建议选定一个步数并保持不变以保证可复现性。<sup>[4](https://www.generativelabs.com/insights/exploring-samplers-in-stable-diffusion-types-approaches-and-practical-differences)</sup> 对于需要可复现性的生产工作流，建议谨慎使用祖先采样器，即名称中带有“a”或“SDE”的采样器，例如 Euler a 或 DPM++ SDE。<sup>[11](https://book.st-hakky.com/en/data-science/role-and-types-of-samplers-in-stable-diffusion-for-image-generation)</sup> Easy Diffusion 文档同样将 `euler_a`、`dpm2_a` 和 `dpmpp_2s_a` 等祖先变体列为不总是确定性的。<sup>[12](https://github.com/easydiffusion/easydiffusion/wiki/Samplers)</sup>

**随机性何时有帮助。** 祖先采样所加入的噪声是否能提升质量，取决于模型和数据。泰罗·卡拉斯（Tero Karras）、米卡·艾塔拉（Miika Aittala）、蒂莫·艾拉（Timo Aila）和萨穆利·莱内（Samuli Laine）的 EDM 设计空间研究发现，在 CIFAR-10 上采用杨松等人的原始训练设置时，随机采样非常有益，而确定性采样导致相对较差的 FID；但在他们改进的训练设置下，情况反了过来：确定性采样最好，任何随机性都有害。然而，在类别条件 ImageNet-64 上，即使采用改进设置，随机采样也明显优于确定性采样，这表明更多样的数据集仍能从中受益。<sup>[13](https://arxiv.org/abs/2206.00364)</sup>

**常见实现。** 在 Diffusers 库中，祖先采样出现在 `EulerAncestralDiscreteScheduler` 等调度器中；该调度器用欧拉法步进执行祖先采样，被描述为一种快速调度器，通常能在 20 到 30 步内生成良好输出，并基于凯瑟琳·克罗森（Katherine Crowson）的原始 k-diffusion 实现。<sup>[14](https://huggingface.co/docs/diffusers/main/api/schedulers/euler_ancestral)</sup> 其 `step` 函数通过反转 SDE 来预测上一时间步的样本，从模型预测的噪声出发推进该过程。<sup>[14](https://huggingface.co/docs/diffusers/main/api/schedulers/euler_ancestral)</sup>

## References

1. [Turning Noise into Knowledge: The Statistics Behind Diffusion Models, INFORMS OR/MS Tomorrow](https://www.informs.org/Publications/OR-MS-Tomorrow/Turning-Noise-into-Knowledge-The-Statistics-Behind-Di-usion-Models)
2. [Jiaming Song, Chenlin Meng, Stefano Ermon (2020). Denoising Diffusion Implicit Models. arXiv:2010.02502.](https://arxiv.org/abs/2010.02502)
3. [Jiayi Fu, Yuxia Wang (2026). A Tutorial on Diffusion Theory: From Differential Equations to Diffusion Models. arXiv:2605.22586.](https://arxiv.org/abs/2605.22586)
4. [Exploring Samplers in Stable Diffusion: Types, Approaches, and Practical Differences, Generative Labs](https://www.generativelabs.com/insights/exploring-samplers-in-stable-diffusion-types-approaches-and-practical-differences)
5. [Jonathan Ho, Ajay Jain, Pieter Abbeel (2020). Denoising Diffusion Probabilistic Models. arXiv:2006.11239.](https://arxiv.org/abs/2006.11239)
6. [Alexander Quinn Nichol, Prafulla Dhariwal (2021). Improved Denoising Diffusion Probabilistic Models. PMLR 139:8162-8171.](https://proceedings.mlr.press/v139/nichol21a)
7. [Yang Song et al. (2020). Score-Based Generative Modeling through Stochastic Differential Equations. arXiv:2011.13456.](https://arxiv.org/abs/2011.13456)
8. [Diffusion Models: A Comprehensive Survey of Methods and Applications. ACM Computing Surveys.](https://doi.org/10.1145/3626235)
9. [Cheng Lu et al. (2022). DPM-Solver: A Fast ODE Solver for Diffusion Probabilistic Model Sampling in Around 10 Steps. arXiv:2206.00927.](https://arxiv.org/abs/2206.00927)
10. [Cheng Lu et al. (2022). DPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models. arXiv:2211.01095.](https://arxiv.org/abs/2211.01095)
11. [Choosing Stable Diffusion Samplers: Settings and Comparison, Hakky Handbook](https://book.st-hakky.com/en/data-science/role-and-types-of-samplers-in-stable-diffusion-for-image-generation)
12. [Samplers, Easy Diffusion Wiki](https://github.com/easydiffusion/easydiffusion/wiki/Samplers)
13. [Tero Karras, Miika Aittala, Timo Aila, Samuli Laine (2022). Elucidating the Design Space of Diffusion-Based Generative Models. arXiv:2206.00364.](https://arxiv.org/abs/2206.00364)
14. [EulerAncestralDiscreteScheduler, Hugging Face Diffusers documentation](https://huggingface.co/docs/diffusers/main/api/schedulers/euler_ancestral)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Generative media methods: diffusion, flow, and autoregressive generation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/ancestral-sampling

English original: https://www.edgepedia.org/ancestral-sampling
