Guides, the ELBO, and Variational Autoencoders

learned proposals · ELBO · stochastic gradients · VAEs

Javier Burroni

2026-07-01

Section 1: Learned Proposals

Change of notation

Today we will denote the latent variables by \(z\) and the observed data by \(x\). A probabilistic program defines a joint over latents and data: \[ p_\theta(x,z) = p(z)\,p_\theta(x \given z). \]

  • We observe \(x\) and want the posterior \(p_\theta(z \given x)\).
  • Inference must generate latent executions \(z\) that could have produced \(x\).

Question 1

A model gives a joint \(p_\theta(x,z)=p(z)\,p_\theta(x \given z)\), and we introduce a guide \(q_\phi(z \given x)\).

What role does the guide play?

A) It defines a map from observations to the most probable latent execution.

B) It defines an alternative joint model \(\hat{p}_\theta(x,z)\) whose posterior is easier to sample.

C) It defines a distribution from which latent executions are proposed, using the observations.

D) It guides the estimation of the evidence by directly predicting \(p_\theta(x)\). which is usually intractable.

A guide proposes executions

The model defines the target.

The guide defines how candidate latent executions are proposed:

\[z \sim q_\phi(z \given x).\]

The guide does not replace the model. It is part of the inference mechanism.

Question 2

Likelihood weighting samples latent variables from the prior, \(z \sim p(z),\) and scores the observation. Now suppose we instead sample

\[z \sim q_\phi(z \given x).\]

What has changed relative to likelihood weighting?

A) The target distribution has changed from the posterior under the model to the posterior under the guide.

B) The observation has been incorporated into the sampler, so the importance correction is no longer needed.

C) The proposal distribution has changed.

D) The proposal is now equivalent to likelihood weighting whenever it assigns nonzero mass wherever the prior does.

Proposal, not target

  • The guide changes how candidate executions are generated.
  • The model still defines which executions are plausible.
  • The correction is still with respect to the model joint \(p_\theta(x,z)\), whose normalized version is the posterior \(p_\theta(z \given x)\).
  • Conditioning the proposal on \(x\) can reduce weight variance, but does not remove the correction in general.

Question 3

For importance sampling with proposal \(q(z \given x)\) and weight \(w(z)=\dfrac{p_\theta(x,z)}{q(z \given x)}\),

which proposal would make the weights constant?

A) \(q(z\given x)=p(z)\), since the prior is the model’s own distribution over latents.

B) \(q(z\given x)=p_\theta(z\given x)\), since the proposal would match the normalized target over latents.

C) \(q(z\given x)\propto p_\theta(x\given z)\), since the proposal should favor latents that explain the observation.

D) \(q(z\given x)\) should concentrate near maximizers of \(p_\theta(x,z)\), since high-density traces dominate posterior expectations.

The zero-variance proposal

The ideal proposal is the posterior:

\[q^\star(z \given x)=p_\theta(z \given x).\]

Then

\[w(z) = \frac{p_\theta(x,z)}{p_\theta(z \given x)} = p_\theta(x),\]

which does not depend on \(z\).

This is ideal but unavailable, because sampling from \(p_\theta(z \given x)\) is the inference problem itself.

Section 2: Training guides

Question 4

The two KL directions are

\[\mathrm{KL}(q_\phi \Given p_\theta) = \mathbb{E}_{q_\phi(z \given x)}\!\left[\log q_\phi(z \given x) - \log p_\theta(z \given x)\right],\]

and

\[\mathrm{KL}(p_\theta \Given q_\phi) = \mathbb{E}_{p_\theta(z \given x)}\!\left[\log p_\theta(z \given x) - \log q_\phi(z \given x)\right].\]

The first is often more mode-seeking; the second is often more mode-covering.

Why does ordinary VI naturally lead to the first direction?

A) Its expectation is over the model posterior, which makes it more faithful to all posterior modes.

B) It avoids the need to evaluate either the guide density or the model density.

C) It is the only direction whose value is finite for both discrete and continuous latent variables.

D) Its expectation is over the guide, which is the distribution we can sample from and evaluate during inference.

What we can actually compute

Ordinary VI uses expectations under the guide:

\[z \sim q_\phi(z \given x).\]

That is operationally available because we control \(q_\phi\).

The inclusive direction would require expectations under

\[p_\theta(z \given x),\]

which is exactly the posterior we are trying to approximate.

Question 5

For fixed \(\theta\), suppose we want to fit \(q_\phi(z \given x)\) by minimizing \(\mathbb{E}_{q_\phi(z \given x)}\!\left[\log q_\phi(z \given x) - \log p_\theta(z \given x)\right].\)

But, the ELBO is: \[ \mathbb{E}_{q_\phi(z \given x)}\!\left[\log p_\theta(x,z) - \log q_\phi(z \given x)\right].\]

Why do we usually optimize the ELBO instead of this KL directly?

A) The KL requires samples from \(q_\phi(z \given x)\), while the ELBO can be optimized without sampling latent variables.

B) The KL contains the unknown evidence term \(\log p_\theta(x)\), while the ELBO removes this term without changing the optimizer over \(\phi\).

