Files
Notes/MachineLearning/Models/DDPM/Denoising-Diffusion-Probalistic-Model.md
2026-02-13 19:45:50 +08:00

63 lines
3.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
### Diffusion 扩散模型 DDPM 理解
#### 一、概述
Diffusion是一个类似多层VAE架构的模型通过输入一个具体的样本 $x$ ,通过不断增加噪音使整个样本最终达到一个**已知的**状态,然后再通过**前向过程确定的真实后验**学习,最终学习到一个近似的$p_\theta(x_{t-1}| x_t)$
#### 二、详细分析
##### 第一部分:噪声处理-马尔可夫链
![image.png](/image.png)
如图所示,我们人为定义一个**加噪过程**
$$ x_t = \sqrt{\alpha_t} x_{t - 1} + \sqrt{1 - {\alpha}_t} \epsilon_t ,其中\epsilon_t \sim \mathcal N(0, I) $$
这个式子改写为下面的形式:
$x_t \sim \mathcal N(\sqrt{\alpha_t} x_{t - 1}, (1 - {\alpha}_t) I)$ ,其中前一部分是均值,后一部分是**方差**(协方差矩阵);
$$ p(x_t|x_{t-1}) = \mathcal N(\sqrt{\alpha_t} x_{t - 1}, (1 - {\alpha}_t) I)$$
由马尔科夫链,**每一个$x_t$只和上一个时间的$x_{t-1}$状态有关**,则我们可以将上述式子推广到:
$x_t = \sqrt{\alpha_t} x_{t - 1} + \sqrt{1 - {\alpha}_t} \epsilon_t$
$x_{t-1} = \sqrt{\alpha_{t-1}} x_{t - 2} + \sqrt{1 - {\alpha}_{t-1}} \epsilon_{t-1}$
$...$
$x_1 = \sqrt{\alpha_1} x_{0} + \sqrt{1 - {\alpha}_1} \epsilon_1$
依次带入上述式子,可以经过化简后得到
$x_t = \sqrt{\bar{\alpha_t}} x_{0} + \sqrt{1 - \bar{{\alpha}_t}} \epsilon'$ ,注意此处是合成后的$\epsilon' \sim \mathcal{N}(0, I)$
与下面三个式子等价
$x_0 = \frac{1}{\sqrt{\bar{\alpha_t}}}(x_t - \sqrt{1 - \bar{{\alpha}_t}} \epsilon')$
$x_t \sim \mathcal N(\sqrt{\bar\alpha_t} x_0, (1 - {\bar\alpha}_t) I)$
$p(x_t|x_{0}) = \mathcal N(\sqrt{\bar\alpha_t} x_0, (1 - {\bar\alpha}_t) I)$
##### 第二部分:学习真实后验分布
我们希望模型学习到**逆向过程的条件分布** $p_\theta(x_{t-1} | x_t)$,用**前向过程的真实后验** $q(x_{t-1} | x_t, x_0)$ 作为指导(训练时近似:$q(x_{t-1} | x_t, x_0) \approx p_\theta(x_{t-1} | x_t)$。对 $q$ 进行贝叶斯展开:
把$x_0$看作先验条件,由 $p(A|B) = \frac{p(B|A) p(A)}{p(B)}$,得到:
$$ q(x_{t-1} | x_t, x_0) = \frac{p(x_t | x_{t-1}, x_0) p(x_{t-1} | x_0)}{p(x_t | x_0)} $$
**由于马尔可夫链的性质,我们可以把等式右边第一项中的 $x_0$ 消掉**,得到:
$$ q(x_{t-1} | x_t, x_0) = \frac{p(x_t | x_{t-1}) p(x_{t-1} | x_0)}{p(x_t | x_0)} $$
接下来计算这三个值:由第一部分,三个概率分布均已知
$$ p(x_t | x_{t-1}) = \mathcal N(\sqrt{\alpha_t} x_{t - 1}, (1 - {\alpha}_t) I) $$
$$ p(x_{t-1} | x_{0}) = \mathcal N(\sqrt{\bar\alpha_{t-1}} x_0, (1 - {\bar\alpha}_{t-1}) I) $$
$$ p(x_t | x_{0}) = \mathcal N(\sqrt{\bar\alpha_t} x_0, (1 - {\bar\alpha}_t) I) $$
将上面三个式子全部改写为单变量正态分布函数:
$$ \rho(x) = \frac{1}{\sqrt{2\pi \sigma^2}} e^{-\frac{(x - \mu)^2}{2 \sigma^2}} $$
(注:指数为负)
经过化简(高斯乘积/除法),原式正比于 $e^{a x_{t-1}^2 + b x_{t-1} + c}$ 形式,配方后得到一个高斯分布:均值与 $x_0, x_t$ 有关,方差是个超参数(常量,只和 $\alpha_i$ 有关)。
经过计算得到:
$$ q(x_{t-1} | x_t, x_0) = \mathcal{N}\left( x_{t-1}; \tilde{\mu}_t(x_t, x_0), \tilde{\beta}_t I \right) $$
其中均值:
$$ \tilde{\mu}_t(x_t, x_0) = \frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1 - \bar{\alpha}_t} x_0 + \frac{\sqrt{\alpha_t} (1 - \bar{\alpha}_{t-1})}{1 - \bar{\alpha}_t} x_t $$
方差:
$$ \tilde{\beta}_t = \frac{(1 - \bar{\alpha}_{t-1}) \beta_t}{1 - \bar{\alpha}_t}, \quad \beta_t = 1 - \alpha_t $$
这个 $q$ 就是训练时用于指导 $p_θ$ 学习的近似函数(通过 KL 最小化,简化为噪声预测损失)。