supercalifragilisticexpialidocious

Koji Higasa Blog

Score-Based Generative Modeling through Stochastic Differential Equations - notes

Posted at # ML/AI

デノイジング拡散モデル (DDPM) に来て, 拡散モデル (Sohl-Dickstein et al., 2015), および Langevin 動力学を用いたデノイジングスコアマッチング (DSMLD) は本質的に等価であることが示された. それらは離散有限ステップでの摂動を考え方の機軸としているが, 本論文においてはそれを連続無限に拡張するという, 確率微分方程式 (SDE) を用いた一般化をみる. 核となる考えは下図にまとめられている. idea

先行研究の整理

DSMLD (Song & Ermon, 2019)

— 記法と設計 —

— 訓練目標 —

DSM の重み付き和.

θ=arg minθi=1Nσi2Epdata(x)Epσi(x~x)[sθ(x~,σi)xlogpσi(x~x)2]\mathbf{θ}^* = \mathop{\argmin}\limits_{\mathbf{θ}}\sum_{i=1}^Nσ_i^2\mathbb{E}_{p_{\text{data}}(\mathbf{x})}\mathbb{E}_{p_{σ_i}\left(\tilde{\mathbf{x}}|\mathbf{x}\right)}\left[\left\|\mathbf{s}_{\mathbf{θ}}\left(\tilde{\mathbf{x}},σ_i\right)-∇_{\mathbf{x}}\log p_{σ_i}\left(\tilde{\mathbf{x}}|\mathbf{x}\right)\right\|^2\right]

— サンプリング —

焼きなまし Langevin MCMC.

ximxim1+ϵisθ(xim1,σi)+2ϵizim,m=1,2,,Mwhere ϵi>0 and zimN(0,I)\begin{align} \mathbf{x}_i^m &≔ \mathbf{x}_i^{m-1}+ϵ_i\mathbf{s}_{\mathbf{θ}^*}\left(\mathbf{x}_i^{m-1},σ_i\right)+\sqrt{2ϵ_i}\mathbf{z}_i^m, m=1,2,\ldots,M \\ &\text{where }ϵ_i>0\text{ and }\mathbf{z}_i^m∼\mathcal{N}(\mathbf{0},\mathbf{I}) \end{align}

DDPM (Ho et al., 2020)

— 記法と設計 —

— 訓練目標 —

ELBO.

θ=arg minθi=1N(1αi)Epdata(x)Epαi(x~x)[sθ(x~,i)xlogpαi(x~x)2]\mathbf{θ}^* = \mathop{\argmin}\limits_{\mathbf{θ}}\sum_{i=1}^N\left(1-α_i\right)\mathbb{E}_{p_{\text{data}}(\mathbf{x})}\mathbb{E}_{p_{α_i}\left(\tilde{\mathbf{x}}|\mathbf{x}\right)}\left[\left\|\mathbf{s}_{\mathbf{θ}}\left(\tilde{\mathbf{x}},i\right)-∇_{\mathbf{x}}\log p_{α_i}\left(\tilde{\mathbf{x}}|\mathbf{x}\right)\right\|^2\right]

— サンプリング —

Ancestral sampling.

xi1=11βi(xi+βisθ(xi,i))+βizi , i=N,N1,,1where ziN(0,I)\begin{align} \mathbf{x}_{i-1} &= \frac{1}{\sqrt{1-β_i}}\left(\mathbf{x}_i+β_i\mathbf{s}_{\mathbf{θ}^*}\left(\mathbf{x}_i,i\right)\right)+\sqrt{β_i}\mathbf{z}_i\text{ , }i=N,N-1,\ldots,1 \\ &\text{where }\mathbf{z}_i∼\mathcal{N}(\mathbf{0},\mathbf{I}) \end{align}

SDE を用いたスコアベースモデル

下図がこの枠組みの概念図である. framework

SDE による拡散過程のモデル化

