扩散模型(二)
相关阅读:扩散模型(一)
Parameterization of L t L_t Lt for Training Loss
回想一下,我们需要训练一个神经网络来近似反向扩散过程中的条件概率分布,即, p θ ( x t − 1 ∣ x t ) = N ( x t − 1 ; μ θ ( x t , t ) , Σ θ ( x t , t ) ) p_\theta(\mathbf{x}_{t-1} \vert \mathbf{x}_t) = \mathcal{N}(\mathbf{x}_{t-1}; \boldsymbol{\mu}_\theta(\mathbf{x}_t, t), \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t)) pθ(xt−1∣xt)=N(xt−1;μθ(xt,t),Σθ(xt,t)). 我们希望训练 μ θ \boldsymbol{\mu}_\theta μθ 以预测 μ ~ t = 1 α t ( x t − 1 − α t 1 − α ˉ t ϵ t ) \tilde{\boldsymbol{\mu}}_t = \frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_t \Big) μ~t=αt1(xt−1−αˉt1−αtϵt). 由于训练时 x t \mathbf{x}_t xt 可以获取, 我们可以对高斯噪声项进行重参数化, 使其在时间步 t t t 从输入 x t \mathbf{x}_t xt 预测出 ϵ t \boldsymbol{\epsilon}_t ϵt。
μ θ ( x t , t ) = 1 α t ( x t − 1 − α t 1 − α ˉ t ϵ θ ( x t , t ) ) Thus x t − 1 = N ( x t − 1 ; 1 α t ( x t − 1 − α t 1 − α ˉ t ϵ θ ( x t , t ) ) , Σ θ ( x t , t ) ) \begin{aligned} \boldsymbol{\mu}_\theta(\mathbf{x}_t, t) &= \color{blue}{\frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \Big)} \\ \text{Thus } \mathbf{x}_{t-1} &= \mathcal{N}(\mathbf{x}_{t-1}; \frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \Big), \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t)) \end{aligned} μθ(xt,t)Thus xt−1=αt1(xt−1−αˉt1−αtϵθ(xt,t))=N(xt−1;αt1(xt−1−αˉt1−αtϵθ(xt,t)),Σθ(xt,t))
损失项 L t L_t Lt 通过参数化来最小化与 μ ~ \tilde{\boldsymbol{\mu}} μ~ 的差异:
L t = E x 0 , ϵ [ 1 2 ∥ Σ θ ( x t , t ) ∥ 2 2 ∥ μ ~ t ( x t , x 0 ) − μ θ ( x t , t ) ∥ 2 ] = E x 0 , ϵ [ 1 2 ∥ Σ θ ∥ 2 2 ∥ 1 α t ( x t − 1 − α t 1 − α ˉ t ϵ t ) − 1 α t ( x t − 1 − α t 1 − α ˉ t ϵ θ ( x t , t ) ) ∥ 2 ] = E x 0 , ϵ [ ( 1 − α t ) 2 2 α t ( 1 − α ˉ t ) ∥ Σ θ ∥ 2 2 ∥ ϵ t − ϵ θ ( x t , t ) ∥ 2 ] = E x 0 , ϵ [ ( 1 − α t ) 2 2 α t ( 1 − α ˉ t ) ∥ Σ θ ∥ 2 2 ∥ ϵ t − ϵ θ ( α ˉ t x 0 + 1 − α ˉ t ϵ t , t ) ∥ 2 ] \begin{aligned} L_t &= \mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}} \Big[\frac{1}{2 \| \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t) \|^2_2} \| \color{blue}{\tilde{\boldsymbol{\mu}}_t(\mathbf{x}_t, \mathbf{x}_0)} - \color{green}{\boldsymbol{\mu}_\theta(\mathbf{x}_t, t)} \|^2 \Big] \\ &= \mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}} \Big[\frac{1}{2 \|\boldsymbol{\Sigma}_\theta \|^2_2} \| \color{blue}{\frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_t \Big)} - \color{green}{\frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\boldsymbol{\epsilon}}_\theta(\mathbf{x}_t, t) \Big)} \|^2 \Big] \\ &= \mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}} \Big[\frac{ (1 - \alpha_t)^2 }{2 \alpha_t (1 - \bar{\alpha}_t) \| \boldsymbol{\Sigma}_\theta \|^2_2} \|\boldsymbol{\epsilon}_t - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\|^2 \Big] \\ &= \mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}} \Big[\frac{ (1 - \alpha_t)^2 }{2 \alpha_t (1 - \bar{\alpha}_t) \| \boldsymbol{\Sigma}_\theta \|^2_2} \|\boldsymbol{\epsilon}_t - \boldsymbol{\epsilon}_\theta(\sqrt{\bar{\alpha}_t}\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t}\boldsymbol{\epsilon}_t, t)\|^2 \Big] \end{aligned} Lt=Ex0,ϵ[2∥Σθ(xt,t)∥221∥μ~t(xt,x0)−μθ(xt,t)∥2]=Ex0,ϵ[2∥Σθ∥221∥αt1(xt−1−αˉt1−αtϵt)−αt1(xt−1−αˉt1−αtϵθ(xt,t))∥2]=Ex0,ϵ[2αt(1−αˉt)∥Σθ∥22(1−αt)2∥ϵt−ϵθ(xt,t)∥2]=Ex0,ϵ[2αt(1−αˉt)∥Σθ∥22(1−αt)2∥ϵt−ϵθ(αˉtx0+1−αˉtϵt,t)∥2]
- Simplification
根据实证研究,霍等人(2020 年)发现,使用一个简化的目标函数来训练扩散模型效果更好,该目标函数忽略了加权项。
L t simple = E t ∼ [ 1 , T ] , x 0 , ϵ t [ ∥ ϵ t − ϵ θ ( x t , t ) ∥ 2 ] = E t ∼ [ 1 , T ] , x 0 , ϵ t [ ∥ ϵ t − ϵ θ ( α ˉ t x 0 + 1 − α ˉ t ϵ t , t ) ∥ 2 ] \begin{aligned} L_t^\text{simple} &= \mathbb{E}_{t \sim [1, T], \mathbf{x}_0, \boldsymbol{\epsilon}_t} \Big[\|\boldsymbol{\epsilon}_t - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)\|^2 \Big] \\ &= \mathbb{E}_{t \sim [1, T], \mathbf{x}_0, \boldsymbol{\epsilon}_t} \Big[\|\boldsymbol{\epsilon}_t - \boldsymbol{\epsilon}_\theta(\sqrt{\bar{\alpha}_t}\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t}\boldsymbol{\epsilon}_t, t)\|^2 \Big] \end{aligned} Ltsimple=Et∼[1,T],x0,ϵt[∥ϵt−ϵθ(xt,t)∥2]=Et∼[1,T],x0,ϵt[∥ϵt−ϵθ(αˉtx0+1−αˉtϵt,t)∥2]
最终简化的目标为:
L simple = L t simple + C L_\text{simple} = L_t^\text{simple} + C Lsimple=Ltsimple+C
其中 C C C 是常数,不依赖于 θ \theta θ。
- 图 4
Connection with noise-conditioned score networks (NCSN)
宋和埃尔蒙(2019)提出了一种基于分数的生成建模方法,通过利用分数匹配估计的数据分布梯度,经由朗之万动力学(见 “与随机梯度朗之万动力学的联系”)生成样本。每个样本
x
\mathbf{x}
x 的密度概率的分数定义为其梯度
∇
x
log
q
(
x
)
\nabla_{\mathbf{x}} \log q(\mathbf{x})
∇xlogq(x) 。训练一个分数网络
s
θ
:
R
D
→
R
D
\mathbf{s}_\theta: \mathbb{R}^D \to \mathbb{R}^D
sθ:RD→RD 来进行估计,即
s
θ
(
x
)
≈
∇
x
log
q
(
x
)
\mathbf{s}_\theta(\mathbf{x}) \approx \nabla_{\mathbf{x}} \log q(\mathbf{x})
sθ(x)≈∇xlogq(x) 。
为了使其在深度学习环境中适用于高维数据,他们提出使用 “去噪分数匹配” 或 “切片分数匹配”。去噪分数匹配向数据
q
(
x
~
∣
x
)
q(\tilde{\mathbf{x}} \vert \mathbf{x})
q(x~∣x) 添加预先指定的少量噪声,并通过分数匹配估计
q
(
x
~
)
q(\tilde{\mathbf{x}})
q(x~)。回想一下,朗之万动力学可以在迭代过程中仅使用分数
∇
x
log
q
(
x
)
\nabla_{\mathbf{x}} \log q(\mathbf{x})
∇xlogq(x) 从概率密度分布中采样数据点。
然而,根据流形假设,尽管观测到的数据可能看起来维度极高,但大多数数据预计集中在一个低维流形中。这对分数估计产生了负面影响,因为数据点无法覆盖整个空间。在数据密度低的区域,分数估计的可靠性较差。在添加少量高斯噪声以使扰动后的数据分布覆盖整个
R
D
\mathbb{R}^D
RD 空间后,分数估计器网络的训练变得更加稳定。宋和埃尔蒙(2019)通过用不同程度的噪声扰动数据,并训练一个噪声条件分数网络来联合估计不同噪声水平下所有扰动数据的分数,从而对该方法进行了改进。
噪声水平增加的过程类似于正向扩散过程。如果我们使用扩散过程的标注,分数近似为
s
θ
(
x
t
,
t
)
≈
∇
x
t
log
q
(
x
t
)
\mathbf{s}_\theta(\mathbf{x}_t, t) \approx \nabla_{\mathbf{x}_t} \log q(\mathbf{x}_t)
sθ(xt,t)≈∇xtlogq(xt) 。对于高斯分布
x
∼
N
(
μ
,
σ
2
I
)
\mathbf{x} \sim \mathcal{N}(\mathbf{\mu}, \sigma^2 \mathbf{I})
x∼N(μ,σ2I) ,我们可以将其密度函数对数的导数写为
∇
x
log
p
(
x
)
=
∇
x
(
−
1
2
σ
2
(
x
−
μ
)
2
)
=
−
x
−
μ
σ
2
=
−
ϵ
σ
\nabla_{\mathbf{x}}\log p(\mathbf{x}) = \nabla_{\mathbf{x}} \Big(-\frac{1}{2\sigma^2}(\mathbf{x} - \boldsymbol{\mu})^2 \Big) = - \frac{\mathbf{x} - \boldsymbol{\mu}}{\sigma^2} = - \frac{\boldsymbol{\epsilon}}{\sigma}
∇xlogp(x)=∇x(−2σ21(x−μ)2)=−σ2x−μ=−σϵ ,其中
ϵ
∼
N
(
0
,
I
)
\boldsymbol{\epsilon} \sim \mathcal{N}(\boldsymbol{0}, \mathbf{I})
ϵ∼N(0,I) 。考虑到前文介绍的
q
(
x
t
∣
x
0
)
∼
N
(
α
ˉ
t
x
0
,
(
1
−
α
ˉ
t
)
I
)
q(\mathbf{x}_t \vert \mathbf{x}_0) \sim \mathcal{N}(\sqrt{\bar{\alpha}_t} \mathbf{x}_0, (1 - \bar{\alpha}_t)\mathbf{I})
q(xt∣x0)∼N(αˉtx0,(1−αˉt)I),因此,
s θ ( x t , t ) ≈ ∇ x t log q ( x t ) = E q ( x 0 ) [ ∇ x t log q ( x t ∣ x 0 ) ] = E q ( x 0 ) [ − ϵ θ ( x t , t ) 1 − α ˉ t ] = − ϵ θ ( x t , t ) 1 − α ˉ t \mathbf{s}_\theta(\mathbf{x}_t, t) \approx \nabla_{\mathbf{x}_t} \log q(\mathbf{x}_t) = \mathbb{E}_{q(\mathbf{x}_0)} [\nabla_{\mathbf{x}_t} \log q(\mathbf{x}_t \vert \mathbf{x}_0)] = \mathbb{E}_{q(\mathbf{x}_0)} \Big[ - \frac{\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)}{\sqrt{1 - \bar{\alpha}_t}} \Big] = - \frac{\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t)}{\sqrt{1 - \bar{\alpha}_t}} sθ(xt,t)≈∇xtlogq(xt)=Eq(x0)[∇xtlogq(xt∣x0)]=Eq(x0)[−1−αˉtϵθ(xt,t)]=−1−αˉtϵθ(xt,t)
Parameterization of β t \beta_t βt
在霍等人(2020)的研究中,正向方差被设定为一系列线性递增的常数,从
β
1
=
1
0
−
4
\beta_1=10^{-4}
β1=10−4 到
β
T
=
0.02
\beta_T=0.02
βT=0.02。与归一化后在
[
−
1
,
1
]
[-1, 1]
[−1,1] 之间的图像像素值相比,这些方差相对较小。他们实验中的扩散模型生成了高质量的样本,但与其他生成模型相比,仍无法实现具有竞争力的模型对数似然。
尼科尔和达里瓦尔(2021)提出了几种改进技术,以帮助扩散模型获得更低的负对数似然(NLL)。其中一项改进是使用基于余弦的方差调度。调度函数的选择可以是任意的,只要它在训练过程中间提供近乎线性的下降,并且在
t
=
0
t=0
t=0 和
t
=
T
t=T
t=T 附近变化细微。
β t = clip ( 1 − α ˉ t α ˉ t − 1 , 0.999 ) α ˉ t = f ( t ) f ( 0 ) where f ( t ) = cos ( t / T + s 1 + s ⋅ π 2 ) 2 \beta_t = \text{clip}(1-\frac{\bar{\alpha}_t}{\bar{\alpha}_{t-1}}, 0.999) \quad\bar{\alpha}_t = \frac{f(t)}{f(0)}\quad\text{where }f(t)=\cos\Big(\frac{t/T+s}{1+s}\cdot\frac{\pi}{2}\Big)^2 βt=clip(1−αˉt−1αˉt,0.999)αˉt=f(0)f(t)where f(t)=cos(1+st/T+s⋅2π)2
其中,小的偏移量 s s s 是为了防止在接近 t = 0 t=0 t=0 时 β t \beta_t βt 过小。
- 图 5
Parameterization of reverse process variance Σ θ \boldsymbol{\Sigma}_\theta Σθ
Ho et al. (2020) 选择将
β
t
\beta_t
βt 固定为常数,而非使其可学习,并设定
Σ
θ
(
x
t
,
t
)
=
σ
t
2
I
\boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t) = \sigma^2_t \mathbf{I}
Σθ(xt,t)=σt2I , 其中
σ
t
\sigma_t
σt 并非通过学习得到,而是设为
β
t
\beta_t
βt 或者
β
~
t
=
1
−
α
ˉ
t
−
1
1
−
α
ˉ
t
⋅
β
t
\tilde{\beta}_t = \frac{1 - \bar{\alpha}_{t-1}}{1 - \bar{\alpha}_t} \cdot \beta_t
β~t=1−αˉt1−αˉt−1⋅βt. 因为他们发现,学习对角方差
Σ
θ
\boldsymbol{\Sigma}_\theta
Σθ 会导致训练不稳定,且样本质量较差。
Nichol & Dhariwal (2021) 提出,通过模型预测一个混合向量
v
\mathbf{v}
v,将
Σ
θ
(
x
t
,
t
)
\boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t)
Σθ(xt,t) 作为
β
t
\beta_t
βt 与
β
~
t
\tilde{\beta}_t
β~t 之间的插值来进行学习。
Σ θ ( x t , t ) = exp ( v log β t + ( 1 − v ) log β ~ t ) \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t) = \exp(\mathbf{v} \log \beta_t + (1-\mathbf{v}) \log \tilde{\beta}_t) Σθ(xt,t)=exp(vlogβt+(1−v)logβ~t)
然而,简单目标函数 L simple L_\text{simple} Lsimple 并不依赖于 Σ θ \boldsymbol{\Sigma}_\theta Σθ。为了引入这种依赖关系,他们构建了一个混合目标函数 L hybrid = L simple + λ L VLB L_\text{hybrid} = L_\text{simple} + \lambda L_\text{VLB} Lhybrid=Lsimple+λLVLB,其中 λ = 0.001 \lambda=0.001 λ=0.001 取值较小,并且在 L VLB L_\text{VLB} LVLB 项中对 μ θ \boldsymbol{\mu}_\theta μθ 停止梯度计算,这样 L VLB L_\text{VLB} LVLB 就仅用于指导 Σ θ \boldsymbol{\Sigma}_\theta Σθ 的学习。从经验上看,他们发现 L VLB L_\text{VLB} LVLB 极难优化,这很可能是由于梯度噪声所致,因此他们提议使用一种带有重要性采样的 L VLB L_\text{VLB} LVLB 时间平均平滑版本。
Conditioned Generation
在使用诸如 ImageNet 数据集这类带有条件信息的图像来训练生成模型时,基于类别标签或一段描述性文本等条件生成样本是很常见的做法。
Classifier Guided Diffusion
为了将类别信息明确纳入扩散过程,达里瓦尔和尼科尔(2021)在含噪图像 x t \mathbf{x}_t xt 上训练了一个分类器 f ϕ ( y ∣ x t , t ) f_\phi(y \vert \mathbf{x}_t, t) fϕ(y∣xt,t),并通过改变噪声预测,使用梯度 ∇ x log f ϕ ( y ∣ x t ) \nabla_\mathbf{x} \log f_\phi(y \vert \mathbf{x}_t) ∇xlogfϕ(y∣xt) 将扩散采样过程导向到条件信息 y y y。
考虑之前的 ∇ x t log q ( x t ) = − 1 1 − α ˉ t ϵ θ ( x t , t ) \nabla_{\mathbf{x}_t} \log q(\mathbf{x}_t) = - \frac{1}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) ∇xtlogq(xt)=−1−αˉt1ϵθ(xt,t),我们可以将联合分布 q ( x t , y ) q(\mathbf{x}_t, y) q(xt,y) 的分数函数写成如下形式:
∇ x t log q ( x t , y ) = ∇ x t log q ( x t ) + ∇ x t log q ( y ∣ x t ) ≈ − 1 1 − α ˉ t ϵ θ ( x t , t ) + ∇ x t log f ϕ ( y ∣ x t ) = − 1 1 − α ˉ t ( ϵ θ ( x t , t ) − 1 − α ˉ t ∇ x t log f ϕ ( y ∣ x t ) ) \begin{aligned} \nabla_{\mathbf{x}_t} \log q(\mathbf{x}_t, y) &= \nabla_{\mathbf{x}_t} \log q(\mathbf{x}_t) + \nabla_{\mathbf{x}_t} \log q(y \vert \mathbf{x}_t) \\ &\approx - \frac{1}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) + \nabla_{\mathbf{x}_t} \log f_\phi(y \vert \mathbf{x}_t) \\ &= - \frac{1}{\sqrt{1 - \bar{\alpha}_t}} (\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) - \sqrt{1 - \bar{\alpha}_t} \nabla_{\mathbf{x}_t} \log f_\phi(y \vert \mathbf{x}_t)) \end{aligned} ∇xtlogq(xt,y)=∇xtlogq(xt)+∇xtlogq(y∣xt)≈−1−αˉt1ϵθ(xt,t)+∇xtlogfϕ(y∣xt)=−1−αˉt1(ϵθ(xt,t)−1−αˉt∇xtlogfϕ(y∣xt))
因此,一种新的由分类器引导的预测器 ϵ ˉ θ \bar{\boldsymbol{\epsilon}}_\theta ϵˉθ 将采用如下形式。
ϵ ˉ θ ( x t , t ) = ϵ θ ( x t , t ) − 1 − α ˉ t ∇ x t log f ϕ ( y ∣ x t ) \bar{\boldsymbol{\epsilon}}_\theta(\mathbf{x}_t, t) = \boldsymbol{\epsilon}_\theta(x_t, t) - \sqrt{1 - \bar{\alpha}_t} \nabla_{\mathbf{x}_t} \log f_\phi(y \vert \mathbf{x}_t) ϵˉθ(xt,t)=ϵθ(xt,t)−1−αˉt∇xtlogfϕ(y∣xt)
为了控制分类器引导的强度,我们可以在增量部分添加一个权重 w w w
ϵ ˉ θ ( x t , t ) = ϵ θ ( x t , t ) − 1 − α ˉ t w ∇ x t log f ϕ ( y ∣ x t ) \bar{\boldsymbol{\epsilon}}_\theta(\mathbf{x}_t, t) = \boldsymbol{\epsilon}_\theta(x_t, t) - \sqrt{1 - \bar{\alpha}_t} \; w \nabla_{\mathbf{x}_t} \log f_\phi(y \vert \mathbf{x}_t) ϵˉθ(xt,t)=ϵθ(xt,t)−1−αˉtw∇xtlogfϕ(y∣xt)
由此得到的简化扩散模型(ADM)以及添加了分类器引导的模型(ADM - G),能够取得比当时最先进的生成模型(如 BigGAN)更好的效果。
- 图 7
此外,通过对 U-Net 架构进行一些修改,达里瓦尔和尼科尔(2021)展示了扩散模型优于生成对抗网络(GAN)的性能。架构修改包括增加模型深度 / 宽度、增多注意力头、采用多分辨率注意力机制、在上下采样中使用 BigGAN 残差块、通过 对残差连接进行重新缩放 1 / 2 1/\sqrt{2} 1/2,以及使用自适应组归一化(AdaGN)。
Classifier-Free Guidance
即便没有独立的分类器 f ϕ f_{\phi} fϕ,通过整合来自条件扩散模型和无条件扩散模型的分数,依然可以执行条件扩散步骤(霍和萨利曼斯,2021)。设通过分数估计器 ϵ θ ( x t , t ) \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) ϵθ(xt,t) 参数化的无条件去噪扩散模型为 p θ ( x ) p_\theta(\mathbf{x}) pθ(x),通过 ϵ θ ( x t , t , y ) \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y) ϵθ(xt,t,y) 参数化的条件模型为 p θ ( x ∣ y ) p_\theta(\mathbf{x} \vert y) pθ(x∣y)。这两个模型可以由单个神经网络进行学习。确切地说,条件扩散模型 p θ ( x ∣ y ) p_\theta(\mathbf{x} \vert y) pθ(x∣y) 在成对数据上 ( x , y ) (\mathbf{x}, y) (x,y) 进行训练,其中条件信息 y y y 会被随机且周期性地丢弃,这样模型也能知晓如何进行无条件图像生成,即 ϵ θ ( x t , t ) = ϵ θ ( x t , t , y = ∅ ) \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) = \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y=\varnothing) ϵθ(xt,t)=ϵθ(xt,t,y=∅)。
隐式分类器的梯度可以用条件和无条件分数估计器来表示。一旦将其代入分类器引导的修正分数中,该分数就不再依赖于单独的分类器。
∇ x t log p ( y ∣ x t ) = ∇ x t log p ( x t ∣ y ) − ∇ x t log p ( x t ) = − 1 1 − α ˉ t ( ϵ θ ( x t , t , y ) − ϵ θ ( x t , t ) ) ϵ ˉ θ ( x t , t , y ) = ϵ θ ( x t , t , y ) − 1 − α ˉ t w ∇ x t log p ( y ∣ x t ) = ϵ θ ( x t , t , y ) + w ( ϵ θ ( x t , t , y ) − ϵ θ ( x t , t ) ) = ( w + 1 ) ϵ θ ( x t , t , y ) − w ϵ θ ( x t , t ) \begin{aligned} \nabla_{\mathbf{x}_t} \log p(y \vert \mathbf{x}_t) &= \nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t \vert y) - \nabla_{\mathbf{x}_t} \log p(\mathbf{x}_t) \\ &= - \frac{1}{\sqrt{1 - \bar{\alpha}_t}}\Big( \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y) - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \Big) \\ \bar{\boldsymbol{\epsilon}}_\theta(\mathbf{x}_t, t, y) &= \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y) - \sqrt{1 - \bar{\alpha}_t} \; w \nabla_{\mathbf{x}_t} \log p(y \vert \mathbf{x}_t) \\ &= \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y) + w \big(\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y) - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \big) \\ &= (w+1) \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, y) - w \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \end{aligned} ∇xtlogp(y∣xt)ϵˉθ(xt,t,y)=∇xtlogp(xt∣y)−∇xtlogp(xt)=−1−αˉt1(ϵθ(xt,t,y)−ϵθ(xt,t))=ϵθ(xt,t,y)−1−αˉtw∇xtlogp(y∣xt)=ϵθ(xt,t,y)+w(ϵθ(xt,t,y)−ϵθ(xt,t))=(w+1)ϵθ(xt,t,y)−wϵθ(xt,t)
他们的实验表明,无分类器引导能够在 FID(区分合成图像与生成图像的指标)和 IS(衡量图像质量与多样性的指标)之间取得良好平衡。
引导扩散模型 GLIDE(尼科尔、达里瓦尔、拉梅什等人,2022)对 CLIP 引导和无分类器引导这两种策略都进行了探索,结果发现更倾向于后者。他们推测,这是因为 CLIP 引导是利用对抗样本对 CLIP 模型进行开发利用,而非优化生成匹配度更高的图像。
Speed up Diffusion Models
通过遵循反向扩散过程的马尔可夫链从 DDPM(去噪扩散概率模型)生成样本的速度非常慢,因为步数可能多达一千步甚至几千步。宋等人(2020)的一项数据表明:“例如,从 DDPM 中采样 50,000 张 32×32 尺寸的图像大约需要 20 个小时,而在英伟达 2080 Ti GPU 上,使用生成对抗网络(GAN)采样同样数量的图像则不到一分钟。”
Fewer Sampling Steps & Distillation
一种简单的方法是采用跨步采样方案(尼科尔和达里瓦尔,2021),每 ⌈ T / S ⌉ \lceil T/S \rceil ⌈T/S⌉ 步进行一次采样更新,从而将采样过程从 T T T 步缩减至 S S S 步。新的生成采样方案为 { τ 1 , … , τ S } \{\tau_1, \dots, \tau_S\} {τ1,…,τS},其中 τ 1 < τ 2 < ⋯ < τ S ∈ [ 1 , T ] \tau_1 < \tau_2 < \cdots < \tau_S \in [1, T] τ1<τ2<⋯<τS∈[1,T] 且 S < T S < T S<T。至于另一种方法,我们可以将 q σ ( x t − 1 ∣ x t , x 0 ) q_\sigma(\mathbf{x}_{t-1} \vert \mathbf{x}_t, \mathbf{x}_0) qσ(xt−1∣xt,x0) 改写为通过期望标准差 σ t \sigma_t σt 进行参数化的形式。
x t − 1 = α ˉ t − 1 x 0 + 1 − α ˉ t − 1 ϵ t − 1 = α ˉ t − 1 x 0 + 1 − α ˉ t − 1 − σ t 2 ϵ t + σ t ϵ = α ˉ t − 1 ( x t − 1 − α ˉ t ϵ θ ( t ) ( x t ) α ˉ t ) + 1 − α ˉ t − 1 − σ t 2 ϵ θ ( t ) ( x t ) + σ t ϵ q σ ( x t − 1 ∣ x t , x 0 ) = N ( x t − 1 ; α ˉ t − 1 ( x t − 1 − α ˉ t ϵ θ ( t ) ( x t ) α ˉ t ) + 1 − α ˉ t − 1 − σ t 2 ϵ θ ( t ) ( x t ) , σ t 2 I ) \begin{aligned} \mathbf{x}_{t-1} &= \sqrt{\bar{\alpha}_{t-1}}\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_{t-1}}\boldsymbol{\epsilon}_{t-1} & \\ &= \sqrt{\bar{\alpha}_{t-1}}\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_{t-1} - \sigma_t^2} \boldsymbol{\epsilon}_t + \sigma_t\boldsymbol{\epsilon} & \\ &= \sqrt{\bar{\alpha}_{t-1}} \Big( \frac{\mathbf{x}_t - \sqrt{1 - \bar{\alpha}_t} \epsilon^{(t)}_\theta(\mathbf{x}_t)}{\sqrt{\bar{\alpha}_t}} \Big) + \sqrt{1 - \bar{\alpha}_{t-1} - \sigma_t^2} \epsilon^{(t)}_\theta(\mathbf{x}_t) + \sigma_t\boldsymbol{\epsilon} \\ q_\sigma(\mathbf{x}_{t-1} \vert \mathbf{x}_t, \mathbf{x}_0) &= \mathcal{N}(\mathbf{x}_{t-1}; \sqrt{\bar{\alpha}_{t-1}} \Big( \frac{\mathbf{x}_t - \sqrt{1 - \bar{\alpha}_t} \epsilon^{(t)}_\theta(\mathbf{x}_t)}{\sqrt{\bar{\alpha}_t}} \Big) + \sqrt{1 - \bar{\alpha}_{t-1} - \sigma_t^2} \epsilon^{(t)}_\theta(\mathbf{x}_t), \sigma_t^2 \mathbf{I}) \end{aligned} xt−1qσ(xt−1∣xt,x0)=αˉt−1x0+1−αˉt−1ϵt−1=αˉt−1x0+1−αˉt−1−σt2ϵt+σtϵ=αˉt−1(αˉtxt−1−αˉtϵθ(t)(xt))+1−αˉt−1−σt2ϵθ(t)(xt)+σtϵ=N(xt−1;αˉt−1(αˉtxt−1−αˉtϵθ(t)(xt))+1−αˉt−1−σt2ϵθ(t)(xt),σt2I)
其中,模型
ϵ
θ
(
t
)
(
.
)
\epsilon^{(t)}_\theta(.)
ϵθ(t)(.) 从
x
t
\mathbf{x}_t
xt 预测
ϵ
t
\epsilon_t
ϵt。
根据前面的定义,在
q
(
x
t
−
1
∣
x
t
,
x
0
)
=
N
(
x
t
−
1
;
μ
~
(
x
t
,
x
0
)
,
β
~
t
I
)
q(\mathbf{x}_{t-1} \vert \mathbf{x}_t, \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_{t-1}; \tilde{\boldsymbol{\mu}}(\mathbf{x}_t, \mathbf{x}_0), \tilde{\beta}_t \mathbf{I})
q(xt−1∣xt,x0)=N(xt−1;μ~(xt,x0),β~tI) 中, 因此我们有:
β ~ t = σ t 2 = 1 − α ˉ t − 1 1 − α ˉ t ⋅ β t \tilde{\beta}_t = \sigma_t^2 = \frac{1 - \bar{\alpha}_{t-1}}{1 - \bar{\alpha}_t} \cdot \beta_t β~t=σt2=1−αˉt1−αˉt−1⋅βt
令
σ
t
2
=
η
⋅
β
~
t
\sigma_t^2 = \eta \cdot \tilde{\beta}_t
σt2=η⋅β~t,这样我们就可以将
η
∈
R
+
\eta \in \mathbb{R}^+
η∈R+ 作为超参数进行调整,以控制采样的随机性。当
η
=
0
\eta = 0
η=0 这个特殊情况时,采样过程就变为确定性的。这样的模型被称为去噪扩散隐式模型(DDIM;宋等人,2020)。DDIM 具有相同的边际噪声分布,但能确定性地将噪声映射回原始数据样本。
在生成过程中,我们无需遵循从
t
=
1
,
2
,
.
.
.
T
t=1,2,...T
t=1,2,...T 的完整链条,而是只需其中的一个步骤子集。设
s
<
t
s < t
s<t 为这个加速轨迹中的两个步骤。DDIM 的更新步骤为:
q σ , s < t ( x s ∣ x t , x 0 ) = N ( x s ; α ˉ s ( x t − 1 − α ˉ t ϵ θ ( t ) ( x t ) α ˉ t ) + 1 − α ˉ s − σ t 2 ϵ θ ( t ) ( x t ) , σ t 2 I ) q_{\sigma, s < t}(\mathbf{x}_s \vert \mathbf{x}_t, \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_s; \sqrt{\bar{\alpha}_s} \Big( \frac{\mathbf{x}_t - \sqrt{1 - \bar{\alpha}_t} \epsilon^{(t)}_\theta(\mathbf{x}_t)}{\sqrt{\bar{\alpha}_t}} \Big) + \sqrt{1 - \bar{\alpha}_s - \sigma_t^2} \epsilon^{(t)}_\theta(\mathbf{x}_t), \sigma_t^2 \mathbf{I}) qσ,s<t(xs∣xt,x0)=N(xs;αˉs(αˉtxt−1−αˉtϵθ(t)(xt))+1−αˉs−σt2ϵθ(t)(xt),σt2I)
在实验中,所有模型均采用 T = 1000 T=1000 T=1000 扩散步骤进行训练。他们观察到,当 S S S 较小时,DDIM([ η = 0 \eta=0 η=0])能够生成质量最佳的样本,而 DDPM([ η = 1 \eta=1 η=1])在较小的 [ S S S] 下表现则差得多。当我们能够承担完整的反向马尔可夫扩散步骤([ S = T = 1000 S=T=1000 S=T=1000])时,DDPM 的表现确实更好。借助 DDIM,可以将扩散模型训练到任意数量的前向步骤,但在生成过程中仅从步骤子集中进行采样。
与去噪扩散概率模型(DDPM)相比,去噪扩散隐式模型(DDIM)具备以下优势:
- 以更少的步数生成更高质量的样本:DDIM 能够在使用显著更少步数的情况下,生成质量更高的样本。这意味着在实际应用中,它可以更高效地完成样本生成任务,减少计算资源的消耗和时间成本。
- 具有 “一致性” 特性:由于 DDIM 的生成过程是确定性的,它具有 “一致性”。这表明基于相同潜在变量生成的多个样本,应具有相似的高级特征。这种一致性在许多应用场景中非常重要,例如需要生成具有相似风格或主题的多个样本时,DDIM 可以确保这些样本在整体特征上保持一致。
- 可在潜在变量中进行语义上有意义的插值:得益于其一致性特性,DDIM 能够在潜在变量空间中进行语义上有意义的插值操作。这使得在潜在变量空间中进行平滑过渡成为可能,从而生成在语义上连贯且有意义的样本序列。例如,在图像生成任务中,可以通过对潜在变量进行插值,实现从一个图像到另一个图像的平滑过渡,生成一系列具有渐进变化的图像。
- 图9:Progressive distillation can reduce the diffusion sampling steps by half in each iteration.
渐进蒸馏(萨利曼斯和霍,2022 年)是一种将训练好的确定性采样器提炼成采样步数减半的新模型的方法。学生模型由教师模型初始化,并且在去噪时,其目标设定为让一个学生 DDIM 步骤匹配两个教师步骤,而不是像原来那样使用样本 x 0 \mathbf{x}_0 x0 作为去噪目标。在每一次渐进蒸馏迭代中,我们都可以将采样步数减半。
- 图 10:Comparison of Algorithm 1 (diffusion model training) and Algorithm 2 (progressive distillation) side-by-side, where the relative changes in progressive distillation are highlighted in green.
相关阅读:
- 扩散模型(三)
参考:
What are Diffusion Models?
Weng, Lilian. (Jul 2021). What are diffusion models? Lil’Log. https://lilianweng.github.io/posts/2021-07-11-diffusion-models/.