高斯混合模型GMM
高斯分布包括一元高斯分布和多元高斯分布
高斯混合模型指多个高斯分布的组合
高斯混合分布涉及的参数: 1. 高斯分布中x的维度, 2. 多少个高斯分布
利用高斯混合模型,为某个类别生成数据 1. 选择一个类别c, 选中第c个高斯分布N(\mu_c, \sigma^2_c) 2. 用该多元高斯分布N(\mu_c, \sigma^2_c)采样生成潜在变量z, 计算该潜在变量属于该类别的后验概率, 若概率不够大,可以重复此步骤 3. 使用z带入到生成模型(解码器)中得到样本x, 该样本属于类别c的概率即步骤二中的后验概率
z_mean, z_logvar = model.encode(x)
z = model.reparameterize(z_mean, z_logvar)
获取vae的隐变量z, vae的隐变量z是高斯混合模型中的x, 计算隐变量z属于各个类的概率
p_c_z = tf.exp(tf.reduce_sum(tf.log(theta_p)
- 0.5 * tf.log(2 * np.pi * c_var)
- tf.square(z - c_mean)/(2 * c_var),
axis=1)
) + 1e-10 # (batch_size, n_centroid)
q_c_x = p_c_z/tf.reduce_sum(p_c_z, axis=-1, keepdims=True) #(batch_size, n_centroid)
p_c_z的计算, 将z带入到高斯混合模型中, 高斯混合模型中的每个多元高斯分布这里是按照各维度不相关进行计算的(变成概率乘积,在exp里变成求和), 把theta_p即高斯混合模型中每个高斯分布前的系数提到exp里进行计算。
\begin{aligned}
f(x) &=\theta*\frac{1}{\sqrt{{2\pi}}\times\sigma}e^{\frac{1}{2}(\frac{x-\mu}{\sigma})^2} \\
&= e^{\Big[log\theta - log(\sqrt{{2\pi}}\times\sigma) - \frac{1}{2}(\frac{x-\mu}{\sigma})^2 \Big]}
\end{aligned}
- q_c_x: shape为(batch_size, n_centroid), 每个样本x属于各个类的概率,
- z: shape为(batch_size, latent_dim, n_centroid)
- c_mean: shape为(batch_size, latent_dim, n_centroid)
多元高斯分布推导和计算: https://zhuanlan.zhihu.com/p/36522776
vae
\begin{aligned}
ELBO &=\mathcal{L} \\
&= E_{q(z|x^{(i)})} \big[ \log p(x^{(i)} \vert z) \big] \\
&+ E_{q(z|x^{(i)})} \big[ \log p(z) \big] \\
&- E_{q(z|x^{(i)})} \big[ \log q(z |x^{(i)}) \big]
\end{aligned}
vade
\begin{aligned}
ELBO &=\mathcal{L} \\
&= E_{q(z,c|x^{(i)})} \big[ \log p(x^{(i)} \vert z) \big] \\
&+ E_{q(z,c|x^{(i)})} \big[ \log p(z|c) \big] \\
&+ E_{q(z,c|x^{(i)})} \big[ \log p(c) \big] \\
&- E_{q(z,c|x^{(i)})} \big[ \log q(z|x^{(i)}) \big] \\
&- E_{q(z,c|x^{(i)})} \big[ \log q(c|x^{(i)}) \big] \\
\end{aligned}
- q(c|x^{(i)}) 求解
q(c|x) = E_{q(z|x^{(i)})}[p(c|z])]
-
E_{q(z,c|x^{(i)})} \big[ \log p(z|c) \big]求解
\begin{aligned} E_{q(z,c|x^{(i)})} \big[ \log p(z|c) \big] &= \int \sum_{c=1}^{K} q(c|x)q(z|x) \log p(z|c)dz \\ &= \sum_{c=1}^{K} [q(c|x) \int q(z|x) \log p(z|c)dz] \\ &= \end{aligned} -
E_{q(z,c|x^{(i)})} \big[ \log p(c) \big]求解
\begin{aligned} E_{q(z,c|x^{(i)})} \big[ \log p(z|c) \big] &= \int \sum_{c=1}^{K} q(z|x)q(c|x) \log p(c)dz \\ &= \int q(z|x) \sum_{c=1}^{K} \big[q(c|x) \log p(c)]dz \\ &= \sum_{c=1}^{K} \big[q(c|x) \log p(c)] \end{aligned}