境界条件 x(0)p0\mathbf{x}(0)∼p_0 (データ分布), および x(T)pT\mathbf{x}(T)∼p_T (事前分布) を満たす, 連続時間 t[0,T]t\in[0,T] を変数とする拡散過程 {x(t)}t=0T\left\lbrace \mathbf{x}(t)\right\rbrace_{t=0}^T は次のような Itoˆ\text{\^{o}} (伊藤) SDE に対する解としてモデル化できる.

dx=f(x,t)dt+g(t)dw\mathrm{d}\mathbf{x} = \mathbf{f}(\mathbf{x},t)\mathrm{d}t+g(t)\mathrm{d}\mathbf{w}

ここで, w\mathbf{w} は標準 Wiener 過程 (a.k.a., Brown 運動), f(,t):RdRd\mathbf{f}(⋅,t):\mathbb{R}^d→\mathbb{R}^dx(t)\mathbf{x}(t) のドリフト係数とよばれるベクトル値関数, g():RRg(⋅):\mathbb{R}→\mathbb{R}x(t)\mathbf{x}(t) の拡散係数とよばれるスカラー関数である. これらの係数が, x\mathbf{x} および tt に対し大域的 Lipschitz 条件を満たす限り, Itoˆ\text{\^{o}} SDE は一意な強解を持つ (Øksendal, 2003).

SDE による逆拡散過程のモデル化

逆拡散過程もまた TT から 00 へと時間を遡る方向の拡散過程となり, 次の逆時間 SDE でモデル化される (Anderson, 1982).

dx=[f(x,t)g(t)2xlogpt(x)]dt+g(t)dwˉ\mathrm{d}\mathbf{x} = \left[\mathbf{f}(\mathbf{x},t)-g(t)^2∇_\mathbf{x}\log p_t(\mathbf{x})\right]\mathrm{d}t+g(t)\mathrm{d}\bar{\mathbf{w}}

ここで, wˉ\bar{\mathbf{w}} は標準 Wiener 過程, dt\mathrm{d}t は逆向き時間の無限小.

確率フロー ODE

定理. 拡散 / 逆拡散 SDE と同じ周辺確率密度 {pt(x)}t=0T\left\lbrace p_t(\mathbf{x})\right\rbrace_{t=0}^T を軌跡に持つ決定論的 ODE が存在し, それは以下で与えられる. この ODE を確率フロー ODE とよぶ.

dx=[f(x,t)12g(t)2xlogpt(x)]dt\mathrm{d}\mathbf{x} = \left[\mathbf{f}(\mathbf{x},t)-\frac{1}{2}g(t)^2∇_{\mathbf{x}}\log p_t(\mathbf{x})\right]\mathrm{d}t

証明. 次の最も一般の形式を持つ SDE に対して証明する.

dx=f(x,t)dt+G(x,t)dw\mathrm{d}\mathbf{x} = \mathbf{f}(\mathbf{x},t)\mathrm{d}t+\mathbf{G}(\mathbf{x},t)\mathrm{d}\mathbf{w}

ここで, f(,t):RdRd\mathbf{f}(⋅,t):\mathbb{R}^d→\mathbb{R}^d はドリフト係数, G(,t):RdRd×d\mathbf{G}(⋅,t):\mathbb{R}^d→\mathbb{R}^{d×d} は拡散係数.

