Proposition 1I.13.P0144 of 100 in the corpus
A latent-variable model explains each observation by a code it never sees, and its likelihood is an integral over every code.
Putting a prior on the code makes every code meaningful and lets the model generate. The price is an evidence and a posterior that only the linear model can write down, and that sampling the prior cannot estimate.
From a code to a distribution over codes
Chapter I.12 ended with two functions and no distribution. Its encoder gave every training input a code, and nothing said which other codes were valid. A latent-variable model starts from the other end. It says how data are generated: draw a code from a fixed prior, then draw an observation from a distribution the decoder computes from that code.
The decoder now returns the parameters of a distribution: a mean with variance for real-valued inputs, or one probability per coordinate for binary ones. By I.4, its negative log is a reconstruction loss. Generating is two draws: a code from the prior, then an observation from the decoder.
Two quantities follow, and both are hard.
The evidence is an integral over codes, and the posterior needs it.
The evidence is what maximum likelihood would maximise. It averages the decoder’s explanation of over every code the prior allows. The posterior, by Bayes’ rule (Bayes' rule 0.PR.04), says which codes explain , and it divides by the evidence. For a decoder that is a neural network neither has a closed form.
The one model where everything is exact
Make the decoder linear: . A linear function of Gaussians is Gaussian, so the integral in (I.13.1) can be done, and Bayes’ rule can be completed by collecting the terms in :
Probabilistic PCA: the one model in which the evidence and posterior are exact.
This is probabilistic PCA. Tipping and Bishop (1999) found its maximum likelihood in closed form. The noise variance is the average of the discarded eigenvalues of the data covariance, and for any rotation . The columns of span the principal subspace of I.12.T1, each scaled by how far its variance stands above the noise. A direction whose eigenvalue does not exceed gets no weight at all. Proposition 4 meets that threshold again.
Take the four points of Chapter I.12, , , and , with eigenvalues and along and , and one latent dimension. Maximum likelihood gives and
The model’s covariance has eigenvalues and : it is the data covariance exactly. Every corner then has the same log-evidence,
For , the scalar version of (I.13.2) gives the posterior. Its precision is and its mean is :
Compare I.12. The principal-axis code of this point was . The posterior mean points the same way, shrunk by , and it comes with a width of one half, set by the noise. The code is no longer a point.
Why the integral cannot be sampled from the prior
The evidence is an expectation over the prior, . Averaging over prior draws estimates it without bias. How many draws are needed? By Bayes’ rule , so the variance of one draw, relative to the square of what it estimates, is
Sampling the prior pays for every place the posterior is not.
The ratio inside is large wherever the posterior is concentrated and the prior is not. For a one-dimensional Gaussian posterior with mean and variance the integral is . For the point the relative variance is , so a relative standard error of ten per cent takes about draws.
That is the easy case. With noise variance in place of the posterior has standard deviation and the relative variance is . For independent latent dimensions the integral in (I.13.3) is a product, one factor of for each dimension with relative variance . In ten such dimensions the count exceeds ; I.13.B05 works it out. Almost every prior draw lands where the posterior is not, and contributes almost nothing.
The remedy is to draw codes from something close to the posterior. That distribution must be found for every observation, and finding it by a separate optimisation for each one is slow. Kingma and Welling (2014) and Rezende, Mohamed and Wierstra (2014) train an encoder network to output it instead. Sharing one network across all inputs is called amortised inference. It is fast, and it costs something: Cremer, Li and Duvenaud (2018) found that the bound lost to the shared encoder often exceeds the loss from the Gaussian family itself.
The variational autoencoder in one picture
The encoder of a variational autoencoder returns two vectors of width , a mean and a width , which define . A code is drawn from it, the decoder explains from that code, and the loss adds a second term that keeps close to the prior. The figure shows the path. Proposition 2 derives the loss, and proposition 3 shows how a gradient passes through the draw.
A width must be positive. A linear output layer can return , which is not a standard deviation. Encoders therefore predict and exponentiate, or pass the output through softplus, as I.3.X02 anticipated.
Compute the evidence and the posterior
This script evaluates the integral in (I.13.1) on a grid for the point and the model above:
from math import exp, log, pi, sqrt
x, w, var = (1.0, 2.0), (0.8*sqrt(3), 0.6*sqrt(3)), 1.0 # probabilistic PCA
def joint(z): # p(z) p(x | z) for z ~ N(0, 1), x | z ~ N(w z, var I)
r2 = sum((xi - wi*z)**2 for xi, wi in zip(x, w))
return exp(-z*z/2)/sqrt(2*pi)*exp(-r2/(2*var))/(2*pi*var)
h = 0.001
grid = [i*h for i in range(-10000, 10001)]
evidence = sum(joint(z) for z in grid)*h
mean = sum(z*joint(z) for z in grid)*h/evidence
spread = sum((z - mean)**2*joint(z) for z in grid)*h/evidence
print(round(log(evidence), 6), round(mean, 6), round(spread, 6))
It prints -3.531024 0.866025 0.25: the log-evidence, and the
posterior’s mean and variance, as (I.13.2) predicts. A grid works in one
dimension. In twenty it would need points.
Sources
Depends on
Used by
Problems using this
- I.13.B01 — One point, four boundsnumeric▲△△
- I.13.B05 — What sampling the prior costscomplexity▲▲△