DDPM公式推导(五)

3 Diffusion models and denoising autoencoders

扩散模型可能看起来是一类受限制的潜在变量模型,但它们在实现中允许很大的自由度。必须选择正向过程的方差 β t \beta_t βt以及逆向过程的模型架构和高斯分布参数化。为了指导我们的选择,我们在扩散模型和去噪分数匹配之间建立了一个新的显式连接(第 3.2 节),从而为扩散模型提供了一个简化的加权变分边界目标(第 3.4 节)。最终,我们的模型设计通过简单性和实证结果得到了证明(第 4 节)。我们的讨论按公式(5)的术语进行分类。

3.1 Forward process and L T L_T LT


我们忽略了通过重参数化可以使前向过程的方差 β t \beta_t βt变得可学习的事实,而是将它们固定为常数(详见第4节)。因此,在我们的实现中,近似后验分布 q q q没有可学习的参数,因此 L T L_T LT在训练过程中是一个常数,可以忽略不计。

3.2 Reverse process and L 1 : T − 1 L_{1: T-1} L1:T1


现在我们讨论 p θ ( x t − 1 ∣ x t ) = N ( x t − 1 ; μ θ ( x t , t ) , Σ θ ( x t , t ) ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right), \mathbf{\Sigma}_\theta\left(\mathbf{x}_t, t\right)\right) pθ(xt1xt)=N(xt1;μθ(xt,t),Σθ(xt,t)) 中对 1 < t ≤ T 1 < t \leq T 1<tT 的选择。首先,我们将 Σ θ ( x t , t ) = σ t 2 I \boldsymbol{\Sigma}_\theta\left(\mathbf{x}_t, t\right)=\sigma_t^2 \mathbf{I} Σθ(xt,t)=σt2I 设为未训练的时间依赖常数。实验上, σ t 2 = β t \sigma_t^2=\beta_t σt2=βt σ t 2 = β ~ t = 1 − α ˉ t − 1 1 − α ˉ t β t \sigma_t^2=\tilde{\beta}_t=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t σt2=β~t=1αˉt1αˉt1βt 有类似的结果。第一个选择对于 x 0 ∼ N ( 0 , I ) \mathbf{x}_0 \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) x0N(0,I) 是最优的,而第二个选择对于 x 0 \mathbf{x}_0 x0 确定为某一个点是最优的。这是对应于坐标单位方差数据的逆过程熵上下界的两个极端选择 [53]。

其次,为了表示均值 μ θ ( x t , t ) \boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right) μθ(xt,t),我们提出了一种特定的参数化方法,这种方法的动机来源于对 L t L_t Lt 的以下分析。对于 p θ ( x t − 1 ∣ x t ) = N ( x t − 1 ; μ θ ( x t , t ) , σ t 2 I ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)=\mathcal{N}\left(\mathbf{x}_{t-1} ; \boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right), \sigma_t^2 \mathbf{I}\right) pθ(xt1xt)=N(xt1;μθ(xt,t),σt2I),我们可以写成:
L t − 1 = E q [ 1 2 σ t 2 ∥ μ ~ t ( x t , x 0 ) − μ θ ( x t , t ) ∥ 2 ] + C ( 8 ) L_{t-1}=\mathbb{E}_q\left[\frac{1}{2 \sigma_t^2}\left\|\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right)\right\|^2\right]+C \quad(8) Lt1=Eq[2σt21μ~t(xt,x0)μθ(xt,t)2]+C(8)
其中 C C C 是一个不依赖于 θ \theta θ 的常数。因此,我们看到 μ θ \boldsymbol{\mu}_\theta μθ 最直接的参数化方式是预测前向过程的后验均值 μ ~ t \tilde{\boldsymbol{\mu}}_t μ~t。但是,我们可以通过将公式 (4) 重参数化为 x t ( x 0 , ϵ ) = α ˉ t x 0 + 1 − α ˉ t ϵ \mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right)=\sqrt{\bar{\alpha}_t} \mathbf{x}_0+\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon} xt(x0,ϵ)=αˉt x0+1αˉt ϵ ϵ ∼ N ( 0 , I ) \boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) ϵN(0,I),并应用前向过程的后验公式 (7) 来进一步展开公式 (8):
L t − 1 − C = E x 0 , ϵ [ 1 2 σ t 2 ∥ μ ~ t ( x t ( x 0 , ϵ ) , 1 α ˉ t ( x t ( x 0 , ϵ ) − 1 − α ˉ t ϵ ) ) − μ θ ( x t ( x 0 , ϵ ) , t ) ∥ 2 ] ( 9 ) = E x 0 , ϵ [ 1 2 σ t 2 ∥ 1 α t ( x t ( x 0 , ϵ ) − β t 1 − α ˉ t ϵ ) − μ θ ( x t ( x 0 , ϵ ) , t ) ∥ 2 ] ( 10 ) \begin{aligned} L_{t-1}-C & =\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\left[\frac{1}{2 \sigma_t^2}\left\|\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right), \frac{1}{\sqrt{\bar{\alpha}_t}}\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right)-\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}\right)\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right), t\right)\right\|^2\right] \quad(9)\\ & =\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\left[\frac{1}{2 \sigma_t^2}\left\|\frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right)-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right), t\right)\right\|^2\right]\quad(10) \end{aligned} Lt1C=Ex0,ϵ[2σt21 μ~t(xt(x0,ϵ),αˉt 1(xt(x0,ϵ)1αˉt ϵ))μθ(xt(x0,ϵ),t) 2](9)=Ex0,ϵ[2σt21 αt 1(xt(x0,ϵ)1αˉt βtϵ)μθ(xt(x0,ϵ),t) 2](10)

