变分推断公式推导

文章介绍了变分下界作为优化生成概率模型的一种方法,通过最大化变分下界来近似最大化给定数据的生成概率。这种方法涉及到变分推断,利用KL散度和期望运算,可以处理不易直接计算的概率分布。在实践中,通常采用梯度上升策略优化变分下界,从而估计难以直接计算的模型参数和概率分布。
摘要由CSDN通过智能技术生成

变分下界

公式推导

我们的目的是最大化 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x),即最大化给定数据 x \mathbf{x} x 的生成概率。然而,由于 p θ ( x ) p_\theta(\mathbf{x}) pθ(x) 往往难以直接计算,我们可以通过最大化变分下界 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x) 来近似地最大化 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x)。因此,我们的最终目标是最大化变分下界 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x),以此来间接地最大化 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x)

变分下界 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x) 是对 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x) 的下界估计,即 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x) 的值不小于 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x)。这可以通过变分下界的定义式:
log ⁡ p θ ( x ) = E z ∼ q ϕ ( z ∣ x ) [ log ⁡ p θ ( x ) ] ; 见 最 后 = E z ∼ q ϕ ( z ∣ x ) [ log ⁡ p θ ( x , z ) p θ ( z ∣ x ) ] = E z ∼ q ϕ ( z ∣ x ) [ log ⁡ p θ ( x , z ) q ϕ ( z ∣ x ) p θ ( z ∣ x ) q ϕ ( z ∣ x ) ] = E z ∼ q ϕ ( z ∣ x ) [ log ⁡ p θ ( x , z ) q ϕ ( z ∣ x ) ] + E z ∼ q ϕ ( z ∣ x ) [ log ⁡ q ϕ ( z ∣ x ) p θ ( z ∣ x ) ] = L ( θ , ϕ ; x ) + K L ( q ϕ ( z ∣ x ) ∥ p θ ( z ∣ x ) ) ≥ L ( θ , ϕ ; x ) \begin{aligned} & \log p_\theta(\mathbf{x})=\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \mid \mathbf{x})}\left[\log p_\theta(\mathbf{x})\right];见最后\\ & =\mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \mid \mathbf{x})}\left[\log \frac{p_\theta(\mathbf{x}, \mathbf{z})}{p_\theta(\mathbf{z} \mid \mathbf{x})}\right] \\ & =\mathbb{E}_{\mathbf{z} \sim q \phi(\mathbf{z} \mid \mathbf{x})}\left[\log \frac{p_\theta(\mathbf{x}, \mathbf{z}) q_\phi(\mathbf{z} \mid \mathbf{x})}{p_\theta(\mathbf{z} \mid \mathbf{x}) q_\phi(\mathbf{z} \mid \mathbf{x})}\right] \\ & =\mathbb{E}_{\mathbf{z} \sim q \phi(\mathbf{z} \mid \mathbf{x})}\left[\log \frac{p_\theta(\mathbf{x}, \mathbf{z})}{q_\phi(\mathbf{z} \mid \mathbf{x})}\right]+\mathbb{E}_{\mathbf{z} \sim q \phi(\mathbf{z} \mid \mathbf{x})}\left[\log \frac{q_\phi(\mathbf{z} \mid \mathbf{x})}{p_\theta(\mathbf{z} \mid \mathbf{x})}\right] \\ & =\mathcal{L}(\theta, \phi ; \mathbf{x})+\mathrm{KL}\left(q_\phi(\mathbf{z} \mid \mathbf{x}) \| p_\theta(\mathbf{z} \mid \mathbf{x})\right) \\ & \geq \mathcal{L}(\theta, \phi ; \mathbf{x}) \\ & \end{aligned} logpθ(x)=Ezqϕ(zx)[logpθ(x)]=Ezqϕ(zx)[logpθ(zx)pθ(x,z)]=Ezqϕ(zx)[logpθ(zx)qϕ(zx)pθ(x,z)qϕ(zx)]=Ezqϕ(zx)[logqϕ(zx)pθ(x,z)]+Ezqϕ(zx)[logpθ(zx)qϕ(zx)]=L(θ,ϕ;x)+KL(qϕ(zx)pθ(zx))L(θ,ϕ;x)
其中,我们用到了变分下界的定义式,以及 KL 散度的非负性质 KL ( q ϕ ( z ∣ x ) ∣ ∣ p θ ( z ∣ x ) ) ≥ 0 \text{KL}\left(q_\phi(\mathbf{z} \mid \mathbf{x}) || p_\theta(\mathbf{z} \mid \mathbf{x})\right) \geq 0 KL(qϕ(zx)pθ(zx))0,进而得到了 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x) 与变分下界 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x) 的关系。

