文章目录
Diffusion Model(2):前向扩散过程和逆向降噪过程
观看本文之前建议先观看以下文章:
在推导过程中会参考其中中的一些公式,使用到的公式都会标注出来。
Diffusion Models(扩散模型)包含以下三类:
- diffusion probabilistic models 扩散概率模型 Sohl-Dickstein et al., 2015
- noise-conditioned score network 噪声条件分数网络 (NCSN; Yang & Ermon, 2019)
- denoising diffusion probabilistic models去噪扩散概率模型 (DDPM; Ho et al. 2020)。
本文以2020年Ho等人的DDPM为例,其包含了前向扩散过程和反向的扩散过程。
其中,前向扩散过程是为了将复杂的分布转化为一个简单的分布。而反向扩散过程则是从简单分布逆转得到复杂分布。
Forward diffusion process
扩散(Diffusion)在热力学中指细小颗粒从高密度区域扩散至低密度区域,在统计领域,扩散则指将复杂的分布转换为一个简单的分布的过程。
Diffusion模型定义了一个概率分布转换模型 T \mathcal{T} T,能将原始数据 x 0 x_0 x0构成的复杂分布 q c o m p l e x q_{\mathrm{complex}} qcomplex转换为一个简单的已知参数的先验分布 p p r i o r p_{\mathrm{prior}} pprior:
x 0 ∼ q c o m p l e x ⟹ T ( x 0 ) ∼ p p r i o r \begin{equation} \mathbf{x}_0 \sim q_\mathrm{complex}⟹\mathcal{T}(\mathbf{x}_0) \sim p_\mathrm{prior} \end{equation} x0∼qcomplex⟹T(x0)∼pprior
具体来说,Diffusion模型提出可以用马尔科夫链(Markov Chain)来构造 T \mathcal{T} T,即定义一系列条件概率分布 q ( x t ∣ x t − 1 ) t ∈ { 1 , 2 , 3... T } q(\mathbf{x}_t \vert \mathbf{x}_{t-1})\quad t\in\{1,2,3...T\} q(xt∣xt−1)t∈{
1,2,3...T},将 x 0 \mathbf{x_0} x0依次转换为 x 1 \mathbf{x_1} x1, x 2 \mathbf{x_2} x2 , . . . , x T ,...,\mathbf{x_T} ,...,xT,希望当 T → inf T \rightarrow \inf T→inf时, x T ∼ p prior \mathbf{x}_{T} \sim p_{\text {prior }} xT∼pprior 。
为了简洁和有效,此处的 p prior p_{\text {prior }} pprior 选择高斯分布,因此整个前向扩散过程可以被看作是,在 T T T步内,不断添加少量的高斯噪声到样本中。
q ( x t ∣ x t − 1 ) = N ( x t ; 1 − β t x t − 1 , β t I ) q ( x 1 : T ∣ x 0 ) = ∏ t = 1 q ( x t ∣ x t − 1 ) q ( x T ) = p prior ( x T ) = N ( x T ; 0 , I ) where T → inf \begin{equation} \begin{array}{c} q\left(\mathbf{x}_{t} \vert \mathbf{x}_{t-1}\right)=\mathcal{N}\left(\mathbf{x}_{t} ; \sqrt{1-\beta_{t}} \mathbf{x}_{t-1}, \beta_{t} \mathrm{I}\right) \\ q\left(\mathbf{x}_{1: T} \vert \mathbf{x}_{0}\right)=\prod_{t=1} q\left(\mathbf{x}_{t} \vert \mathbf{x}_{t-1}\right)\\ q\left(\mathbf{x}_{T}\right)=p_{\text {prior }}\left(\mathbf{x}_{T}\right)=\mathcal{N}\left(\mathbf{x}_{T} ; \mathbf{0}, \mathrm{I}\right) \quad \text { where } T \rightarrow \inf \end{array} \end{equation} q(xt∣xt−1)=N(xt;1−βtxt−1,βtI)q(x1:T∣x0)=∏t=1q(xt∣xt−1)q(xT)=pprior (xT)=N(xT;0,I) where T→inf
即已知 x t − 1 \mathbf{x_{t-1}} xt−1的时候, x t \mathbf{x_t} xt的概率分布为一个平均值为 1 − β t x t − 1 \sqrt{1-\beta_{t}} \mathbf{x}_{t-1} 1−βtxt−1,方差为 β t I \beta_tI βtI的高斯分布。随着 T T T的不断增大,最终数据分布变成了一个简单固定的高斯分布。
然后对公式2使用Diffusion Model(1):预备知识中提到的重参数化技巧(以下Diffusion Model(1):预备知识中的公式用1-xx代替)进行重参数化可以得到:
x t = 1 − β t x t − 1 + β t z t − 1 where z t − 1 ∈ N ( 0 , I ) \begin{equation} \mathbf{x}_{t}=\sqrt{1-\beta_{t}} \mathbf{x}_{t-1}+\sqrt{\beta_{t}} \mathbf{z}_{t-1} \quad \text { where } \mathbf{z}_{t-1} \in \mathcal{N}(0, \mathbf{I}) \end{equation} xt=1−βtxt−1+βtzt−1 where zt−1∈N(0,I)
这一过程即将高斯分布采样的过程变成了将 x t − 1 \mathbf{x_{t-1}} xt−1与标准高斯分布噪声 z \mathbf{z} z混合,扩散率系数 β t \beta_t βt控制融合 x t − 1 \mathbf{x_{t-1}} xt−1分布和标准高斯分布的比例。
设 α t = 1 − β t \alpha_t=1-\beta_t αt=1−βt以及 α ˉ t = ∏ i = 1 t α i \bar{\alpha}_{t}=\prod_{i=1}^{t} \alpha_{i} αˉt=∏i=1tαi,那么公式3就变成了:
x t = α t x t − 1 + 1 − α t z t − 1 ; where z t − 1 , z t − 2 , ⋯ ∼ N ( 0 , I ) = α t ( α t − 1 x t − 2 + 1 − α t − 1 z t − 2 ) + 1 − α t z t − 1 = α t α t − 1 x t − 2 + α t ( 1 − α t − 1 ) z t − 2 + 1 − α t z t − 1 = α t α t − 1 x t − 2 + 1 − α t − 1 α t z ˉ t − 2 ; where z ˉ t − 2 , z ˉ t − 3 , ⋯ ∼ N ( 0 , I ) = α ˉ t x 0 + 1 − α ˉ t z \begin{equation} \begin{array}{rlr} \mathbf{x}_{t} & =\sqrt{\alpha_{t}} {\color{blue}\mathbf{x}_{t-1}}+\sqrt{1-\alpha_{t}} \mathbf{z}_{t-1} & ; \text { where } \mathbf{z}_{t-1}, \mathbf{z}_{t-2}, \cdots \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) \\ & =\sqrt{\alpha_t}{\color{blue}(\sqrt{\alpha_{t-1}}x_{t-2} + \sqrt{1- \alpha_{t-1}} z_{t-2})} + \sqrt{1- \alpha_t} z_{t-1} & \\ & =\sqrt{\alpha_t\alpha_{t-1}}x_{t-2} + {\color{red}\sqrt{
{\alpha_t}(1- \alpha_{t-1})} z_{t-2} + \sqrt{1- \alpha_t} z_{t-1}} \\ &= \sqrt{\alpha_t\alpha_{t-1}}x_{t-2} + {\color{red}\sqrt{1- \alpha_{t-1}\alpha_t} \bar{z}_{t-2}} & ; \text { where } \bar{\mathbf{z}}_{t-2}, \bar{\mathbf{z}}_{t-3}, \cdots \sim \mathcal{N}(\mathbf{0}, \mathbf{I})\\ & =\sqrt{\bar{\alpha}_{t}} \mathbf{x}_{0}+\sqrt{1-\bar{\alpha}_{t}} \mathbf{z} & \\ \end{array} \end{equation} xt=αtxt−1+1−αtzt−1=αt(αt−1xt−2+1−αt−1zt−2)+1−αtzt−1=αtαt−1xt−2+αt(1−αt−1)zt−2+1−αtzt−1=αtαt−1xt−2+1−αt−1αtzˉt−2=αˉtx0+1−αˉtz; where zt−1,zt−2,⋯∼N(0,I); where zˉt−2,zˉt−3,⋯∼N(0,I)
其中公式4从第一行到第二行是将 x t − 1 \mathbf{x_{t-1}} xt−1继续利用重参数化技巧展开,而从第三行到第四行利用了当两个高斯分布 N ( 0 , σ 1 2 I ) \mathcal{N}\left(\mathbf{0}, \sigma_{1}^{2} \mathbf{I}\right) N(0,σ12I)和 N ( 0 , σ 2 2 I ) \mathcal{N}\left(\mathbf{0}, \sigma_{2}^{2} \mathbf{I}\right) N(0,σ22I)相加时,新的分布为 N ( 0 , ( σ 1 2 + σ 2 2 ) I ) \mathcal{N}\left(\mathbf{0}, (\sigma_{1}^{2}+\sigma_{2}^{2}) \mathbf{I}\right) N(0,(σ12