公式(10)揭示了 μ θ \boldsymbol{\mu}_\theta μθ 必须预测 1 α t ( x t − β t 1 − α ˉ t ϵ ) \frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}\right) αt 1(xt1αˉt βtϵ) 给定 x t \mathbf{x}_t xt。由于 x t \mathbf{x}_t xt 作为模型的输入是可用的,我们可以选择参数化方式:
μ θ ( x t , t ) = μ ~ t ( x t , 1 α ˉ t ( x t − 1 − α ˉ t ϵ θ ( x t ) ) ) = 1 α t ( x t − β t 1 − α ˉ t ϵ θ ( x t , t ) ) ( 11 ) \boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right)=\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \frac{1}{\sqrt{\bar{\alpha}_t}}\left(\mathbf{x}_t-\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}_\theta\left(\mathbf{x}_t\right)\right)\right)=\frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta\left(\mathbf{x}_t, t\right)\right)\quad(11) μθ(xt,t)=μ~t(xt,αˉt 1(xt1αˉt ϵθ(xt)))=αt 1(xt1αˉt βtϵθ(xt,t))(11)
其中 ϵ θ \boldsymbol{\epsilon}_\theta ϵθ 是一个函数逼近器,用于从 x t \mathbf{x}_t xt 预测 ϵ \boldsymbol{\epsilon} ϵ。采样 x t − 1 ∼ p θ ( x t − 1 ∣ x t ) \mathbf{x}_{t-1} \sim p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right) xt1pθ(xt1xt) 相当于计算 x t − 1 = 1 α t ( x t − β t 1 − α ˉ t ϵ θ ( x t , t ) ) + σ t z \mathbf{x}_{t-1}=\frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta\left(\mathbf{x}_t, t\right)\right)+\sigma_t \mathbf{z} xt1=αt 1(xt1αˉt βtϵθ(xt,t))+σtz,其中 z ∼ N ( 0 , I ) \mathbf{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) zN(0,I)。完整的采样过程,算法2,类似于 Langevin 动力学,其中 ϵ θ \epsilon_\theta ϵθ 是数据密度的学习梯度。此外,使用参数化公式(11),公式(10)简化为:
E x 0 , ϵ [ β t 2 2 σ t 2 α t ( 1 − α ˉ t ) ∥ ϵ − ϵ θ ( α ˉ t x 0 + 1 − α ˉ t ϵ , t ) ∥ 2 ] ( 12 ) \mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\left[\frac{\beta_t^2}{2 \sigma_t^2 \alpha_t\left(1-\bar{\alpha}_t\right)}\left\|\boldsymbol{\epsilon}-\boldsymbol{\epsilon}_\theta\left(\sqrt{\bar{\alpha}_t} \mathbf{x}_0+\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}, t\right)\right\|^2\right]\quad(12) Ex0,ϵ[2σt2αt(1αˉt)βt2 ϵϵθ(αˉt x0+1αˉt ϵ,t) 2](12)
这类似于由 t t t 索引的多个噪声尺度上的去噪得分匹配 [55]。由于公式(12)等于 Langevin-like 逆过程(11)的变分界(一个项),我们看到优化类似于去噪得分匹配的目标等价于使用变分推断来拟合类似于 Langevin 动力学的采样链的有限时间边际。

