Skip to main content
Three distributions appear in a VAE, and they are easy to confuse. The prior p(z)p(\mathbf z) is fixed before training, typically N(0,I)\mathcal N(\mathbf 0, \mathbf I). The true posterior pθ(z∣x)p_\theta(\mathbf z \mid \mathbf x) is the distribution of latent codes that could have produced a given x\mathbf x under the decoder. It follows from Bayes’ rule and is intractable for a neural decoder. The approximate posterior qϕ(z∣x)q_\phi(\mathbf z \mid \mathbf x) is the distribution the encoder outputs, and it stands in for the true posterior. The decoder, or generative model, is pθ(x∣z) p(z)p_\theta(\mathbf x \mid \mathbf z)\, p(\mathbf z). The encoder, or inference model, is qϕ(z∣x)q_\phi(\mathbf z \mid \mathbf x). The premise is that the posterior over z\mathbf z is more compact and more informative than the prior, because it concentrates on the codes that explain the observed x\mathbf x. Sampling from it is not possible directly, so the encoder learns an approximation and this is what lets us optimize the marginal likelihood. The two networks are used at different times. Training and posterior inference need the encoder: it maps x\mathbf x to a distribution over z\mathbf z. Unconditional generation does not use it. You sample z∼p(z)\mathbf z \sim p(\mathbf z) from the prior and pass the sample through the decoder. The VAE encoder-decoder spaces are shown below. The picture shows the more compact space that is defined by the encoder. vae VAE spaces and distributions (from here) The architecture of VAE includes four main components as shown below: vae VAE Architecture (from here) Similar to the generative model, the inference model can be, in general, a PGM of the form: q(z∣x;ϕ)=∏j=1Mq(zj∣Pa(zj),x;ϕ)q(\mathbf z | \mathbf x ; \mathbf \phi) = \prod_{j=1}^M q(\mathbf z_j | Pa(\mathbf z_j), \mathbf x ; \mathbf \phi) and this, similarly to the generative model, can be parametrized with a DNNenc(ϕ)DNN_{enc}(\phi). More specifically we obtain the approximation using the following construction: (μ,log⁡Σ)=DNNenc(x,ϕ) (\mathbf \mu, \log \mathbf \Sigma ) = DNN_{enc}(\mathbf x, \mathbf \phi) q(z∣x;ϕ)=N(z;μ,diag(Σ))q(\mathbf z| \mathbf x ; \mathbf \phi) = N(\mathbf z; \mathbf \mu, \textsf{diag}(\mathbf \Sigma))

The reparameterization trick

Training needs gradients of an expectation over qϕq_\phi with respect to ϕ\phi. If you draw z∼q(z∣x;ϕ)\mathbf z \sim q(\mathbf z \mid \mathbf x ; \phi) as a black-box sample, the sampling step has no derivative with respect to ϕ\phi, so backpropagation stops there. The reparameterization trick moves the randomness into a variable that does not depend on ϕ\phi: z=μϕ(x)+σϕ(x)⊙ϵ,ϵ∼N(0,I).\mathbf z = \mathbf \mu_\phi(\mathbf x) + \mathbf \sigma_\phi(\mathbf x) \odot \mathbf \epsilon, \qquad \mathbf \epsilon \sim \mathcal N(\mathbf 0, \mathbf I). The sample z\mathbf z now has the same distribution N(μϕ,diag(σϕ2))\mathcal N(\mathbf \mu_\phi, \textsf{diag}(\mathbf \sigma_\phi^2)) as before. It is also a differentiable function of ϕ\phi and the noise ϵ\mathbf \epsilon, and ϵ\mathbf \epsilon is drawn without reference to any parameter. For a fixed draw of ϵ\mathbf \epsilon, the derivatives ∂z/∂μϕ\partial \mathbf z / \partial \mathbf \mu_\phi and ∂z/∂σϕ\partial \mathbf z / \partial \mathbf \sigma_\phi are ordinary and the chain rule carries the decoder loss back into the encoder weights. Here σϕ=exp⁡(12log⁡Σ)\mathbf \sigma_\phi = \exp(\tfrac12 \log \mathbf \Sigma) in the encoder parameterization above. The DNNencDNN_{enc} implements amortized variational inference, that is, it estimates the posterior parameters over a batch of datapoints and this offers significant boost in the parameter learning. With the encoder defined, the next question is how to train its parameters ϕ\phi jointly with the decoder parameters θ\theta when the true posterior p(z∣x;θ)p(\mathbf z | \mathbf x; \mathbf \theta) is intractable. The answer is the Evidence Lower Bound (ELBO): a tractable surrogate for the marginal log-likelihood that is derived from the KL divergence between qq and the true posterior. The derivation and its consequences for joint optimization are covered in the Optimization and the ELBO page.

PyTorch reference


Found an error on this page? Report an issue.