本文主要是介绍DDPM公式推导(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2 Background
扩散模型【53】是一种以 p θ ( x 0 ) : = ∫ p θ ( x 0 : T ) d x 1 : T p_\theta\left(\mathbf{x}_0\right):=\int p_\theta\left(\mathbf{x}_{0: T}\right) d \mathbf{x}_{1: T} pθ(x0):=∫pθ(x0:T)dx1:T 形式的潜在变量模型,其中 x 1 , … , x T \mathbf{x}_1, \ldots, \mathbf{x}_T x1,…,xT 是与数据 x 0 ∼ q ( x 0 ) \mathbf{x}_0 \sim q\left(\mathbf{x}_0\right) x0∼q(x0) 同维度的潜变量。联合分布 p θ ( x 0 : T ) p_\theta\left(\mathbf{x}_{0: T}\right) pθ(x0:T) 被称为反向过程,其定义为从 p ( x T ) = N ( x T ; 0 , I ) p\left(\mathbf{x}_T\right)=\mathcal{N}\left(\mathbf{x}_T ; \mathbf{0}, \mathbf{I}\right) p(xT)=N(xT;0,I) 开始的学习高斯过渡的马尔科夫链:
p θ ( x 0 : T ) : = p ( x T ) ∏ t = 1 T p θ ( x t − 1 ∣ x t ) , p θ ( x t − 1 ∣ x t ) : = N ( x t − 1 ; μ θ ( x t , t ) , Σ θ ( x t , t ) ) ( 1 ) p_\theta\left(\mathbf{x}_{0: T}\right):=p\left(\mathbf{x}_T\right) \prod_{t=1}^T p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right), \quad 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) \quad(1) pθ(x0:T):=p(xT)t=1∏Tpθ(xt−1∣xt),pθ(xt−1∣xt):=N(xt−1;μθ(xt,t),Σθ(xt,t))(1)
将扩散模型与其他类型的潜在变量模型区别开来的是,近似后验 q ( x 1 : T ∣ x 0 ) q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) q(x1:T∣x0)(被称为前向过程或扩散过程)固定为一个马尔科夫链,该链根据方差调度 β 1 , … , β T \beta_1, \ldots, \beta_T β1,…,βT 逐渐向数据添加高斯噪声:
q ( x 1 : T ∣ x 0 ) : = ∏ t = 1 T q ( x t ∣ x t − 1 ) , q ( x t ∣ x t − 1 ) : = N ( x t ; 1 − β t x t − 1 , β t I ) ( 2 ) q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right):=\prod_{t=1}^T q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right), \quad q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right):=\mathcal{N}\left(\mathbf{x}_t ; \sqrt{1-\beta_t} \mathbf{x}_{t-1}, \beta_t \mathbf{I}\right)\quad(2) q(x1:T∣x0):=t=1∏Tq(xt∣xt−1),q(xt∣xt−1):=N(xt;1−βtxt−1,βtI)(2)
训练过程主要是通过优化变分下界(也称作证据下界,或ELBO)的负对数似然性来完成的:
E [ − log p θ ( x 0 ) ] ≤ E q [ − log p θ ( x 0 : T ) q ( x 1 : T ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t ≥ 1 log p θ ( x t − 1 ∣ x t ) q ( x t ∣ x t − 1 ) ] = : L ( 3 ) \mathbb{E}\left[-\log p_\theta\left(\mathbf{x}_0\right)\right] \leq \mathbb{E}_q\left[-\log \frac{p_\theta\left(\mathbf{x}_{0: T}\right)}{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}\right]=\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t \geq 1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)}\right]=: L \quad(3) E[−logpθ(x0)]≤Eq[−logq(x1:T∣x0)pθ(x0:T)]=Eq[−logp(xT)−t≥1∑logq(xt∣xt−1)pθ(xt−1∣xt)]=:L(3)
可以通过重参数化【33】学习前向过程的方差 β t \beta_t βt,或者将其视为恒定的超参数,并通过选择高斯条件下的 p θ ( x t − 1 ∣ x t ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right) pθ(xt−1∣xt),确保反向过程的表达力,因为当 β t \beta_t βt 很小的时候,这两个过程具有相同的函数形式【53】。前向过程的一个显著特性是,它允许在任意时间步长 t t t 以封闭形式采样 x t \mathbf{x}_t xt:使用符号 α t : = 1 − β t \alpha_t:=1-\beta_t αt:=1−βt 和 α ˉ t : = ∏ s = 1 t α s \bar{\alpha}_t:=\prod_{s=1}^t \alpha_s αˉt:=∏s=1tαs,我们有:
q ( x t ∣ x 0 ) = N ( x t ; α ˉ t x 0 , ( 1 − α ˉ t ) I ) ( 4 ) q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)=\mathcal{N}\left(\mathbf{x}_t ; \sqrt{\bar{\alpha}_t} \mathbf{x}_0,\left(1-\bar{\alpha}_t\right) \mathbf{I}\right)\quad(4) q(xt∣x0)=N(xt;αˉtx0,(1−αˉt)I)(4)
因此,我们可以通过使用随机梯度下降优化 L L L的随机项进行高效训练。进一步的改进来自于通过将 L L L(3)重写为以下格式来降低方差:
E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) ⏟ L T + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) ⏟ L t − 1 − log p θ ( x 0 ∣ x 1 ) ⏟ L 0 ] ( 5 ) \mathbb{E}_q[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)}_{L_T}+\sum_{t>1} \underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)}_{L_{t-1}} \underbrace{-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}_{L_0}]\quad(5) Eq[LT DKL(q(xT∣x0)∥p(xT))+t>1∑Lt−1 DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))L0 −logpθ(x0∣x1)](5)
(详情请参见附录A。这些项的标签用于第3节。)公式(5)使用KL散度直接将 p θ ( x t − 1 ∣ x t ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right) pθ(xt−1∣xt)与条件于 x 0 \mathbf{x}_0 x0时可以处理的前向过程后验进行比较:
q ( x t − 1 ∣ x t , x 0 ) = N ( x t − 1 ; μ ~ t ( x t , x 0 ) , β ~ t I ) ( 6 ) where μ ~ t ( x t , x 0 ) : α ˉ t − 1 β t 1 − α ˉ t x 0 + α t ( 1 − α ˉ t − 1 ) 1 − α ˉ t x t and β ~ t : = 1 − α ˉ t − 1 1 − α ˉ t β t ( 7 ) \begin{aligned} q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) & =\mathcal{N}\left(\mathbf{x}_{t-1} ; \tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right), \tilde{\beta}_t \mathbf{I}\right) \quad(6)\\ \text { where } \quad \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 \quad \text { and } \quad \tilde{\beta}_t:=\frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t \quad(7) \end{aligned} q(xt−1∣xt,x0) where μ~t(xt,x0)=N(xt−1;μ~t(xt,x0),β~tI)(6):1−αˉtαˉt−1βtx0+1−αˉtαt(1−αˉt−1)xt and β~t:=1−αˉt1−αˉt−1βt(7)
因此,公式(5)中的所有KL散度都是高斯间的比较,所以它们可以以Rao-Blackwellized的方式通过封闭形式的表达式计算,而不是使用高方差的蒙特卡洛估计。
上篇文章DDPM公式推导(一)已经推导了式(2)和式(4),下面我们来推导式(3)
设概率模型的分布为 p ( x ; θ ) p(\mathbf{x} ; \theta) p(x;θ) ,其中 θ \theta θ 为分布 p p p 的参数, x \mathbf{x} x 代表观察到的随机变量,在这里就是图像,准确的说 x \mathbf{x} x 是包含 D D D 个像素的高维张量,其每个分量都代表一个像素,因此 x \mathbf{x} x 是多元随机变量。为了区别于其他分布,模型的分布表示为 p θ ( x ) p_\theta(\mathbf{x}) pθ(x) 。
补充知识–最大似然估计
在深度学习中,估计模型参数的一种常用方法是最大似然估计。似然 (likelihood) 这个词是从贝叶斯公式中而来。如果有给定观测随机变量 X X X 和假设 Y Y Y ,则根据贝叶斯公式有:
P ( Y ∣ X ) = P ( X ∣ Y ) P ( Y ) P ( X ) P(Y \mid X)=\frac{P(X \mid Y) P(Y)}{P(X)} P(Y∣X)=P(X)P(X∣Y)P(Y)
其中等式左侧 P ( Y ∣ X ) P(Y \mid X) P(Y∣X) 是给定数据 X X X 的情况下假设 Y Y Y 成立的概率,称为后验概率,右侧分子中 P ( Y ) P(Y) P(Y) 称为假设的先验概率,而 P ( X ∣ Y ) P(X \mid Y) P(X∣Y) 在假设 Y Y Y 下 X X X 出现的概率,称为似然。而分母 P ( X ) P(X) P(X) 称为数据或证据,主要作用是归一化后验概率,使概率积分为 1 ,一般用 α \alpha α 代替,因此可以简写如下:
P ( Y ∣ X ) = α P ( X ∣ Y ) P ( Y ) P(Y \mid X)=\alpha P(X \mid Y) P(Y) P(Y∣X)=αP(X∣Y)P(Y)
如果所有可能的假设 Y Y Y 都有等概率的可能性,则 P ( Y ) P(Y) P(Y) 为常数,因此要使后验 P ( Y ∣ X ) P(Y \mid X) P(Y∣X) 最大,需要似然 P ( X ∣ Y ) P(X \mid Y) P(X∣Y) 最大,这就是最大似然估计。
p θ ( x ) p_\theta(\mathbf{x}) pθ(x)定义为似然,模型参数 θ \theta θ 本身也是随机变量,可以被视为一种假设,显然 θ \theta θ 是未知的, x \mathbf{x} x 是观察到的随机变量,是已知的,那么就有给定已知的 x \mathbf{x} x 下假设 θ \theta θ 的后验概率 p ( θ ∣ x ) p(\theta \mid \mathbf{x}) p(θ∣x) 。设真实分布的参数为 Θ \Theta Θ ,则显然有 p ( Θ ∣ x ) = 1 p(\Theta \mid \mathbf{x})=1 p(Θ∣x)=1 (在给定 x \mathbf{x} x 下真实分布的参数 Θ \Theta Θ 必然存在),因此 p ( θ ∣ x ) ≤ p ( Θ ∣ x ) = 1 p(\theta \mid x) \leq p(\Theta \mid x)=1 p(θ∣x)≤p(Θ∣x)=1 ,进而得到这样的推论:后验概率 p ( θ ∣ x ) p(\theta \mid \mathbf{x}) p(θ∣x) 越大,就越接近真实分布,因此目标就是最大化 p ( θ ∣ x ) p(\theta \mid \mathbf{x}) p(θ∣x) 。用贝叶斯公式表示 p ( θ ∣ x ) p(\theta \mid \mathbf{x}) p(θ∣x) 就有:
p ( θ ∣ x ) = α p ( x ∣ θ ) p ( θ ) p(\theta \mid \mathbf{x})=\alpha p(\mathbf{x} \mid \theta) p(\theta) p(θ∣x)=αp(x∣θ)p(θ)
由于 p ( x ∣ θ ) p(\mathbf{x} \mid \theta) p(x∣θ) 是给定 θ \theta θ 下生成 x \mathbf{x} x 的条件概率分布,而这与 p θ ( x ) p_\theta(\mathbf{x}) pθ(x) 原始定义 p ( x ; θ ) p(\mathbf{x} ; \theta) p(x;θ) 的意义相同,因此两者是等价的。因此,最大化似然就是最大化 p θ ( x ) p_\theta(\mathbf{x}) pθ(x)。
由于对数有很好的数学性质,可以将乘除法变为加减法,降低求导的复杂度,减小计算量,同时还可以防止计算机在数值计算时的溢出,并且不会影响估计的结果。通常使用对数似然来进行估计,即 log p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x),DDPM 的训练目标是使对数似然最大的模型参数 θ \theta θ ,可以表示为:
θ = arg max θ log p θ ( x ) \theta=\underset{\theta}{\arg \max } \log p_\theta(\mathbf{x}) θ=θargmaxlogpθ(x)
将 x 0 \mathbf{x}_0 x0代入扩散模型可以表示为:
p θ ( x 0 ) : = ∫ p θ ( x 0 : T ) d x 1 : T p_\theta\left(\mathbf{x}_0\right):=\int p_\theta\left(\mathbf{x}_{0: T}\right) d \mathbf{x}_{1: T} pθ(x0):=∫pθ(x0:T)dx1:T
取log,得:
log p θ ( x 0 ) = log ∫ p θ ( x 0 , x 1 : T ) d x 1 : T \log p_\theta\left(\mathbf{x}_0\right)=\log \int p_\theta\left(\mathbf{x}_0, \mathbf{x}_{1: T}\right) d \mathbf{x}_{1: T} logpθ(x0)=log∫pθ(x0,x1:T)dx1:T
但是这种高维积分难以处理,因此引入变分推断来解决这个问题
知识补充-变分推断
Variational Inference(变分推断)用一个已知的、易于处理和采样 (tractable) 的分布(例如高斯分布或指数分布) 去近似一个未知复杂的难以处理的分布。在数学上变分推断常用 KL-Divergence ( K L \mathrm{KL} KL 散度,符号 D K L \mathcal{D}_{K L} DKL ) 来近似,KL散度表示两个分布的相似度,值域是标量,值越小两个分布越接近,并且 KL 散度有个性质是 D K L ≥ 0 \mathcal{D}_{K L} \geq 0 DKL≥0 (通过 Jensen 不等式证明)。KL 散度公式如下:
D K L ( q ( x ) ∥ p ( x ) ) = ∫ q ( x ) log q ( x ) p ( x ) d x = ∫ log q ( x ) p ( x ) q ( x ) d x = E q [ log q ( x ) p ( x ) ] \begin{aligned} \mathcal{D}_{K L}(q(x) \| p(x)) & =\int q(x) \log \frac{q(x)}{p(x)} d x \\ & =\int \log \frac{q(x)}{p(x)} q(x)d x\\ & =\mathbb{E}_q\left[\log \frac{q(x)}{p(x)}\right] \end{aligned} DKL(q(x)∥p(x))=∫q(x)logp(x)q(x)dx=∫logp(x)q(x)q(x)dx=Eq[logp(x)q(x)]
积分改写为期望, 下标 q q q 表示 x ∼ q ( x ) x \sim q(x) x∼q(x)
因此,通过变分推断使模型分布 p θ ( x 0 ) p_\theta\left(\mathbf{x}_0\right) pθ(x0) 尽可能接近真实分布 q ( x 0 ) q\left(\mathbf{x}_0\right) q(x0) ,最小化两个分布的 KL 散度,因此 DDPM 的训练目标就从最大化对数似然切换到最小化 KL 散度,如下:
θ = arg min θ D K L ( q ( x 0 ) ∥ p θ ( x 0 ) ) \theta=\underset{\theta}{\arg \min } \mathcal{D}_{K L}\left(q\left(\mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_0\right)\right) θ=θargminDKL(q(x0)∥pθ(x0))
但是现在真实分布 q ( x 0 ) q\left(\mathbf{x}_0\right) q(x0)是未知的,因此人为定义了前向过程 q ( x t ∣ x t − 1 ) q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) q(xt∣xt−1),而前向过程的目的是构建一个从数据 x 0 \mathbf{x}_0 x0 到噪声 x T \mathbf{x}_T xT 的路径,使得数据逐步被加噪直至完全变成高斯噪声。通过这个过程,我们可以从任意时刻 t t t 的噪声状态 x t \mathbf{x}_t xt 推导出其在初始数据 x 0 \mathbf{x}_0 x0 上的后验分布 q ( x t − 1 ∣ x t , x 0 ) q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) q(xt−1∣xt,x0) 。然后,我们训练一个模型 p θ ( x t − 1 ∣ x t ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right) pθ(xt−1∣xt) 来近似这个后验分布。因此前向过程相当于一个中间桥梁,让我们可以让 p θ ( x 0 ) p_\theta\left(\mathbf{x}_0\right) pθ(x0)去接近 q ( x 0 ) q\left(\mathbf{x}_0\right) q(x0)。
后验分布是已知的,代入即可
θ = arg min θ D K L ( q ( x 1 : T ∣ x 0 ) ∥ p θ ( x 1 : T ∣ x 0 ) ) \theta=\underset{\theta}{\arg \min } \mathcal{D}_{K L}\left(q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)\right) θ=θargminDKL(q(x1:T∣x0)∥pθ(x1:T∣x0))
这样就有:
D K L ( q ( x 1 : T ∣ x 0 ) ∥ p θ ( x 1 : T ∣ x 0 ) ) = E q [ log q ( x 1 : T ∣ x 0 ) p θ ( x 1 : T ∣ x 0 ) ] = E q [ log q ( x 1 : T ∣ x 0 ) p θ ( x 0 ) p θ ( x 0 : T ) ] = E q [ log q ( x 1 : T ∣ x 0 ) p θ ( x 0 : T ) + log p θ ( x 0 ) ] = E q [ log q ( x 1 : T ∣ x 0 ) p θ ( x 0 : T ) ] + E q [ log p θ ( x 0 ) ] \begin{array}{rlrl} \mathcal{D}_{K L}(q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) )& =\mathbb{E}_q\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}{p_\theta\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}\right] & \\ & =\mathbb{E}_q\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) p_{\theta}(\mathbf{x}_0)}{p_\theta(\mathbf{x}_{0: T})}\right] & & \\ & =\mathbb{E}_q\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}{p_\theta(\mathbf{x}_{0: T})}+\log p_{\theta}(\mathbf{x}_0)\right] & \\ & =\mathbb{E}_q\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}{p_\theta(\mathbf{x}_{0: T})}\right]+\mathbb{E}_q[\log p_{\theta}(\mathbf{x}_0)] & \ \end{array} DKL(q(x1:T∣x0)∥pθ(x1:T∣x0))=Eq[logpθ(x1:T∣x0)q(x1:T∣x0)]=Eq[logpθ(x0:T)q(x1:T∣x0)pθ(x0)]=Eq[logpθ(x0:T)q(x1:T∣x0)+logpθ(x0)]=Eq[logpθ(x0:T)q(x1:T∣x0)]+Eq[logpθ(x0)]
期望下标 q q q表示 x 1 : T ∼ q ( x 1 : T ∣ x 0 ) \mathbf{x}_{1: T} \sim q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) x1:T∼q(x1:T∣x0)。
将等式两边重新整理有:
E q [ log p θ ( x 0 ) ] = D K L ( q ( x 1 : T ∣ x 0 ) ∥ p θ ( x 1 : T ∣ x 0 ) ) − E q [ log q ( x 1 : T ∣ x 0 ) p θ ( x 0 : T ) ] E q [ log p θ ( x 0 ) ] ≥ − E q [ log q ( x 1 : T ∣ x 0 ) p θ ( x 0 : T ) ] E q [ log p θ ( x 0 ) ] ≥ E q [ log p θ ( x 0 : T ) q ( x 1 : T ∣ x 0 ) ] E q [ − log p θ ( x 0 ) ] ≤ E q [ − log p θ ( x 0 : T ) q ( x 1 : T ∣ x 0 ) ] \begin{aligned} & \mathbb{E}_q[\log p_{\theta}(\mathbf{x}_0)]=\mathcal{D}_{K L}(q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right) )-\mathbb{E}_q\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}{p_\theta(\mathbf{x}_{0: T})}\right] \\ & \mathbb{E}_q[\log p_{\theta}(\mathbf{x}_0)] \geq-\mathbb{E}_q\left[\log \frac{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}{p_\theta(\mathbf{x}_{0: T})}\right] \\ & \mathbb{E}_q[\log p_{\theta}(\mathbf{x}_0)] \geq \mathbb{E}_q\left[\log \frac{p_\theta(\mathbf{x}_{0: T})}{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}\right] \\ & \mathbb{E}_q[-\log p_{\theta}(\mathbf{x}_0)] \leq \mathbb{E}_q\left[-\log \frac{p_\theta(\mathbf{x}_{0: T})}{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}\right]\\ \end{aligned} Eq[logpθ(x0)]=DKL(q(x1:T∣x0)∥pθ(x1:T∣x0))−Eq[logpθ(x0:T)q(x1:T∣x0)]Eq[logpθ(x0)]≥−Eq[logpθ(x0:T)q(x1:T∣x0)]Eq[logpθ(x0)]≥Eq[logq(x1:T∣x0)pθ(x0:T)]Eq[−logpθ(x0)]≤Eq[−logq(x1:T∣x0)pθ(x0:T)]
将式(1)式(2)代入得:
E q [ − log p θ ( x 0 ) ] ≤ E q [ − log p ( x T ) ∏ t = 1 T p θ ( x t − 1 ∣ x t ) ∏ t = 1 T q ( x t ∣ x t − 1 ) ] = E q [ − log p ( x T ) − log ∏ t = 1 T p θ ( x t − 1 ∣ x t ) q ( x t ∣ x t − 1 ) ] = E q [ − log p ( x T ) − ∑ t ≥ 1 log p θ ( x t − 1 ∣ x t ) q ( x t ∣ x t − 1 ) ] \begin{aligned} \mathbb{E}_q\left[-\log p_\theta\left(\mathbf{x}_0\right)\right] & \leq \mathbb{E}_q\left[-\log \frac{p\left(\mathbf{x}_T\right) \prod_{t=1}^T p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{\prod_{t=1}^T q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)}\right] \\ &= \mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right) - \log \prod_{t=1}^T \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t \geq 1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)}\right] \end{aligned} Eq[−logpθ(x0)]≤Eq[−log∏t=1Tq(xt∣xt−1)p(xT)∏t=1Tpθ(xt−1∣xt)]=Eq[−logp(xT)−logt=1∏Tq(xt∣xt−1)pθ(xt−1∣xt)]=Eq[−logp(xT)−t≥1∑logq(xt∣xt−1)pθ(xt−1∣xt)]
在不等式右侧,把负号提出,在数学上被称为变分下界 (VLB) (或称证据下界 ELBO),由于两边取负,不等式符号反转,因此在这里就成为变分上界,统称变分界。
就这样,我们推出了式(3),从而得出结论:最大化对数似然等价于最小化变分上界。即:
θ = arg min θ L \theta=\underset{\theta}{\arg \min } L θ=θargminL
在式(3)中,我们需要考虑从初始状态 x 0 \mathbf{x}_0 x0 到最终状态 x T \mathbf{x}_T xT 的所有时间步 t t t ,并对所有时间步的所有中间状态进行联合期望计算。这种联合期望会包含所有时间步的误差,导致较高的方差。
因此需要降低方差:
L = E q [ − log p θ ( x 0 : T ) q ( x 1 : T ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t ≥ 1 log p θ ( x t − 1 ∣ x t ) q ( x t ∣ x t − 1 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t ∣ x t − 1 ) − log p θ ( x 0 ∣ x 1 ) q ( x 1 ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t ∣ x t − 1 , x 0 ) − log p θ ( x 0 ∣ x 1 ) q ( x 1 ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 , x 0 ) q ( x t , x t − 1 , x 0 ) − log p θ ( x 0 ∣ x 1 ) q ( x 1 ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 , x 0 ) q ( x t − 1 , ∣ x t , x 0 ) q ( x t , x 0 ) − log p θ ( x 0 ∣ x 1 ) q ( x 1 ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 , ∣ x 0 ) q ( x 0 ) q ( x t − 1 , ∣ x t , x 0 ) q ( x t , ∣ x 0 ) q ( x 0 ) − log p θ ( x 0 ∣ x 1 ) q ( x 1 ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 ∣ x t , x 0 ) ⋅ q ( x t − 1 ∣ x 0 ) q ( x t ∣ x 0 ) − log p θ ( x 0 ∣ x 1 ) q ( x 1 ∣ x 0 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 ∣ x t , x 0 ) − ∑ t > 1 log q ( x t − 1 ∣ x 0 ) + ∑ t > 1 log q ( x t ∣ x 0 ) + log q ( x 1 ∣ x 0 ) − log p θ ( x 0 ∣ x 1 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 ∣ x t , x 0 ) − ∑ t > 1 log q ( x t − 1 ∣ x 0 ) + ∑ t ≥ 1 log q ( x t ∣ x 0 ) − log p θ ( x 0 ∣ x 1 ) ] = E q [ − log p ( x T ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 ∣ x t , x 0 ) + log q ( x T ∣ x 0 ) − log p θ ( x 0 ∣ x 1 ) ] = E q [ − log p ( x T ) q ( x T ∣ x 0 ) − ∑ t > 1 log p θ ( x t − 1 ∣ x t ) q ( x t − 1 ∣ x t , x 0 ) − log p θ ( x 0 ∣ x 1 ) ] = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) − log p θ ( x 0 ∣ x 1 ) ] \begin{aligned} L & =\mathbb{E}_q\left[-\log \frac{p_\theta\left(\mathbf{x}_{0: T}\right)}{q\left(\mathbf{x}_{1: T} \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t \geq 1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)}-\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_{t} \mid \mathbf{x}_{t-1}, \mathbf{x}_0\right)} -\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t \right)q\left(\mathbf{x}_{t-1}, \mathbf{x}_0\right)}{q\left(\mathbf{x}_{t} ,\mathbf{x}_{t-1}, \mathbf{x}_0\right)} -\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t \right)q\left(\mathbf{x}_{t-1}, \mathbf{x}_0\right)}{q\left(\mathbf{x}_{t-1} , \mid \mathbf{x}_{t}, \mathbf{x}_0\right)q\left(\mathbf{x}_t,\mathbf{x}_0\right)} -\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t \right)q\left(\mathbf{x}_{t-1}, \mid \mathbf{x}_0\right)q\left(\mathbf{x}_0\right)}{q\left(\mathbf{x}_{t-1} , \mid \mathbf{x}_{t}, \mathbf{x}_0\right)q\left(\mathbf{x}_t, \mid \mathbf{x}_0\right)q\left(\mathbf{x}_0\right)} -\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right)} \cdot \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)}-\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)}\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right)} -\sum_{t>1} \log q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)+\sum_{t>1} \log q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)+\log q\left(\mathbf{x}_1 \mid \mathbf{x}_0\right)-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right)} -\sum_{t>1} \log q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)+\sum_{t \geq 1} \log q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right] \\ & =\mathbb{E}_q\left[-\log p\left(\mathbf{x}_T\right)-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right)} +\log q\left(\mathbf{x}_T \mid \mathbf{x}_0\right)-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right] \\ & =\mathbb{E}_q\left[-\log \frac{p\left(\mathbf{x}_T\right)}{q\left(\mathbf{x}_T \mid \mathbf{x}_0\right)}-\sum_{t>1} \log \frac{p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)}{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right)}-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right]\\ &=\mathbb{E}_q\left[D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)+\sum_{t>1} D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right] \end{aligned} L=Eq[−logq(x1:T∣x0)pθ(x0:T)]=Eq[−logp(xT)−t≥1∑logq(xt∣xt−1)pθ(xt−1∣xt)]=Eq[−logp(xT)−t>1∑logq(xt∣xt−1)pθ(xt−1∣xt)−logq(x1∣x0)pθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt∣xt−1,x0)pθ(xt−1∣xt)−logq(x1∣x0)pθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt,xt−1,x0)pθ(xt−1∣xt)q(xt−1,x0)−logq(x1∣x0)pθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt−1,∣xt,x0)q(xt,x0)pθ(xt−1∣xt)q(xt−1,x0)−logq(x1∣x0)pθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt−1,∣xt,x0)q(xt,∣x0)q(x0)pθ(xt−1∣xt)q(xt−1,∣x0)q(x0)−logq(x1∣x0)pθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt−1∣xt,x0)pθ(xt−1∣xt)⋅q(xt∣x0)q(xt−1∣x0)−logq(x1∣x0)pθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt−1∣xt,x0)pθ(xt−1∣xt)−t>1∑logq(xt−1∣x0)+t>1∑logq(xt∣x0)+logq(x1∣x0)−logpθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt−1∣xt,x0)pθ(xt−1∣xt)−t>1∑logq(xt−1∣x0)+t≥1∑logq(xt∣x0)−logpθ(x0∣x1)]=Eq[−logp(xT)−t>1∑logq(xt−1∣xt,x0)pθ(xt−1∣xt)+logq(xT∣x0)−logpθ(x0∣x1)]=Eq[−logq(xT∣x0)p(xT)−t>1∑logq(xt−1∣xt,x0)pθ(xt−1∣xt)−logpθ(x0∣x1)]=Eq[DKL(q(xT∣x0)∥p(xT))+t>1∑DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))−logpθ(x0∣x1)]
现在可以得到 L L L:
L = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) − log p θ ( x 0 ∣ x 1 ) ] L=\mathbb{E}_q\left[D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)+\sum_{t>1} D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right] L=Eq[DKL(q(xT∣x0)∥p(xT))+t>1∑DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))−logpθ(x0∣x1)]
就这样,推出了式(5),式(5)将式(3)分解为多个时间步的KL散度项,每个项只对应特定时间步的损失。
- L T L_T LT : 这个项衡量的是从真实数据到最终噪声状态的KL散度,即最后一步的误差。
- L t − 1 L_{t-1} Lt−1 : 每个这样的项衡量的是从一个中间状态 x t \mathbf{x}_t xt 到前一个状态 x t − 1 \mathbf{x}_{t-1} xt−1 的KL散度。具体来说, 它衡量的是模型生成的分布 p θ ( x t − 1 ∣ x t ) p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right) pθ(xt−1∣xt) 与真实的后验分布 q ( x t − 1 ∣ x t , x 0 ) q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) q(xt−1∣xt,x0) 之间的差异。
- L 0 L_0 L0 : 这个项衡量的是从初始数据 x 0 \mathbf{x}_0 x0 到第一个中间状态 x 1 \mathbf{x}_1 x1 的对数概率。
继续化简:
L = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) − log p θ ( x 0 ∣ x 1 ) ] = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) − log p θ ( x 0 ∣ x 1 ) q ( x 0 ∣ x 1 , x 0 ) ⋅ q ( x 0 ∣ x 1 , x 0 ) ] = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) + log q ( x 0 ∣ x 1 , x 0 ) p θ ( x 0 ∣ x 1 ) − log q ( x 0 ∣ x 1 , x 0 ) ] = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) + ∑ t > 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) + D K L ( q ( x 0 ∣ x 1 , x 0 ) ∥ p θ ( x 0 ∣ x 1 ) ) − log q ( x 0 ∣ x 1 , x 0 ) ] = E q [ D K L ( q ( x T ∣ x 0 ) ∥ p ( x T ) ) ⏟ 与 θ 无关项 + ∑ t ≥ 1 D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) ⏟ L t − log q ( x 0 ∣ x 1 , x 0 ) ⏟ 与 θ 无关项 ] \begin{aligned} L&=\mathbb{E}_q\left[D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)+\sum_{t>1} D_{\mathrm{KL}}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)-\log p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right]\\ &=\mathbb{E}_q\left[D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)+\sum_{t>1} \mathcal{D}_{K L}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)-\log \frac{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}{q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right)} \cdot q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right)\right]\\ &=\mathbb{E}_q\left[D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)+\sum_{t>1} \mathcal{D}_{K L}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)+\log \frac{q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right)}{p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)}-\log q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right)\right]\\ &=\mathbb{E}_q\left[D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)+\sum_{t>1} \mathcal{D}_{K L}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)+\mathcal{D}_{K L}\left(q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_0 \mid \mathbf{x}_1\right)\right)-\log q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right)\right]\\ &=\mathbb{E}_q\left[\underbrace{D_{\mathrm{KL}}\left(q\left(\mathbf{x}_T \mid \mathbf{x}_0\right) \| p\left(\mathbf{x}_T\right)\right)}_{\text {与 } \theta \text { 无关项 }}+\sum_{t \geq 1} \underbrace{\mathcal{D}_{K L}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)}_{L_t}-\underbrace{\log q\left(\mathbf{x}_0 \mid \mathbf{x}_1, \mathbf{x}_0\right)}_{\text {与 } \theta \text { 无关项 }}\right]\\ \end{aligned} L=Eq[DKL(q(xT∣x0)∥p(xT))+t>1∑DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))−logpθ(x0∣x1)]=Eq[DKL(q(xT∣x0)∥p(xT))+t>1∑DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))−logq(x0∣x1,x0)pθ(x0∣x1)⋅q(x0∣x1,x0)]=Eq[DKL(q(xT∣x0)∥p(xT))+t>1∑DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))+logpθ(x0∣x1)q(x0∣x1,x0)−logq(x0∣x1,x0)]=Eq[DKL(q(xT∣x0)∥p(xT))+t>1∑DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))+DKL(q(x0∣x1,x0)∥pθ(x0∣x1))−logq(x0∣x1,x0)]=Eq 与 θ 无关项 DKL(q(xT∣x0)∥p(xT))+t≥1∑Lt DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))−与 θ 无关项 logq(x0∣x1,x0)
与参数 θ \theta θ 无关梯度为0,因此不作为训练目标。这样 L t − 1 L_{t-1} Lt−1 与 L 0 L_0 L0 合并为 L t L_t Lt 就有:
L t = E q [ D K L ( q ( x t − 1 ∣ x t , x 0 ) ∥ p θ ( x t − 1 ∣ x t ) ) ] L_t=\mathbb{E}_q\left[\mathcal{D}_{K L}\left(q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) \| p_\theta\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t\right)\right)\right] Lt=Eq[DKL(q(xt−1∣xt,x0)∥pθ(xt−1∣xt))]
最终模型目标就是最小化 L t L_t Lt ,即:
θ = arg min θ L t \theta=\underset{\theta}{\arg \min } L_t θ=θargminLt
这篇关于DDPM公式推导(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!