总之,我们可以训练逆过程均值函数逼近器 μ θ \boldsymbol{\mu}_\theta μθ 来预测 μ ~ t \tilde{\boldsymbol{\mu}}_t μ~t,或者通过修改其参数化方式,我们可以训练它来预测 ϵ \epsilon ϵ。(还有预测 x 0 \mathbf{x}_0 x0 的可能性,但我们发现这会导致实验早期的样本质量较差。)我们已经证明了 ϵ \boldsymbol{\epsilon} ϵ-预测参数化方式既类似于 Langevin 动力学,又将扩散模型的变分界简化为类似于去噪得分匹配的目标。然而,这只是 p θ ( x t − 1 ∣ x t ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right) pθ(xt1xt) 的另一种参数化方式,因此在第4节中,我们通过比较预测 ϵ \boldsymbol{\epsilon} ϵ 和预测 μ ~ t \tilde{\boldsymbol{\mu}}_t μ~t 来验证其有效性。

公式解读: x t − 1 = 1 α t ( x t − β t 1 − α ˉ t ϵ θ ( x t , t ) ) + σ t z \mathbf{x}_{t-1}=\frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta\left(\mathbf{x}_t, t\right)\right)+\sigma_t \mathbf{z} xt1=αt 1(xt1αˉt βtϵθ(xt,t))+σtz
好不容易消掉了噪声,为什么又加回来?
直观理解,大致上是因为这步去噪去得过度干净了,而下一步的反向过程的输入,事实上仍应该是带一定量噪声的图片,所以反而要补上一些噪声。训练的时候,所有的 x t , x t − 1 \mathbf{x}_t, \mathbf{x}_{t-1} xt,xt1都是由真实图片加了特定强度的噪声得到的,而预测的时候, x t \mathbf{x}_t xt 却是由 x T , x T − 1 , … , x t + 1 \mathbf{x}_T, \mathbf{x}_{T-1}, \ldots, \mathbf{x}_{t+1} xT,xT1,,xt+1 一步步预测来的,所以要重新模拟回噪声强度,模拟训练场景。
这个噪声其实客观上也增加了图像生成过程中结果的多样性。
这个形式类似于损失函数的惩罚项,相当于正则参数化噪声,使其不要过度去噪。

DDPM本质上在学习一个噪声空间到图像空间的映射(通过多步迭代来实现)。
DDPM利用已知的噪声分布,通过扩散过程逐步逼近未知的真实数据分布。
为什么是高斯分布?最小化 MSE 损失函数可以被解释为在假设数据的条件概率模型为高斯分布的情况下,对数据的最大似然估计(MLE)。

总结

尽管前向加噪过程是线性的,但反向去噪过程面临数据分布复杂性、信息丢失和恢复、以及模型表达能力的挑战。理论上可以假设反向过程是线性的,但在实际应用中,这种假设会导致生成图像的质量和多样性下降。因此,逐步迭代的非线性模型(如DDPM)在计算上更复杂,但它们能够更好地捕捉数据的复杂结构,从而生成高质量和多样性的图像。

总结

虽然假设反向过程符合线性高斯分布可以在理论上简化生成过程,但在实际应用中,这种假设会带来许多问题和挑战。线性高斯模型的表达能力有限,难以捕捉图像数据的复杂非线性结构,导致生成图像的质量和多样性下降。此外,训练过程可能会变得不稳定。因此,尽管逐步迭代的非线性模型(如DDPM)在计算上更复杂,但它们能够更好地捕捉数据的复杂结构,从而生成高质量和多样性的图像。
算法

3.3 Data scaling, reverse process decoder, and L 0 L_0 L0


