变分下界
公式推导
我们的目的是最大化 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)=Ez∼qϕ(z∣x)[logpθ(x)];见最后=Ez∼qϕ(z∣x)[logpθ(z∣x)pθ(x,z)]=Ez∼qϕ(z∣x)[logpθ(z∣x)qϕ(z∣x)pθ(x,z)qϕ(z∣x)]=Ez∼qϕ(z∣x)[logqϕ(z∣x)pθ(x,z)]+Ez∼qϕ(z∣x)[logpθ(z∣x)qϕ(z∣x)]=L(θ,ϕ;x)+KL(qϕ(z∣x)∥pθ(z∣x))≥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ϕ(z∣x)∣∣pθ(z∣x))≥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θ(z∣x) 和
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θ(z∣x)≈q(z∣x)≈K1k=1∑Kq(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(z∣x) 中采样得到的随机向量,
K
K
K 是采样数量。
因此,通过最大化变分下界,我们可以使用一种近似的方式来求解无法直接计算的 log p θ ( x ) \log p_\theta(\mathbf{x}) logpθ(x) 和 p θ ( z ∣ x ) p_\theta(\mathbf{z} \mid \mathbf{x}) pθ(z∣x)。
在实践中,一般使用等价的形式来计算变分下界,即:
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ϕ(z∣x)∥pθ(z∣x))−Eqϕ(z∣x)[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} Ez∼qϕ(z∣x)[logpθ(x)]=∫logpθ(x)qϕ(z∣x)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})
Ez∼qϕ(z∣x)[logpθ(x)]=logpθ(x)∫qϕ(z∣x)dz=logpθ(x)
这里用到了
∫
q
ϕ
(
z
∣
x
)
d
z
=
1
\int q_\phi(\mathbf{z} \mid \mathbf{x}) d\mathbf{z} = 1
∫qϕ(z∣x)dz=1,即
q
ϕ
(
z
∣
x
)
q_\phi(\mathbf{z} \mid \mathbf{x})
qϕ(z∣x) 是一个概率密度函数的性质。因此,右边等于
log
p
θ
(
x
)
\log p_\theta(\mathbf{x})
logpθ(x)。