C) The KL is only useful for guide learning, while the ELBO is only useful for learning the generative model.

D) The KL uses \(\log p_\theta(x,z)\), while the ELBO avoids evaluating the joint density.

From the exclusive KL to the ELBO

Start from

\[\mathrm{KL}(q_\phi \Given p_\theta) = \mathbb{E}_{q_\phi}\!\left[\log q_\phi(z \given x) - \log p_\theta(x,z)\right] + \log p_\theta(x).\]

Rearrange:

\[\log p_\theta(x) = \mathcal{L}(\theta,\phi) + \mathrm{KL}(q_\phi \Given p_\theta),\]

where

\[\mathcal{L}(\theta,\phi) = \mathbb{E}_{q_\phi(z \given x)}\!\left[\log p_\theta(x,z) - \log q_\phi(z \given x)\right].\]

For fixed \(\theta\), \(\log p_\theta(x)\) does not depend on \(\phi\), so maximizing \(\mathcal{L}\) minimizes the exclusive KL.

Question 6

The ELBO is

\[\mathcal{L}(\theta,\phi) = \mathbb{E}_{q_\phi(z \given x)}\!\left[\log p_\theta(x,z) - \log q_\phi(z \given x)\right].\]

We want to estimate \(\nabla_\phi \mathcal{L}(\theta,\phi)\).

What is the main difficulty?

A) The parameter \(\phi\) affects the density inside the expectation and also the distribution that generates \(z\).

B) The parameter \(\phi\) only appears in the term \(\log q_\phi(z \given x)\), so the model term gives no gradient signal.

C) The sampled value \(z\) blocks all gradients, so the expectation has no well-defined derivative.

D) The parameter \(\phi\) can be differentiated only when \(q_\phi\) is exactly equal to the posterior.

The gradient problem

  • Gradients with respect to \(\theta\) are comparatively direct once \(z\) is sampled.
  • Gradients with respect to \(\phi\) are subtler because \(\phi\) changes the law of the sampled execution.
  • Two standard estimators are score-function gradients and reparameterized gradients.

Two gradient estimators

Score-function

\[\nabla_\phi \mathbb{E}_{q_\phi(z)}[f(z)] = \mathbb{E}_{q_\phi(z)}\!\left[f(z)\,\nabla_\phi \log q_\phi(z)\right]\]

Reparameterization

There exists a distribution \(q(\epsilon)\) and a function \(g_\phi\) such that

\[\epsilon \sim q(\epsilon) \implies g_\phi(\epsilon, x) \sim q_\phi(z \given x).\]

\[\nabla_\phi \mathbb{E}_{q_\phi(z \given x)}[f(z)] = \nabla_\phi \mathbb{E}_{\epsilon}\!\left[f(g_\phi(\epsilon,x))\right]\]

  • General, including discrete latents.
  • Often high variance.
  • Needs continuous, differentiable \(z\).
  • Often lower variance.

Reparameterization is the standard route for Gaussian latent VAEs.

Section 3: Deep generative models and amortization

Question 7

Suppose we know part of the data-generating structure and write \(z \sim p(z)\), but the relation between latent structure and observations is complex. We therefore write \[ x \sim p_\theta(x \given z), \]

where \(p_\theta(x \given z)\) is represented by a neural network.

What is gained by making this part of the model deep?

A) We can use the neural network to replace posterior inference with a direct prediction of \(z\) from \(x\).

B) We can keep explicit latent structure while learning a flexible conditional distribution from \(z\) to \(x\).

C) We can avoid specifying a probability model for \(x\), since the neural network only needs to simulate observations.

D) We can make the posterior tractable by choosing a sufficiently expressive decoder distribution.

Partial specification

\[p_\theta(x,z)=p(z)\,p_\theta(x \given z).\]

Deep probabilistic programming does not mean replacing the probabilistic model with a neural network.

It means:

  • specify the structure we know;
  • learn the parts we do not want to hand-design;
  • keep the model generative and probabilistic.

Deep probabilistic programming

The single most important aspect of deep probabilistic programming: the ability to partially specify the generative model; encoding the parts of the model you do know, and letting model learning and generic deep neural network function approximation take care of the rest.

Question 8

Suppose a model uses

\[ \eta_\theta(z)=f_\theta(z), \qquad x \sim p(x;\eta_\theta(z)), \]

where \(p(x;\eta)\) is a known distribution family.

What is the main reason for this pattern?

A) The distribution family is used to make the posterior analytically tractable.

B) The neural network is only needed because the latent variable \(z\) is not directly observable.

C) The program still needs a distribution object that supports sampling and scoring observed values.

D) The distribution family is used so that the model no longer needs a guide.

Why output distribution parameters?

A probabilistic program needs distributions with operations like:

\[ x \sim p_\theta(x \given z) \]

and

\[ \log p_\theta(x \given z). \]

A neural network can make the distribution flexible by predicting parameters,

\[ \eta_\theta(z) = f_\theta(z), \qquad p_\theta(x \given z)=p(x;\eta_\theta(z)), \]

while the chosen family \(p(x;\eta)\) supplies:

  • a sampler;
  • a density or log-density;
  • gradients with respect to \(\eta\) and \(\theta\), when needed.