通过最大化 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x),我们可以得到一个近似的最优解 ( θ ^ , ϕ ^ ) (\hat{\theta}, \hat{\phi}) (θ^,ϕ^),这个近似的最优解可以用来估计 p θ ( z ∣ x ) p_\theta(\mathbf{z} \mid \mathbf{x}) pθ(zx) log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x)。具体来说,我们可以通过以下的方式来估计这些量:
log ⁡ p θ ( x ) ≈ L ( θ ^ , ϕ ^ ; x ) p θ ( z ∣ x ) ≈ q ( z ∣ x ) ≈ 1 K ∑ k = 1 K q ( z ( k ) ∣ x ) \begin{gathered} \log p_\theta(\mathbf{x}) \approx \mathcal{L}(\hat{\theta}, \hat{\phi} ; \mathbf{x}) \\ p_\theta(\mathbf{z} \mid \mathbf{x}) \approx q(\mathbf{z} \mid \mathbf{x}) \approx \frac{1}{K} \sum_{k=1}^K q\left(\mathbf{z}^{(k)} \mid \mathbf{x}\right) \end{gathered} logpθ(x)L(θ^,ϕ^;x)pθ(zx)q(zx)K1k=1Kq(z(k)x)
其中 z ( 1 ) , z ( 2 ) , … , z ( K ) \mathbf{z}^{(1)}, \mathbf{z}^{(2)}, \ldots, \mathbf{z}^{(K)} z(1),z(2),,z(K) 是从 q ( z ∣ x ) q(\mathbf{z} \mid \mathbf{x}) q(zx) 中采样得到的随机向量, K K K 是采样数量。

因此,通过最大化变分下界,我们可以使用一种近似的方式来求解无法直接计算的 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x) p θ ( z ∣ x ) p_\theta(\mathbf{z} \mid \mathbf{x}) pθ(zx)

在实践中,一般使用等价的形式来计算变分下界,即:
L ( θ , ϕ ; x ) = K L ( q ϕ ( z ∣ x ) ∥ p θ ( z ∣ x ) ) − E q ϕ ( z ∣ x ) [ log ⁡ p θ ( x , z ) ] \mathcal{L}(\theta, \phi ; \mathbf{x})=\mathrm{KL}\left(q_\phi(\mathbf{z} \mid \mathbf{x}) \| p_\theta(\mathbf{z} \mid \mathbf{x})\right)-\mathbb{E}_{q_\phi(\mathbf{z} \mid \mathbf{x})}\left[\log p_\theta(\mathbf{x}, \mathbf{z})\right] L(θ,ϕ;x)=KL(qϕ(zx)pθ(zx))Eqϕ(zx)[logpθ(x,z)]
其中,第一项是 KL \text{KL} KL 散度。第二项是期望,可以使用蒙特卡罗方法(如重参数化技巧)来估计。这样就可以通过对 L ( θ , ϕ ; x ) \mathcal{L}(\theta, \phi; \mathbf{x}) L(θ,ϕ;x) 进行梯度上升来最大化该下界,从而近似最大化 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x)

公式说明

E z ∼ q ϕ ( z ∣ x ) [ log ⁡ p θ ( x ) ] = ∫ log ⁡ p θ ( x ) q ϕ ( z ∣ x ) d z \mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \mid \mathbf{x})}\left[\log p_\theta(\mathbf{x})\right]=\int \log p_\theta(\mathbf{x}) q_\phi(\mathbf{z} \mid \mathbf{x}) d \mathbf{z} Ezqϕ(zx)[logpθ(x)]=logpθ(x)qϕ(zx)dz

由于 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x) 不依赖于 z \mathbf{z} z,所以可以将其提取出来,得到:
E z ∼ q ϕ ( z ∣ x ) [ log ⁡ p θ ( x ) ] = log ⁡ p θ ( x ) ∫ q ϕ ( z ∣ x ) d z = log ⁡ p θ ( x ) \mathbb{E}_{\mathbf{z} \sim q_\phi(\mathbf{z} \mid \mathbf{x})}\left[\log p_\theta(\mathbf{x})\right]=\log p_\theta(\mathbf{x}) \int q_\phi(\mathbf{z} \mid \mathbf{x}) d \mathbf{z}=\log p_\theta(\mathbf{x}) Ezqϕ(zx)[logpθ(x)]=logpθ(x)qϕ(zx)dz=logpθ(x)
这里用到了 ∫ q ϕ ( z ∣ x ) d z = 1 \int q_\phi(\mathbf{z} \mid \mathbf{x}) d\mathbf{z} = 1 qϕ(zx)dz=1,即 q ϕ ( z ∣ x ) q_\phi(\mathbf{z} \mid \mathbf{x}) qϕ(zx) 是一个概率密度函数的性质。因此,右边等于 log ⁡ p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值