周辺分布 pt(x)p_t(\mathbf{x}) の時間発展は次の Kolmogorov 前進 (a.k.a., Fokker-Planck) 方程式で与えられる.

    pt(x)t=i=1dxi[fi(x,t)pt(x)]+12i=1dj=1d2xixj[k=1dGik(x,t)Gjk(x,t)pt(x)]=i=1dxi[fi(x,t)pt(x)]  +12i=1dxi[j=1d{xj[k=1dGik(x,t)Gjk(x,t)]pt(x)+k=1dGik(x,t)Gjk(x,t)pt(x)xjlogpt(x)}]=i=1dxi[fi(x,t)pt(x)]  +12i=1dxi[[G(x,t)G(x,t)T]pt(x)+G(x,t)G(x,t)Tpt(x)xlogpt(x)]=i=1dxi[{fi(x,t)12[[G(x,t)G(x,t)T]+G(x,t)G(x,t)Txlogpt(x)]}pt(x)]i=1dxi[f~i(x,t)pt(x)]\begin{align} &\space\space\space\space\frac{∂p_t(\mathbf{x})}{∂t} \\ &= -\sum_{i=1}^d\frac{∂}{∂x_i}\left[f_i(\mathbf{x},t)p_t(\mathbf{x})\right]+\frac{1}{2}\sum_{i=1}^d\sum_{j=1}^d\frac{∂^2}{∂x_i∂x_j}\left[\sum_{k=1}^dG_{ik}(\mathbf{x},t)G_{jk}(\mathbf{x},t)p_t(\mathbf{x})\right] \\ &= -\sum_{i=1}^d\frac{∂}{∂x_i}\left[f_i(\mathbf{x},t)p_t(\mathbf{x})\right] \\ &\space\space+\frac{1}{2}\sum_{i=1}^d\frac{∂}{∂x_i}\left[\sum_{j=1}^d\left\lbrace\frac{∂}{∂x_j}\left[\sum_{k=1}^dG_{ik}(\mathbf{x},t)G_{jk}(\mathbf{x},t)\right]p_t(\mathbf{x})+\sum_{k=1}^dG_{ik}(\mathbf{x},t)G_{jk}(\mathbf{x},t)p_t(\mathbf{x})\frac{∂}{∂x_j}\log p_t(\mathbf{x})\right\rbrace\right] \\ &= -\sum_{i=1}^d\frac{∂}{∂x_i}\left[f_i(\mathbf{x},t)p_t(\mathbf{x})\right] \\ &\space\space+\frac{1}{2}\sum_{i=1}^d\frac{∂}{∂x_i}\left[∇⋅\left[\mathbf{G}(\mathbf{x},t)\mathbf{G}(\mathbf{x},t)^{\mathrm{T}}\right]p_t(\mathbf{x})+\mathbf{G}(\mathbf{x},t)\mathbf{G}(\mathbf{x},t)^{\mathrm{T}}p_t(\mathbf{x})∇_{\mathbf{x}}\log p_t(\mathbf{x})\right] \\ &=-\sum_{i=1}^d\frac{∂}{∂x_i}\left[\left\lbrace f_i(\mathbf{x},t)-\frac{1}{2}\left[∇⋅\left[\mathbf{G}(\mathbf{x},t)\mathbf{G}(\mathbf{x},t)^{\mathrm{T}}\right]+\mathbf{G}(\mathbf{x},t)\mathbf{G}(\mathbf{x},t)^{\mathrm{T}}∇_{\mathbf{x}}\log p_t(\mathbf{x})\right]\right\rbrace p_t(\mathbf{x})\right] \\ &≔-\sum_{i=1}^d\frac{∂}{∂x_i}\left[\tilde{\mathbf{f}}_i(\mathbf{x},t)p_t(\mathbf{x})\right] \end{align}

結局これは, dx=f~(x,t)dt+G~(x,t)dw\mathrm{d}\mathbf{x}=\tilde{\mathbf{f}}(\mathbf{x},t)\mathrm{d}t+\tilde{\mathbf{G}}(\mathbf{x},t)\mathrm{d}\mathbf{w} という SDE において, G~(x,t)=O\tilde{\mathbf{G}}(\mathbf{x},t)=\mathbf{O} とした ODE dx=f~(x,t)dt\mathrm{d}\mathbf{x}=\tilde{\mathbf{f}}(\mathbf{x},t)\mathrm{d}t の Kolmogorov 前進方程式となっている. f~(x,t)\tilde{\mathbf{f}}(\mathbf{x},t) において, G(x,t)=g(t)I\mathbf{G}(\mathbf{x},t)=g(t)\mathbf{I} とした特別の場合が確率フロー ODE である. \square

訓練目標