我们假设图像数据由整数 { 0 , 1 , … , 255 } \{0,1, \ldots, 255\} {0,1,,255} 线性缩放到 [ − 1 , 1 ] [-1,1] [1,1]。这样确保神经网络逆过程在一致缩放的输入上操作,从标准正态先验 p ( x T ) p\left(\mathbf{x}_T\right) p(xT) 开始。为了获得离散的对数似然,我们将逆过程的最后一项设置为从高斯分布 N ( x 0 ; μ θ ( x 1 , 1 ) , σ 1 2 I ) \mathcal{N}\left(\mathbf{x}_0 ; \boldsymbol{\mu}_\theta\left(\mathbf{x}_1, 1\right), \sigma_1^2 \mathbf{I}\right) N(x0;μθ(x1,1),σ12I) 导出的独立离散解码器:
p θ ( x 0 ∣ x 1 ) = ∏ i = 1 D ∫ δ − ( x 0 i ) δ + ( x 0 i ) N ( x ; μ θ i ( x 1 , 1 ) , σ 1 2 ) d x δ + ( x ) = { ∞  if  x = 1 x + 1 255  if  x < 1 δ − ( x ) = { − ∞  if  x = − 1 x − 1 255  if  x > − 1 ( 13 ) \begin{aligned} p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right) & =\prod_{i=1}^D \int_{\delta_{-}\left(x_0^i\right)}^{\delta_{+}\left(x_0^i\right)} \mathcal{N}\left(x ; \mu_\theta^i\left(\mathbf{x}_1, 1\right), \sigma_1^2\right) d x \\ \delta_{+}(x) & =\left\{\begin{array}{ll} \infty & \text { if } x=1 \\ x+\frac{1}{255} & \text { if } x<1 \end{array} \quad \delta_{-}(x)= \begin{cases}-\infty & \text { if } x=-1 \\ x-\frac{1}{255} & \text { if } x>-1\end{cases} \right.\quad(13) \end{aligned} pθ(x0x1)δ+(x)=i=1Dδ(x0i)δ+(x0i)N(x;μθi(x1,1),σ12)dx={x+2551 if x=1 if x<1δ(x)={x2551 if x=1 if x>1(13)
其中 D D D 是数据的维度,上标 i i i 表示提取一个坐标。(我们也可以简单地使用更强大的解码器,如条件自回归模型,但我们将这留给未来的工作。)与 VAE 解码器和自回归模型中使用的离散连续分布类似 [ 34 , 52 ] [34,52] [34,52],我们在这里的选择确保变分界是离散数据的无损编码长度,无需向数据添加噪声或将缩放操作的雅可比矩阵合并到对数似然中。在采样结束时,我们无噪声地显示 μ θ ( x 1 , 1 ) \boldsymbol{\mu}_\theta\left(\mathbf{x}_1, 1\right) μθ(x1,1)

这段话解释了如何通过扩散模型的逆过程从高斯分布生成离散图像数据,确保数值稳定性并实现无损编码。

3.4 Simplified training objective


通过上述定义的逆过程和解码器,由公式(12)和(13)导出的变分界对 θ \theta θ 是明显可微的,并且准备好用于训练。然而,我们发现对训练样本质量(和更简单的实现)有益的是对以下变分界的变体进行训练:
L simple  ( θ ) : = E t , x 0 , ϵ [ ∥ ϵ − ϵ θ ( α ˉ t x 0 + 1 − α ˉ t ϵ , t ) ∥ 2 ] ( 14 ) L_{\text {simple }}(\theta):=\mathbb{E}_{t, \mathbf{x}_0, \boldsymbol{\epsilon}}\left[\left\|\boldsymbol{\epsilon}-\boldsymbol{\epsilon}_\theta\left(\sqrt{\bar{\alpha}_t} \mathbf{x}_0+\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}, t\right)\right\|^2\right]\quad(14) Lsimple (θ):=Et,x0,ϵ[ ϵϵθ(αˉt x0+1αˉt ϵ,t) 2](14)
其中 t t t 在 1 和 T T T 之间均匀分布。 t = 1 t=1 t=1 的情况对应于 L 0 L_0 L0,在离散解码器定义(13)中,积分由高斯概率密度函数乘以箱宽近似,忽略了 σ 1 2 \sigma_1^2 σ12 和边缘效应。 t > 1 t>1 t>1 的情况对应于方程(12)的未加权版本,类似于 NCSN 去噪评分匹配模型使用的损失加权。 ( L T L_T LT 不出现,因为前向过程方差 β t \beta_t βt 是固定的。)算法 1 显示了使用此简化目标的完整训练过程。

由于我们的简化目标(14)丢弃了公式(12)中的加权,它是一种加权变分界,与标准变分界相比,强调重建的不同方面。特别是,我们在第 4 节中设置的扩散过程导致简化目标降低了与小 t t t 对应的损失项权重。这些项训练网络去除非常小量的噪声数据,因此将它们降权是有益的,这样网络就可以将重点放在更大 t t t 项的更困难的去噪任务上。我们将在我们的实验中看到,这种重新加权导致更好的样本质量。

由上篇文章DDPM公式推导(四)推出了我们的最终优化目标的解析式:
L t − 1 = E q [ 1 2 σ t 2 ∥ μ ~ t − μ θ ∥ 2 ] + C \begin{aligned} L_{t-1}=\mathbb{E}_q\left[\frac{1}{2 \sigma_t^2}\left\|\tilde{\boldsymbol{\mu}}_t-\boldsymbol{\mu}_\theta\right\|^2\right]+C \end{aligned} Lt1=Eq[2σt21μ~tμθ2]+C
由此可以推出,比较的实际上是两个分布(估计分布和已知分布)的均值。其中:
μ ~ t = μ ~ t ( x t , x 0 ) = α ˉ t − 1 β t 1 − α ˉ t x 0 + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t x t \tilde{\boldsymbol{\mu}}_t=\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right)=\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_0+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t} \mathbf{x}_t μ~t=μ~t(xt,x0)=1αˉtαˉt1 βtx0+1αˉtαt (1αˉt1)xt
其中 x 0 , x t \mathbf{x}_0, \mathbf{x}_t x0,xt 在正向过程中已知,且能相互转化,而逆向过程的均值 μ θ ( x t , t ) \mathbf{\mu}_\theta\left(\mathbf{x}_t, t\right) μθ(xt,t) 是关于 x t \mathbf{x}_t xt 的函数。最一般的建模方式是让神经网络直接根据 ( x t , t ) \left(\mathbf{x}_t, t\right) (xt,t) 来学对应的真值 μ q \boldsymbol{\mu}_q μq ,这样显然有点复杂。原因有二,一个是数值稳定性:由于直接学习均值涉及到对潜在变量 x t \mathbf{x}_t xt 的准确估计,任何微小的误差都会被放大,尤其是在模型初始阶段,导致数值不稳定性。方差 σ t 2 \sigma_t^2 σt2 在不同时间步上可能有很大的变化,直接对均值进行优化可能导致梯度爆炸或消失的问题。一个是优化效率:直接学习均值需要精确预测每个时间步的去噪均值, 这对模型参数的要求非常高,可能导致训练过程非常慢,难以收敛。如果 μ q \boldsymbol{\mu}_q μq μ θ \boldsymbol{\mu}_\theta μθ 形式很相近的话,就会只需要对不同的地方做差,这样能大大简化优化过程。一个很自然而然的想法就是让神经网络去学真值 x 0 \mathbf{x}_0 x0 ,也就是 x θ ( x t , t ) \mathbf{x}_{\theta}\left(\mathbf{x}_t, t\right) xθ(xt,t) 。因此,我们建模成: μ θ = μ θ ( x t , t ) = α ˉ t − 1 β t 1 − α ˉ t x θ + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t x t \boldsymbol{\mu}_{\boldsymbol{\theta}}=\mathbf{\mu}_\theta\left(\mathbf{x}_t, t\right)=\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_{ \theta}+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t} \mathbf{x}_t μθ=μθ(xt,t)=1αˉtαˉt1 βtxθ+1αˉtαt (1αˉt1)xt
式(8)可以写为:
L t − 1 − C = E q [ 1 2 σ t 2 ∥ μ ~ t ( x t , x 0 ) − μ θ ( x t , t ) ∥ 2 ] = E q [ 1 2 σ t 2 ∥ α ˉ t − 1 β t 1 − α ˉ t x 0 − α ˉ t − 1 β t 1 − α ˉ t x θ ∥ 2 ] = E q [ 1 2 σ t 2 ∥ α ˉ t − 1 β t 1 − α ˉ t ( x 0 − x θ ) ∥ 2 ] = E q [ 1 2 σ t α ˉ t − 1 β t 2 ( 1 − α ˉ t ) 2 ∥ ( x 0 − x θ ) ∥ 2 ] \begin{aligned} L_{t-1}-C & =\mathbb{E}_{q}\left[\frac{1}{2 \sigma_t^2}\left\|\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right)\right\|^2\right] \\ & =\mathbb{E}_{q}\left[\frac{1}{2 \sigma_t^2}\left\|\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_0-\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_{ \theta}\right\|^2\right]\\ \\&=\mathbb{E}_{q}\left[\frac{1}{2 \sigma_t^2}\left\|\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t}\left(\mathbf{x}_0-\mathbf{x}_{ \theta}\right)\right\|^2\right] \\ &=\mathbb{E}_{q}\left[\frac{1}{2 \sigma_t} \frac{\bar{\alpha}_{t-1} \beta_t^2}{\left(1-\bar{\alpha}_t\right)^2}\left\|\left(\mathbf{x}_0-\mathbf{x}_{ \theta}\right)\right\|^2\right] \end{aligned} Lt1C=Eq[2σt21μ~t(xt,x0)μθ(xt,t)2]=Eq[2σt21 1αˉtαˉt1 βtx01αˉtαˉt1 βtxθ 2]=Eq[2σt21 1αˉtαˉt1 βt(x0xθ) 2]=Eq[2σt1(1αˉt)2αˉt1βt2(x0xθ)2]
训练神经网络,可以使其根据损坏的图像 x t \mathbf{x}_t xt预测输入 x 0 \mathbf{x}_0 x0
扩散模型中的噪声通常服从已知分布(如标准正态分布),预测噪声比预测图像本身 ( x 0 ) \left(\mathbf{x}_0\right) (x0)更加直接和稳定。模型可以更容易地学到如何将噪声从数据中去除,这通常比直接重建图像更简单。因此,引入重参数化来来重写式(8)。借助式(4)进行重参数化: μ ~ t = μ ~ t ( x t , ϵ ) = α ˉ t − 1 β t 1 − α ˉ t x 0 + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t x t = α ˉ t − 1 β t 1 − α ˉ t x t − 1 − α ˉ t ϵ α ˉ t + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t x t = [ α ˉ t − 1 β t ( 1 − α ˉ t ) α t + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t ] x t − 1 − α t 1 − α ˉ t α t ϵ = [ β t ( 1 − α ˉ t ) α t + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t ] x t − 1 − α t 1 − α ˉ t α t ϵ = ( 1 − α t ) + α t ( 1 − α ˉ t − 1 ) ( 1 − α ˉ t ) α t x t − 1 − α t 1 − α ˉ t α t ϵ = 1 − α t + α t − α ˉ t ( 1 − α ˉ t ) α t x t − 1 − α t 1 − α ˉ t α t ϵ = 1 α t x t − 1 − α t 1 − α ˉ t α t ϵ \begin{aligned} \tilde{\boldsymbol{\mu}}_t=\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \boldsymbol{\epsilon} \right) & =\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_0+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t} \mathbf{x}_t \\ & =\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \frac{\mathbf{x}_t-\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}}{\sqrt{\bar{\alpha}_t}}+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t} \mathbf{x}_t \\ & =\left[\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{\left(1-\bar{\alpha}_t\right) \sqrt{\alpha_t}}+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t}\right] \mathbf{x}_t-\frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t} \sqrt{\alpha_t}} \boldsymbol{\epsilon} \\ & =\left[\frac{\beta_t}{\left(1-\bar{\alpha}_t\right) \sqrt{\alpha_t}}+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t}\right] \mathbf{x}_t-\frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t} \sqrt{\alpha_t}} \boldsymbol{\epsilon} \\ & =\frac{\left(1-\alpha_t\right)+\alpha_t\left(1-\bar{\alpha}_{t-1}\right)}{\left(1-\bar{\alpha}_t\right) \sqrt{\alpha_t}} \mathbf{x}_t-\frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t} \sqrt{\alpha_t}} \boldsymbol{\epsilon} \\ & =\frac{1-\alpha_t+\alpha_t-\bar{\alpha}_t}{\left(1-\bar{\alpha}_t\right) \sqrt{\alpha_t}} \mathbf{x}_t-\frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t} \sqrt{\alpha_t}} \boldsymbol{\epsilon} \\ & =\frac{1}{\sqrt{\alpha_t}} \mathbf{x}_t-\frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t} \sqrt{\alpha_t}} \boldsymbol{\epsilon} \end{aligned} μ~t=μ~t(xt,ϵ)=1αˉtαˉt1 βtx0+1αˉtαt (1αˉt1)xt=1αˉtαˉt1 βtαˉt xt1αˉt ϵ+1αˉtαt (1αˉt1)xt=[(1αˉt)αt αˉt1 βt+1αˉtαt (1αˉt1)]xt1αˉt αt 1αtϵ=[(1αˉt)αt βt+1αˉtαt (1αˉt1)]xt1αˉt αt 1αtϵ=(1αˉt)αt (1αt)+αt(1αˉt1)xt1αˉt αt 1αtϵ=(1αˉt)αt 1αt+αtαˉtxt1αˉt αt 1αtϵ=αt 1xt1αˉt αt 1αtϵ
所以,我们建模成: μ θ = μ θ ( x t , t ) = 1 α t x t − 1 − α t 1 − α ˉ t α t ϵ θ ( x t , t ) 1 α t ( x t − β t 1 − α ˉ t ϵ θ ( x t , t ) ) \begin{aligned} \boldsymbol{\mu}_{\boldsymbol{\theta}}=\mathbf{\mu}_\theta\left(\mathbf{x}_t, t\right)=\frac{1}{\sqrt{\alpha_t}} \mathbf{x}_t-\frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t} \sqrt{\alpha_t}} \boldsymbol{\epsilon}_{\theta}\left(\mathbf{x}_t,t\right)\\ \frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta\left(\mathbf{x}_t, t\right)\right) \end{aligned} μθ=μθ(xt,t)=αt 1xt1αˉt αt 1αtϵθ(xt,t)αt 1(xt1αˉt βtϵθ(xt,t))
即式(11),然后重写式(8)可以得到:
L t − 1 − C = E q [ 1 2 σ t 2 ∥ μ ~ t ( x t , x 0 ) − μ θ ( x t , t ) ∥ 2 ] = E x 0 , ϵ [ 1 2 σ t 2 ∥ μ ~ t ( x t ( x 0 , ϵ ) , 1 α ˉ t ( x t ( x 0 , ϵ ) − 1 − α ˉ t ϵ ) ) − μ θ ( x t ( x 0 , ϵ ) , t ) ∥ 2 ] = E x 0 , ϵ [ 1 2 σ t 2 ∥ 1 α t ( x t ( x 0 , ϵ ) − β t 1 − α ˉ t ϵ ) − μ θ ( x t ( x 0 , ϵ ) , t ) ∥ 2 ] = E x 0 , ϵ [ β t 2 2 σ t 2 α t ( 1 − α ˉ t ) ∥ ϵ − ϵ θ ( α ˉ t x 0 + 1 − α ˉ t ϵ , t ) ∥ 2 ] \begin{aligned} L_{t-1}-C & =\mathbb{E}_{q}\left[\frac{1}{2 \sigma_t^2}\left\|\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t, t\right)\right\|^2\right] \\ & =\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\left[\frac{1}{2 \sigma_t^2}\left\|\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right), \frac{1}{\sqrt{\bar{\alpha}_t}}\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right)-\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}\right)\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right), t\right)\right\|^2\right] \\&=\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\left[\frac{1}{2 \sigma_t^2}\left\|\frac{1}{\sqrt{\alpha_t}}\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right)-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \boldsymbol{\epsilon}\right)-\boldsymbol{\mu}_\theta\left(\mathbf{x}_t\left(\mathbf{x}_0, \boldsymbol{\epsilon}\right), t\right)\right\|^2\right]\\ &=\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\left[\frac{\beta_t^2}{2 \sigma_t^2 \alpha_t\left(1-\bar{\alpha}_t\right)}\left\|\boldsymbol{\epsilon}-\boldsymbol{\epsilon}_\theta\left(\sqrt{\bar{\alpha}_t} \mathbf{x}_0+\sqrt{1-\bar{\alpha}_t} \boldsymbol{\epsilon}, t\right)\right\|^2\right] \end{aligned} Lt1C=Eq[2σt21μ~t(xt,x0)μθ(xt,t)2]=Ex0,ϵ[2σt21 μ~t(xt(x0,ϵ),αˉt 1(xt(x0,ϵ)1αˉt ϵ))μθ(xt(x0,ϵ),t) 2]=Ex0,ϵ[2σt21 αt 1(xt(x0,ϵ)1αˉt βtϵ)μθ(xt(x0,ϵ),t) 2]=Ex0,ϵ[2σt2αt(1αˉt)βt2 ϵϵθ(αˉt x0+1αˉt ϵ,t) 2]
训练神经网络,使其能够根据损坏的图像 x t \mathbf{x}_t xt 预测噪声(即根据已知的时间表 β t \beta_t βt x 0 \mathbf{x}_0 x0 上应用噪声)。预测的就是这个应用噪声
就这样,我们得到了式(12)。

