Preface: Since elementary school, I have loved pure mathematics. Later, I developed a passion for physics and studied theoretical physics for a period of time. It wasn’t until after my undergraduate graduation that I gradually entered the field of machine learning. Therefore, even in the field of machine learning, my research habits still retain the style of mathematics and physics: attempting to understand and derive as much as possible from the fewest principles. This article is one of the results of this philosophy, attempting to use variational inference as a starting point to unify the understanding of various models in deep learning, especially the dazzling array of GANs. This paper has been posted on arXiv; those who need to read the original English manuscript can move to “Variational Inference: A Unified Framework of Generative Models and Some Revelations”.
Below is the introduction to the article. In fact, the information in the Chinese version might be slightly richer than the English version—please forgive my clumsy English...
Abstract: This paper describes variational inference from a new perspective and proves that the EM algorithm, VAE, GAN, AAE, and ALI (BiGAN) can all be regarded as special cases of variational inference. Furthermore, the paper demonstrates that the optimization objective of the standard GAN is incomplete, which explains why GAN training requires careful selection of various hyperparameters. Finally, the paper provides a regularization term that can improve this incompleteness. Experiments show that this regularization term can enhance the stability of GAN training.
In recent years, deep generative models, especially GANs, have achieved great success. We can now find dozens or even hundreds of GAN variants. However, most of them are improved based on empirical evidence, with little complete theoretical guidance.
The goal of this paper is to establish a unified framework for these generative models through variational inference. First, this paper introduces a new form of variational inference. This new form has actually been introduced in previous blog posts; it allows us to derive Variational Autoencoders (VAE) and the EM algorithm within a few lines. Then, using this new form, we can directly derive GANs and discover that the loss of the standard GAN is actually incomplete, missing a regularization term. Without this regularization term, we need to carefully adjust hyperparameters to make the model converge.
In fact, the original intention of this work was to incorporate GANs into the framework of variational inference. It seems that the initial intention has been achieved, and the results are gratifying. The newly derived regularization term is actually a byproduct, and fortunately, this byproduct worked in our experiments.
A New Interpretation of Variational Inference
Assume x is the observed variable, z is the latent variable, \tilde{p}(x) is the evidence distribution of x, and we have: q(x)=q_{\theta}(x)=\int q_{\theta}(x,z)dz We hope q_{\theta}(x) can approximate \tilde{p}(x), so in general, we maximize the likelihood function: \theta = \mathop{\text{argmax}}_{\theta}\, \int \tilde{p}(x)\log q(x) dx This is also equivalent to minimizing the KL divergence KL(\tilde{p}(x)\Vert q(x)): KL(\tilde{p}(x)\Vert q(x)) = \int \tilde{p}(x) \log \frac{\tilde{p}(x)}{q(x)}dx However, because the integral may be difficult to calculate, it is difficult to optimize directly in most cases.
In variational inference, we first introduce a joint distribution p(x,z) such that \tilde{p}(x)=\int p(x,z)dz. The essence of variational inference is to change the KL divergence of the marginal distribution KL(\tilde{p}(x)\Vert q(x)) to the KL divergence of the joint distribution KL(p(x,z)\Vert q(x,z)) or KL(q(x,z)\Vert p(x,z)). Since: \begin{aligned} KL(p(x,z)\Vert q(x,z)) &= KL(\tilde{p}(x)\Vert q(x)) + \int \tilde{p}(x) KL(p(z|x)\Vert q(z|x)) dx\\ &\geq KL(\tilde{p}(x)\Vert q(x)) \end{aligned} it means that the KL divergence of the joint distribution is a stronger condition (upper bound). Therefore, once the optimization is successful, we get q(x,z)\to p(x,z), and thus \int q(x,z)dz \to \int p(x,z)dz = \tilde{p}(x), meaning \int q(x,z)dz becomes an approximation of the true distribution \tilde{p}(x).
Of course, we are not strengthening the condition for the sake of strengthening it, but because in many cases, KL(p(x,z)\Vert q(x,z)) or KL(q(x,z)\Vert p(x,z)) is often easier to calculate than KL(\tilde{p}(x)\Vert q(x)). Thus, variational inference provides a computable scheme.
VAE and the EM Algorithm
Based on the above new understanding of variational inference, we can derive two basic results within a few sentences: the Variational Autoencoder and the EM algorithm. This content has been introduced in detail in “From Maximum Likelihood to EM Algorithm: A Consistent Way of Understanding” and “Variational Autoencoder (2): From a Bayesian Perspective”. Here, it is briefly mentioned again.
VAE
In VAE, we set q(x,z)=q(x|z)q(z) and p(x,z)=\tilde{p}(x) p(z|x), where q(x|z) and p(z|x) are Gaussian distributions with unknown parameters, and q(z) is a standard Gaussian distribution. The objective to minimize is: \label{eq:kl-oo} KL\left(p(x,z)\Vert q(x,z) \right)=\iint \tilde{p}(x) p(z|x) \log \frac{\tilde{p}(x) p(z|x)}{q(x|z)q(z)}dxdz Since \log \tilde{p}(x) does not contain optimization parameters, it can be regarded as a constant, and the integral over \tilde{p}(x) is transformed into sampling from the data, thus: \mathbb{E}_{x\sim \tilde{p}(x)}\left[-\int p(z|x)\log q(x|z)dz + KL(p(z|x)\Vert q(z))\right] Because q(x|z) and p(z|x) are Gaussian distributions parameterized by neural networks, KL\left(p(z|x)\Vert q(z)\right) can be calculated explicitly. By using the reparameterization trick to sample a point to estimate the integral \int p(z|x) \log q(x|z)dz, we obtain the final loss to be minimized for VAE: \mathbb{E}_{x\sim \tilde{p}(x)}\Big[-\log q(x|z) + KL(p(z|x)\Vert q(z))\Big]
EM Algorithm
In VAE, we imposed a constraint on the posterior distribution, assuming it is Gaussian, so we optimize the parameters of the Gaussian distribution. If we do not make this assumption, directly optimizing the original objective [eq:kl-oo] is also feasible in some cases, but it requires an alternating optimization approach: first fix p(z|x) and optimize q(x|z), then: \label{eq:em-1} q(x|z) = \mathop{\text{argmax}}_{q(x|z)} \,\mathbb{E}_{x\sim \tilde{p}(x)}\left[\int p(z|x) \log q(x,z) dz\right] After completing this step, we fix q(x,z) and optimize p(z|x). First, write q(x|z)q(z) in the form q(z|x)q(x): q(x)=\int q(x|z)q(z)dz,\quad q(z|x)=\frac{q(x|z)q(z)}{q(x)} Then we have: \begin{aligned} p(z|x) &= \mathop{\text{argmin}}_{p(z|x)} \,\mathbb{E}_{x\sim \tilde{p}(x)}\left[\int p(z|x) \log \frac{p(z|x)}{q(z|x)q(x)} dz\right]\\ &= \mathop{\text{argmin}}_{p(z|x)} \,\mathbb{E}_{x\sim \tilde{p}(x)}\left[KL\left(p(z|x)\Vert q(z|x)\right)-\log q(x)\right]\\ &= \mathop{\text{argmin}}_{p(z|x)} \,\mathbb{E}_{x\sim \tilde{p}(x)} \left[KL\left(p(z|x)\Vert q(z|x)\right)\right] \end{aligned} Since there are currently no constraints on p(z|x), we can directly set p(z|x)=q(z|x) to make the loss equal to 0. That is, p(z|x) has a theoretical optimal solution: \label{eq:em-2} p(z|x) = \frac{q(x|z)q(z)}{\int q(x|z)q(z)dz} The alternating execution of [eq:em-1] and [eq:em-2] constitutes the solution steps of the EM algorithm. Thus, we quickly obtain the EM algorithm from the variational inference framework.
GAN under Variational Inference
In this section, we introduce a generalized method for incorporating GANs into variational inference, which will lead us to a new understanding of GANs and an effective regularization term.
General Framework
Like VAE, GAN also hopes to train a generative model q(x|z) to map q(z)=N(z;0,I) to the dataset distribution \tilde{p}(x). Unlike VAE, which chooses q(x|z) as a Gaussian distribution, GAN’s choice is: q(x|z)=\delta\left(x - G(z)\right),\quad q(x)=\int q(x|z)q(z)dz where \delta(x) is the Dirac delta function and G(z) is the neural network of the generator.
Generally, we consider z as a latent variable, but since the \delta function actually implies a point distribution, the relationship between x and z can be considered one-to-one. Therefore, the relationship between z and x is already “not random enough”; in GAN, we do not consider it a latent variable (meaning we do not need to consider the posterior distribution p(z|x)).
In fact, in GAN, only a binary latent variable y is introduced to form the joint distribution: q(x,y)=\left\{\begin{aligned}&\tilde{p}(x)p_1,\,y=1\\&q(x)p_0,\,y=0\end{aligned}\right. Here p_1 = 1-p_0 describes a binary probability distribution; we directly take p_1=p_0=1/2. On the other hand, we set p(x,y)=p(y|x) \tilde{p}(x), where p(y|x) is a conditional Bernoulli distribution. The optimization objective is the KL\left(q(x,y)\Vert p(x,y) \right) in the other direction: \begin{aligned} KL\left(q(x,y)\Vert p(x,y) \right)&=\int \tilde{p}(x)p_1\log \frac{\tilde{p}(x)p_1}{p(1|x)\tilde{p}(x)}dx+\int q(x)p_0\log \frac{q(x)p_0}{p(0|x)\tilde{p}(x)}dx\\ &\sim\int \tilde{p}(x)\log \frac{1}{p(1|x)}dx+\int q(x)\log \frac{q(x)}{p(0|x)\tilde{p}(x)}dx \end{aligned} Once optimized successfully, we have q(x,y)\to p(x,y), then: p_1 \tilde{p}(x) + p_0 q(x) = \sum_y q(x,y) \to \sum_y p(x,y) = \tilde{p}(x) Thus q(x)\to\tilde{p}(x), completing the construction of the generative model.
Now our optimization targets are p(y|x) and G(x). Let p(1|x)=D(x), which is the discriminator. Similar to the EM algorithm, we perform alternating optimization: first fix G(z), which also means q(x) is fixed, and then optimize p(y|x). Omitting constants, we get the optimization objective: D = \mathop{\text{argmin}}_{D} -\mathbb{E}_{x\sim\tilde{p}(x)}\left[\log D(x)\right]-\mathbb{E}_{x\sim q(x)}\left[\log (1-D(x))\right] Then fix D(x) to optimize G(x). The relevant loss is: \label{eq:gan-g-loss} G = \mathop{\text{argmin}}_{G}\int q(x)\log \frac{q(x)}{(1-D(x)) \tilde{p}(x)}dx This contains \tilde{p}(x), which we do not know. However, if the D(x) model has sufficient fitting capacity, then similar to equation [eq:em-2], the optimal solution for D(x) should be: D(x)=\frac{\tilde{p}(x)}{\tilde{p}(x)+q^{o}(x)} where q^{o}(x) is the q(x) from the previous stage. Solving for \tilde{p}(x) and substituting it into [eq:gan-g-loss] gives: \begin{aligned} \int q(x)\log \frac{q(x)}{D(x) q^{o}(x)}dx&=-\mathbb{E}_{x\sim q(x)}\log D(x) + KL\left(q(x)\Vert q^{o}(x)\right)\\ &=-\mathbb{E}_{z\sim q(z)}\log D(G(z)) + KL\left(q(x)\Vert q^{o}(x)\right) \end{aligned}
Basic Analysis
As can be seen, the first term is one of the standard losses used by GAN generators: -\mathbb{E}_{z\sim q(z)}\log D(G(z)) The extra second term describes the distance between the new distribution and the old distribution. These two loss terms are adversarial because KL\left(q(x)\Vert q^{o}(x)\right) wants the new and old distributions to be as consistent as possible, but if the discriminator is fully optimized, for samples in the old distribution q^{o}(x), D(x) will be very small (almost all identified as negative samples), so -\log D(x) will be quite large, and vice versa. In this way, when the entire loss is optimized together, the model must both “inherit” the old distribution q^{o}(x) and explore in the new direction p(1|y), interpolating between the old and the new.
We know that currently, the standard GAN generator losses do not include KL\left(q(x)\Vert q^{o}(x)\right), which in fact causes the loss to be incomplete. Suppose there is an optimization algorithm that can always find the theoretical optimal solution for G(z), and G(z) has infinite fitting capacity. In that case, G(z) only needs to generate a single sample that maximizes D(x) (regardless of what the input z is); this is mode collapse. In this sense, theoretically, it will definitely happen.
So, what is the inspiration given to us by KL\left(q(x)\Vert q^{o}(x)\right)? Let: q^{o}(x)=q_{\theta-\Delta \theta}(x),\quad q(x)=q_{\theta}(x) That is, assuming the current model parameter change is \Delta\theta, then expanding to the second order gives: KL\left(q(x)\Vert q^{o}(x)\right)\approx \int\frac{\left(\Delta\theta\cdot \nabla_{\theta}q_{\theta}(x)\right)^2}{2q_{\theta}(x)} dx \approx \left(\Delta\theta\cdot c\right)^2
We have pointed out that a complete GAN generator loss function should include KL\left(q(x)\Vert q^{o}(x)\right). If it is not included, then this effect must be achieved through various indirect means. The above approximation indicates that the additional loss is approximately \left(\Delta\theta\cdot c\right)^2, which requires us not to let it be too large, i.e., not to let \Delta\theta be too large (in each stage, c can be considered approximately constant). Since we use gradient descent-based optimization algorithms, \Delta\theta is proportional to the gradient. Therefore, many tricks in standard GAN training, such as gradient clipping, using the Adam optimizer, and using BN, can be explained; they are all to stabilize the gradient so that \theta does not become too large. At the same time, the number of iterations for G(z) cannot be too many, because too many will also lead to \Delta\theta being too large.
Also, this part of the analysis only applies to the generator, while the discriminator itself is not constrained, so the discriminator can be trained to the optimum.
Regularization Term
Now we derive some truly useful content from this, directly estimating KL\left(q(x)\Vert q^{o}(x)\right) to obtain a regularization term that can be used in actual training. Direct calculation is difficult, but we can use KL\left(q(x,z)\Vert \tilde{q}(x,z)\right) to estimate it: \begin{aligned} KL\left(q(x,z)\Vert \tilde{q}(x,z)\right)&=\iint q(x|z)q(z)\log \frac{q(x|z)q(z)}{\tilde{q}(x|z)q(z)}dxdz\\ &=\iint \delta\left(x-G(z)\right)q(z)\log \frac{\delta\left(x-G(z)\right)}{\delta\left(x-G^{o}(z)\right)}dxdz\\ &=\int q(z)\log \frac{\delta(0)}{\delta\left(G(z)-G^{o}(z)\right)}dz \end{aligned} Because of the limit: \delta(x)=\lim_{\sigma\to 0}\frac{1}{(2\pi\sigma^2)^{d/2}}\exp\left(-\frac{x^2}{2\sigma^2}\right) we can treat \delta(x) as a Gaussian distribution with small variance. Substituting this, we have: KL\left(q(x)\Vert q^{o}(x)\right)\sim \lambda \int q(z)\Vert G(z) - G^{o}(z)\Vert^2 dz So the complete generator loss can be chosen as: \mathbb{E}_{z\sim q(z)}\left[-\log D(G(z))+\lambda \Vert G(z) - G^{o}(z)\Vert^2\right] In other words, the distance between the new and old generated samples can be used as a regularization term. The regularization term ensures that the model does not deviate too much from the old distribution.
The following two experiments on the CelebA face dataset show that this regularization term is effective.
Experiment 1: Ordinary DCGAN network, training the generator and discriminator for one batch each per iteration.
Experiment 2: Ordinary DCGAN network, but with BN removed, training the generator and discriminator for five batches each per iteration.
Conclusion and Summary
The results of this paper show that variational inference is indeed a unified framework for deriving and explaining generative models, including VAE and GAN. Through a new interpretation of variational inference, we introduced how variational inference achieves this goal.
Of course, this is not the first paper to propose using variational inference to study GANs. In the paper “On Unifying Deep Generative Models”, the authors also attempted to unify VAE and GAN using variational inference. Relatively speaking, I think the discussion in this paper is clearer and more explicit.
It seems that there is still a lot of room for exploration in variational inference, waiting for us to discover.