θ=arg minθEt{λ(t)Ex(0)pdata(x)Ex(t)p0t(x(t)x(0))[sθ(x(t),t)x(t)logp0t(x(t)x(0))2]}\mathbf{θ}^* = \mathop{\argmin}\limits_{\mathbf{θ}}\mathbb{E}_t\left\lbrace λ(t)\mathbb{E}_{\mathbf{x}(0)∼p_{\text{data}}(\mathbf{x})}\mathbb{E}_{\mathbf{x}(t)∼p_{0t}\left(\mathbf{x}(t)|\mathbf{x}(0)\right)}\left[\left\|\mathbf{s}_{\mathbf{θ}}\left(\mathbf{x}(t),t\right)-∇_{\mathbf{x}(t)}\log p_{0t}\left(\mathbf{x}(t)|\mathbf{x}(0)\right)\right\|^2\right]\right\rbrace

ここで, λ:[0,T]R>0λ:[0,T]→\mathbb{R}_{>0} は重み付け関数, tU(0,T)t∼U(0,T). 評価のためには遷移核 p0t(x(t)x(0))p_{0t}\left(\mathbf{x}(t)|\mathbf{x}(0)\right) を求める必要があるが, f(,t)\mathbf{f}(⋅,t) が Affine 変換ならそれは常に Gauss 分布となり閉形式でかける (Särkkä & Solin, 2019). しかし一般の場合には, Kolmogorov 前進方程式を解かなければならない.

サンプリング

逆拡散サンプラー

逆拡散 SDE の離散化.

xi=xi+1fi+1(xi+1)+gi+12sθ(xi+1,i+1)+gi+1zi+1 , i=N1,N2,,0where zi+1N(0,I)\begin{align} \mathbf{x}_i &= \mathbf{x}_{i+1}-\mathbf{f}_{i+1}\left(\mathbf{x}_{i+1}\right)+g_{i+1}^2\mathbf{s}_{θ^*}\left(\mathbf{x}_{i+1},i+1\right)+g_{i+1}\mathbf{z}_{i+1}\text{ , }i=N-1,N-2,\ldots,0 \\ &\text{where }\mathbf{z}_{i+1}∼\mathcal{N}(\mathbf{0},\mathbf{I}) \end{align}

予測器 - 修正器 (PC) サンプラー

逆拡散サンプラー (predictor) + 焼きなまし Langevin MCMC (corrector). PC sampling

確率フローサンプラー

確率フロー ODE の離散化.

xi=xi+1fi+1(xi+1)12gi+12sθ(xi+1,i+1) , i=N1,N2,,0\mathbf{x}_i = \mathbf{x}_{i+1}-\mathbf{f}_{i+1}\left(\mathbf{x}_{i+1}\right)-\frac{1}{2}g_{i+1}^2\mathbf{s}_{θ^*}\left(\mathbf{x}_{i+1},i+1\right)\text{ , }i=N-1,N-2,\ldots,0

これは決定論的ゆえサンプルを早く生成することが可能だが, 結局サンプル品質向上 (離散化誤差軽減) のためには多少のノイズを加えるのがよいとされている.

例: VE, VP SDE, およびその先へ

以上の議論を DSMLD と DDPM に適用する. 得られた 2 つの異なる SDE は, 分散の時間発展性に応じてそれぞれ 分散爆発型 (VE) SDE, 分散保存型 (VP) SDE とよばれる.

VE SDE

DSMLD の 各摂動核 pσi(xixi1)p_{σ_i}\left(\mathbf{x}_i|\mathbf{x}_{i-1}\right) に対応する xi\mathbf{x}_i の分布は, 次の離散 Markov 鎖で与えられる.

xi=xi1+σi2σi12zi1 , i=1,2,,Nwhere zi1N(0,I)\begin{align} \mathbf{x}_i &= \mathbf{x}_{i-1}+\sqrt{σ_i^2-σ_{i-1}^2}\mathbf{z}_{i-1}\text{ , }i=1,2,\ldots,N \\ &\text{where }\mathbf{z}_{i-1}∼\mathcal{N}(\mathbf{0},\mathbf{I}) \end{align}

