7
$\begingroup$

I'm trying to understand how VAEs work, because I didn't understand how cross entropy between $x$ (input fed into the encoder), and $p(x|z)$ (output of decoder) minus KL divergence between $p(z|x)$, and $p(z)$ result in the latent space being so continuous. Although, I still don't understand it, the first step is to understand why are we calculating the loss as $KL(q(z|x)||p(z|x))$, because as I read, this is the first "idea", but because of $p(x)$ is intractable, we use the fact that maximizing ELBO minimizes KL, and breaking down ELBO in an equation where we can calculate each piece.

The first question in my mind was why aren't we comparing $p(x|z)$, and $q(x|z)$. At the end of the day we care about how well $x$ was constructed from $z$. The encoder produces $q(z|x)$, but where does $p(z|x)$ come from, and what do we know about it? We're calculating $p(x|z)$ with the decoder, not $p(z|x)$. Or does it mean if we can transform $p(x|z)$ to $p(z|x)$, and the difference between $q(z|x)$, and $p(z|x)$ is small, then $p$ is predicting $x$ from $z$ accurately, as $p(x|z)$?

In this understanding, we're trying to build two networks mirroring each other. But I just don't understand seeing the current equations how it is performing well. We're comparing two distributions that maybe similar to each other, but in neural network perspective, can be wrong (poor reconstruction, or wrong place in latent space). So it shouldn't be enough to build networks that are exact opposite of each other. Our only pillar is $x$ fed into the encoder, and if it would mean $q(x|z)$, it would make sense, but as I've read so far, the input fed into the encoder is simply $x$. On the other side, if the input fed into encoder would be $q(x|z)$, it would prove that if the difference between $q(z|x)$, and $p(z|x)$ is small, it doesn't mean $p$ is predicting $x$ from $z$ accurately, as $p(x|z)$, at least early stages of the training.

So the question is does comparing $q(z|x)$ with $p(z|x)$ prove how effectively the encoder, and decoder mirroring each other? In my head they could mirror each other effectively by placing the latent variables not with the continuousity as they do, so how does the loss we're calculating ensures that? Also, regardless of the tractability of the breakdown, could comparing $q(x|z)$ with $p(x|z)$ measure the loss just as effective as comparing $q(z|x)$ with $p(z|x)$?

Are my thoughts, and imagination right, or I'm missing something important?

Sorry for asking questions that may be stupid, or obvious, but I don't have too much knowledge of probabilities, just built simple neural networks, and it is very hard to imagine in a programmer perspective.

$\endgroup$

1 Answer 1

3
+50
$\begingroup$

The first thing to appreciate about VAEs is that they are not just some magical deep generative model but that they are a special case of the Auto-Encoding Variational Bayes algorithm for doing variational Bayesian inference in generative models.

What that means is we consider a setup with a dataset $\mathcal{D} =\{x_i\}_{i=1}^n$ where we assume the generative process for an instance $x_i$ is done in two steps:

  1. First sample a latent variable $z_i$ from some prior $p(z)$
  2. Second sample $x_i$ from the distribution $p(x|z)$

Note that we don't observe the values of $z_i$ in our dataset so we're interested in working out for each example in our data what's the posterior distribution over its corresponding latent variable given we've seen all the data - i.e. what is $p(z|x)$?

So in a sense then this is the main objective in VAEs - to uncover $p(z|x)$ and not exactly to model p(x), that is just a byproduct of the algorithm.

To answer your first question then, we are interested in the $D_{KL}[q(z|x)||(p(z|x)]$ because what we're doing is accepting that $p(z|x)$ is intractable and we won't be able to calculate it, but we're trying to approximate it with $q(z|x)$ (which here is using a neural network) and we want them to be as similar as possible, hence why we're minimising that divergence.

For your second question on why this should perform well you just need to take a look at the form of the ELBO given here: $$ \mathcal{L}(\theta,\phi) = \underbrace{\mathbb{E}_{q_\phi}[\log p_\theta(x|z)]}_{\text{Reconstruction Loss}} - \underbrace{D_{KL}[q_\phi(z|x)||p(z)]}_{\text{KL Regulariser}} $$ The first term is the expected likelihood of the data given the latent variables, it should make sense that is a sensible thing to maximise as this makes the data more likely under the model while the second term maintains the `auto-encoder' part and keeps the approximate distribution close to the prior.

For your final question I would just say that I don't think it's particularly useful to think of the two network as ``mirroring'' each other. Rather they both model a different probability distribution and it so happens that we can train them jointly and effectively through this auto encoding algorithm. Comparing $q(z|x)$ and $p(z|x)$ then is measuring how good the encoder is at approximating the true posterior distribution over the latent variables, and so doesn't really say anything about the decoder.

Also you repeatedly mention $q(x|z)$ but this is just not something that is part of the model since we kind of assume the structure of $p_\theta(x|z)$ and just learn that directly (it's the decoder) so we don't worry about some $q$ variational distribution.

$\endgroup$
6
  • $\begingroup$ I understood this as our only goal is to get the probability of $z$ given $x$, and we only need the decoder to have $p(x|z)$ in order to be able to do the loss calculation. But this assumes $p(x|z)$ (the decoder) is "right", which is not the case. Also, you mention we sample $x$ from $p(x|z)$. Isn't what the decoder outputs is $p(x|z)$ as a Bernoulli distribution as I've read? In practice for the reconstruction loss, we're calculating the cross entropy between the encoder's input, and the decoder's output. Mathematically how they are noted (input, and the output)? $\endgroup$ Commented Dec 18, 2020 at 19:31
  • $\begingroup$ It doesn't really assume $p(x|z)$ is 'right' in any sense because we are learning it at the same time - we're trying to learn the parameters of this distribution. The output of the decoder is arbitrary, it could be the parameters of any parameterisable distribution. It is true that often (for a basic MNIST example) this is a Bernoulli distribution for each pixel - the cross entropy loss is then equivalent to the log-likelihood of seeing the example. $\endgroup$
    – XanderJC
    Commented Dec 19, 2020 at 17:49
  • $\begingroup$ And how are the encoder’s input, and the decoder’s output denoted mathematically? $x$ and $\hat{x}$? So in practice we’re calculating the cross entropy between them, but mathematically the part of that loss is denoted as $E_q[log \space p_\theta(x|z)]$. How is that converted? $\endgroup$ Commented Dec 20, 2020 at 12:47
  • $\begingroup$ During training we don't sample from the decoder - the output of the decoder are some parameters of a distribution (e.g. Bernoulli), we then calculate the log-likelihood of the input $x$ given these parameters - in the Bernoulli case this is equivalent to the CE loss. (side point: you can't calculate a CE loss between realisations like $x$ and $\hat{x}$, but between a realisation and a probability) $\endgroup$
    – XanderJC
    Commented Dec 20, 2020 at 15:02
  • $\begingroup$ What do you mean by some parameters of a distribution? How is that called? You’re saying the log likelihood of input $x$ given these parameters. But the notation says $log \space p(x|z)$. In this case these parameters are $z$, as it is the log likelihood of $x$ given $z$. The transition doesn’t make sense. I don’t understand how $E_q[log \space p(x|z)]$ became $-(x \space log(\hat{x}) + (1 - x)\space log(1 - \hat{x}))$ $\endgroup$ Commented Dec 20, 2020 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.