So the network does not usually replace the probability distribution. It parameterizes one.

Normalizing flows

We are not limited to very simple families.

Start with a base distribution that we know how to sample and score:

\[ u \sim p_0(u). \]

Apply an invertible neural transformation: \(x = T_\theta(u).\)

Then samples are easy:

\[ u \sim p_0(u), \qquad x = T_\theta(u). \]

And densities are available by change of variables:

\[ \log p_\theta(x) = \log p_0(T_\theta^{-1}(x)) + \log \Bigl| \det \frac{\partial T_\theta^{-1}(x)}{\partial x} \Bigr|. \]

A normalizing flow is a more expressive distribution family supporting the two needed operations:

  • sampling;
  • log-density evaluation.

Amortized inference

Suppose that for each observation \(x\) we want to infer the latent \(z\).

\[ p_\theta(z \given x). \]

A guide shares parameters across observations:

\[ \lambda_\phi(x) = \text{network}_\phi(x), \qquad q_\phi(z \given x) = q(z;\lambda_\phi(x)). \]

Instead of solving a new inference problem from scratch for every \(x\), we learn a function that maps observations to proposal distributions over latents.

Question 9

A variational autoencoder uses

\[z \sim p(z), \qquad x \sim p_\theta(x \given z), \qquad z \sim q_\phi(z \given x).\]

The terms \(p_\theta(x \given z)\) and \(q_\phi(z \given x)\) are often called the decoder and encoder.

How should we interpret these two networks in probabilistic-programming terms?

A) The decoder is part of the generative model, while the encoder is a learned guide for posterior inference.

B) The encoder is part of the generative model, while the decoder is a learned guide for posterior inference.

C) The encoder and decoder are two parameterizations of the same posterior distribution, trained from opposite directions.

D) The decoder defines the prior over \(z\), while the encoder defines the marginal distribution over \(x\).

The VAE, assembled

Model:

\[p_\theta(x,z)=p(z)\,p_\theta(x \given z).\]

Guide:

\[q_\phi(z \given x).\]

Objective:

\[\mathcal{L}(\theta,\phi) = \mathbb{E}_{q_\phi(z \given x)}\!\left[\log p_\theta(x,z) - \log q_\phi(z \given x)\right].\]

Common Gaussian guide:

\[q_\phi(z \given x) = \mathcal{N}\!\left(\mu_\phi(x),\,\operatorname{diag}(\sigma_\phi^2(x))\right),\]

with reparameterization

\[z = \mu_\phi(x) + \sigma_\phi(x)\odot\epsilon, \qquad \epsilon \sim \mathcal{N}(0,I).\]

Summary

  • A guide is a learned proposal over latent executions.
  • The ideal proposal is the posterior, but it is not directly available.
  • VI learns a tractable guide \(q_\phi(z \given x)\).
  • The available KL direction uses expectations under \(q_\phi\).
  • The ELBO is the tractable objective used to train the guide.
  • Gradients through stochastic choices require score-function or reparameterized estimators.
  • Learning \(\theta\) trains the generative model, often partially specified with neural conditionals.
  • A VAE is a neural generative model plus an amortized neural guide.

Questions: Answer Key (1/2)

Solutions

  • Q1: C. The guide proposes latent executions, conditioned on \(x\). It is not an alternative joint model (B), not a map to a single most probable latent (A), and not an evidence estimator (D).
  • Q2: C. Only the proposal changed; the correction still targets \(p_\theta(x,z)\). The target does not switch to a guide posterior (A), the weight is still needed (B), and it is not equivalent to likelihood weighting under that support condition (D).
  • Q3: B. With \(q=p_\theta(z \given x)\) the weight equals the constant \(p_\theta(x)\). The prior (A) and the likelihood-shaped proposal (C) leave the weights varying, and concentrating on the mode (D) is not the posterior.
  • Q4: D. The exclusive direction’s expectation is over \(q_\phi\), which we can sample and evaluate. It is not taken over the posterior (A), it still evaluates densities (B), and finiteness across latent types is not the reason (C).
  • Q5: B. The KL hides the intractable \(\log p_\theta(x)\), which is constant in \(\phi\), so the ELBO shares its \(\phi\)-optimizer. Both still sample \(q_\phi\) (A), the ELBO also trains the guide (C), and the ELBO does evaluate the joint (D).

Questions: Answer Key (1/2)

Solutions

  • Q6: A. \(\phi\) enters both the integrand and the sampling law, so the gradient does not pass straight inside. \(\phi\) is not confined to \(\log q_\phi\) (B), the derivative is well defined (C), and it does not require \(q_\phi\) to equal the posterior (D).
  • Q7: C. We keep the latent structure we understand and learn the conditional we cannot write by hand. It does not reduce to a direct \(x \to z\) predictor (A), does not make inference exact (B), and the model density is still evaluated (D).
  • Q8: C.
  • Q9: A. The decoder defines \(p_\theta(x,z)\) and the encoder is the guide \(q_\phi(z \given x)\). The roles are not swapped (B), they are not two views of one posterior (C), and the decoder is not the prior nor the encoder the marginal (D).