Δt=1NΔt=\frac{1}{N}, t={0,1N,,N1N}t=\left\lbrace 0,\frac{1}{N},\cdots,\frac{N-1}{N}\right\rbrace とし, NN→∞ の極限において 1 次近似までをとれば, 連続確率過程 x(t)\mathbf{x}(t) として次の SDE を得る.

dx=dσ2(t)dtdw\mathrm{d}\mathbf{x} = \sqrt{\frac{\mathrm{d}σ^2(t)}{\mathrm{d}t}}\mathrm{d}\mathbf{w}

そして, 遷移核として次を得る.

p0t(x(t)x(0))=N(x(t);x(0),[σ2(t)σ2(0)]I)p_{0t}\left(\mathbf{x}(t)|\mathbf{x}(0)\right) = \mathcal{N}\left(\mathbf{x}(t);\mathbf{x}(0),\left[σ^2(t)-σ^2(0)\right]\mathbf{I}\right)

また, 逆拡散サンプラー, および確率フローサンプラーとして以下を得る.

VP SDE

DDPM の 各摂動核 pσi(xixi1)p_{σ_i}\left(\mathbf{x}_i|\mathbf{x}_{i-1}\right) に対応する xi\mathbf{x}_i の分布は, 次の離散 Markov 鎖で与えられる.

xi=1βixi1+βizi1 , i=1,2,,Nwhere zi1N(0,I)\begin{align} \mathbf{x}_i &= \sqrt{1-β_i}\mathbf{x}_{i-1}+\sqrt{β_i}\mathbf{z}_{i-1}\text{ , }i=1,2,\ldots,N \\ &\text{where }\mathbf{z}_{i-1}∼\mathcal{N}(\mathbf{0},\mathbf{I}) \end{align}

Δt=1NΔt=\frac{1}{N}, t={0,1N,,N1N}t=\left\lbrace 0,\frac{1}{N},\cdots,\frac{N-1}{N}\right\rbrace とし, NN→∞ の極限において 1 次近似までをとれば, 連続確率過程 x(t)\mathbf{x}(t) として次の SDE を得る.

dx=12β(t)xdt+β(t)dw\mathrm{d}\mathbf{x} = -\frac{1}{2}β(t)\mathbf{x}\mathrm{d}t+\sqrt{β(t)}\mathrm{d}\mathbf{w}

そして, 遷移核として次を得る.

p0t(x(t)x(0))=N(x(t);x(0)exp(120tβ(s)ds),[1exp(0tβ(s)ds)]I)p_{0t}\left(\mathbf{x}(t)|\mathbf{x}(0)\right) = \mathcal{N}\left(\mathbf{x}(t);\mathbf{x}(0)\exp\left(-\frac{1}{2}∫_0^tβ(s)\mathrm{d}s\right),\left[1-\exp\left(-∫_0^tβ(s)\mathrm{d}s\right)\right]\mathbf{I}\right)

また, 逆拡散サンプラー, および確率フローサンプラーとして以下を得る.

sub-VP SDE

VP SDE の性質を引き継いだ上で, より低分散を実現しようという一工夫.

dx=12β(t)xdt+β(t)[1exp(20tβ(s)ds)]dw\mathrm{d}\mathbf{x} = -\frac{1}{2}β(t)\mathbf{x}\mathrm{d}t+\sqrt{β(t)\left[1-\exp\left(-2∫_0^tβ(s)\mathrm{d}s\right)\right]}\mathrm{d}\mathbf{w}

このとき, 遷移核として次を得る.

p0t(x(t)x(0))=N(x(t);x(0)exp(120tβ(s)ds),[1exp(0tβ(s)ds)]2I)p_{0t}\left(\mathbf{x}(t)|\mathbf{x}(0)\right) = \mathcal{N}\left(\mathbf{x}(t);\mathbf{x}(0)\exp\left(-\frac{1}{2}∫_0^tβ(s)\mathrm{d}s\right),\left[1-\exp\left(-∫_0^tβ(s)\mathrm{d}s\right)\right]^2\mathbf{I}\right)