### DDPM去噪扩散概率模型公式推导 #### 正向扩散过程定义 在DDPM中,正向扩散过程可以表示为一系列马尔可夫链的状态转移。假设初始数据$x_0$来自真实的数据分布$q(x)$,那么经过T步的加噪处理后的状态$x_T$近似于纯噪声。 每一步的转换遵循下述方程: $$ q(x_t|x_{t-1}) = \mathcal{N}(x_t; \sqrt{1-\beta_t}x_{t-1},\beta_t I) $$ 其中$\beta_t$是一个预设的小常数序列[^2]。 #### 反向生成过程建模 为了实现从噪声到原始数据样本的转变,需要构建一个逆过程$p_\theta(x_{t-1}|x_t)$,该过程由神经网络参数化并优化以最小化与前向过程之间的差异。理想情况下,这一过程应该能够逆转所有的加噪操作: $$ p_\theta(x_{t-1}|x_t)=\mathcal{N}\left(\tilde{\mu}_\theta(x_t,t),\Sigma_\theta(t)\right) $$ 这里$\tilde{\mu}_\theta(x_t,t)$代表预测均值函数而$\Sigma_\theta(t)$则是协方差矩阵,二者皆依赖时间戳$t$以及当前含噪观测$x_t$并通过训练得到最优解[^4]。 #### 训练目标设定 整个框架的目标是最小化变分界下的负对数似然估计(Variational Lower Bound),即KL散度形式表达如下: $$ L_{VLB}=L_0+\sum^{T}_{t=1}L_t=\mathrm{E}_{q(x_0)}[\log p_\theta(x_0)] + \sum^T_{t=1}\mathrm{E}_{q(x_{0:t})}[D_{KL}(q(x_{t-1}|x_t,x_0)||p_\theta(x_{t-1}|x_t))] $$ 简化版损失函数$L_{simple}$通常用于实际计算当中,它忽略了部分项并且更容易求梯度下降更新规则: $$ L_{simple}=\mathrm{E}_{q(x_0),\epsilon\sim\mathcal{N}(0,I)}[(\frac{1}{\alpha_t}\epsilon - \epsilon_\theta(\sqrt{\bar{\alpha}}x_0+\sqrt{(1-\bar{\alpha})I}, t))^2 ] $$ 此处$\alpha_t,\bar{\alpha}$分别对应着特定时刻的标准偏差和平滑累积版本;$\epsilon_\theta()$指代用来拟合残差误差的学习模块。 ```python import torch.nn as nn class SimpleUNet(nn.Module): def __init__(self, input_channels, output_channels): super(SimpleUNet, self).__init__() # 定义U-net结构... def loss_function(model_output, target_noise, alpha_bar_sqrt, one_minus_alpha_bar_sqrt): predicted_epsilon = model_output / alpha_bar_sqrt - \ (target_noise * one_minus_alpha_bar_sqrt) return ((predicted_epsilon - target_noise)**2).mean() ``` 上述代码片段展示了如何基于给定的时间步长和输入特征来设计简单的U-net架构,并定义相应的损失函数来进行模型训练。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾道长生'

您的支持是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值