Introduces the variational autoencoder: a learned recognition model, the evidence lower bound and the reparameterization trick (ICLR 2014).
Variational Autoencoders
A latent-variable model trained by maximizing the evidence lower bound (ELBO). The bound falls short of the log-likelihood by the KL divergence from the encoder to the true posterior, so the encoder learns approximate inference, not just compression.
Intuition
The idea in plain words, before the symbols.
Maximum likelihood asks for a model that assigns high log probability to the observed data:
A variational autoencoder (Kingma and Welling, 2014; Rezende, Mohamed and Wierstra, 2014) is a maximum-likelihood model with a hidden cause. It assumes that each observation was produced from an unobserved latent variable :
Training would be easy if we could compute the marginal likelihood
The problem is that this integral is usually hard for neural decoders. Bayesian inference tells us what we would like to use:
But that posterior contains the same hard evidence term . The VAE move is to learn an encoder
that acts as a tractable approximate posterior.
The central mechanism is not "an autoencoder with noise." It is: replace the intractable posterior with a learned distribution , then optimize a lower bound whose looseness is exactly a KL divergence from to the true posterior.
That is why VAEs sit at the intersection of maximum likelihood, Bayesian inference, and KL divergence.
Two natural next moves branch from this page. Normalizing flows ask what changes when the latent transformation is invertible and likelihood stays exact. Diffusion asks what changes when generation is learned as a gradual denoising process rather than as one sampled latent code.
Study prompts for this section: Intuition
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Mathematics
Definitions, assumptions and the derivation.
Latent variable likelihood
For one observed datapoint , assume a prior and a decoder likelihood . The latent-variable model, marginal likelihood, and true posterior are
The evidence appears in the denominator, so exact posterior inference and exact likelihood training are tied to the same difficult integral. The whole method rests on three lines:
The first line says the ELBO gap is exactly the KL from the learned encoder to the true posterior. The second line separates reconstruction likelihood from prior pressure. The third line keeps randomness in external noise , so gradients of sampled reconstruction terms can flow through into the encoder outputs.
The ELBO identity
Let be any approximate posterior whose support is compatible with the true posterior. To keep the equations readable, write for and for expectation under that distribution:
Using Bayes' rule and rearranging gives the exact identity
Here is the ELBO and is the inference gap. Let denote the true posterior . Then the gap is
Equivalently,
The ELBO term is
The gap can also be read by subtraction:
KL is nonnegative, so
Maximizing the ELBO improves this lower bound. The bound is tight exactly when the encoder distribution equals the true posterior, up to probability-zero events.
Reconstruction likelihood plus prior KL
Because
the ELBO can be rewritten as
The first term is a reconstruction log likelihood, not generically a pixel distance. The second term keeps the encoder's posterior close to the prior used for generation. At generation time we sample , not for a training example, so keeping the encoder's distributions close to the prior matters.
Two KL divergences now appear, and they are easy to confuse. , to the prior, sits inside the ELBO; , to the posterior, is the gap. An encoder equal to the prior makes the first zero but usually leaves a gap; an encoder equal to the posterior closes the gap but usually raises the first, and the ELBO still goes up. The ELBO bounds one model's , so a higher ELBO for one model than for another does not mean a higher likelihood unless their gaps are known. The practice after the demo computes both divergences for a model small enough to solve exactly.
Reconstruction likelihood is not always MSE
The reconstruction term is
It becomes a familiar loss only after choosing a decoder distribution. With a fixed-variance Gaussian decoder centered at , the negative log likelihood is a scaled squared error plus a constant. So MSE appears as a Gaussian negative log likelihood only after that modeling choice.
If is binary and
then the negative reconstruction log likelihood is binary cross-entropy.
Reparameterization trick
For a diagonal Gaussian encoder, the network outputs and . We sample with external noise:
This keeps the randomness in and makes the sample differentiable with respect to the encoder outputs and .
Beta-VAE objective
A beta-VAE (Higgins et al., 2017) changes the training objective to
When , this is the standard VAE ELBO. When , it is a modified objective rather than the original ELBO. For , it remains a lower bound because it subtracts extra nonnegative KL penalty, but it is no longer the standard maximum-likelihood ELBO. For , it is generally not guaranteed to lower-bound .
Larger puts more pressure on to carry less information and stay close to the prior. This can encourage more factor-like representations in some settings, but it does not guarantee disentanglement: Locatello et al. (2019) show that learning disentangled representations without supervision is impossible without inductive biases on both the model and the data. If the pressure is too strong, the encoder may approach , so carries little information about .
Study prompts for this section: Mathematics
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Code
The same calculation as runnable code, in the notation of the derivation.
This scalar linear-Gaussian example is small enough that we can compute the exact marginal likelihood, exact posterior, ELBO, and inference gap, then check a one-sample reparameterized reconstruction gradient by finite differences.
import math
def log_norm(x, mean, var):
return -0.5 * (math.log(2 * math.pi * var) + (x - mean) ** 2 / var)
def kl_norm(mu0, var0, mu1, var1):
return 0.5 * (var0 / var1 + (mu1 - mu0) ** 2 / var1 - 1 + math.log(var1 / var0))
x, w, b, sigma_x = 1.5, 1.2, -0.1, 0.3 # z~N(0,1), x|z~N(wz+b,sigma_x^2)
var_x = sigma_x ** 2
mu_q, logvar_q = 0.3, -0.7 # q(z|x)=N(mu_q, exp(logvar_q))
var_q = math.exp(logvar_q)
log_px = log_norm(x, b, w * w + var_x)
post_var = 1 / (1 + w * w / var_x)
post_mu = post_var * w * (x - b) / var_x
recon = -0.5 * (math.log(2 * math.pi * var_x) + ((x - b - w * mu_q) ** 2 + w * w * var_q) / var_x)
kl_prior = kl_norm(mu_q, var_q, 0, 1)
elbo = recon - kl_prior
gap = log_px - elbo
kl_post = kl_norm(mu_q, var_q, post_mu, post_var)
assert abs(gap - kl_post) < 1e-10
eps = -0.4
def sampled_recon(mu, logvar):
sigma = math.exp(0.5 * logvar)
z = mu + sigma * eps
return log_norm(x, w * z + b, var_x), z, sigma
sample_loglik, z, sigma = sampled_recon(mu_q, logvar_q)
dloglik_dz = (x - (w * z + b)) * w / var_x
path_mu = dloglik_dz
path_logvar = dloglik_dz * 0.5 * sigma * eps
h = 1e-5
fd_mu = (sampled_recon(mu_q + h, logvar_q)[0] - sampled_recon(mu_q - h, logvar_q)[0]) / (2 * h)
fd_logvar = (sampled_recon(mu_q, logvar_q + h)[0] - sampled_recon(mu_q, logvar_q - h)[0]) / (2 * h)
assert abs(path_mu - fd_mu) < 1e-7
assert abs(path_logvar - fd_logvar) < 1e-7
print(round(elbo, 3), round(log_px, 3), round(gap, 3), round(sample_loglik, 3))
For a batched neural VAE, typical shapes are:
x:(B, D)- encoder outputs
mu,logvar:(B, K) - noise
eps:(S, B, K) - latent samples
z = mu[None, :, :] + exp(0.5 * logvar)[None, :, :] * eps:(S, B, K) - decoder log likelihood per sample:
(S, B) - Monte Carlo reconstruction estimate:
(B,) - analytic diagonal-Gaussian KL:
(B,) - per-example ELBO:
(B,) - optimization loss
-elbo.mean(): scalar
Study prompts for this section: Code
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Demo
Change an input, predict the result, and see which quantities respond.
Explore Variational Autoencoders
Observation guide
Optional and ungraded. Opening a guide saves nothing.
Choose what to inspect in Variational Autoencoders, then open its guide.
The demo keeps the latent variable one-dimensional so the hidden inference geometry can be revealed after a prediction. Move the approximate posterior , inspect the prior and likelihood shape, then predict what the ELBO gap will diagnose.
After reveal, compare the target curve and identity values with your prediction. The point is to feel the ELBO as a lower bound whose looseness is measured by the mismatch between and the hidden target distribution.
Study prompts for this section: Demo
Each button copies a prompt about this section to your clipboard, for an AI assistant you already use. Nothing is sent by this site.
Practice
In a model whose posterior and evidence are exact, compute the KL to the prior and the KL to the posterior for two encoders. Then compare two models by their ELBOs.
Concept: Variational Autoencoders
What is the smallest example of Variational Autoencoders you can work through by hand, and what does it show?
DetailsGenerative Models
Reference id
concept:generative-models/vaesAfter the first pass
Go deeper
Optional material for a second reading. Sources are always shown; the other panels open from their headings or from these links, and closing one keeps what you wrote or revealed.What to look for in each sectionChoose a section and what you expect to see in it, then read short advice.
Section by section
Choose what to look for in each section
A latent-variable model trained by maximizing the evidence lower bound (ELBO). The bound falls short of the log-likelihood by the KL divergence from the encoder to the true posterior, so the encoder learns approximate inference, not just compression.

Start with the picture, metaphor, or geometric mechanism.
Choose what you expect to see in this section of Variational Autoencoders.
Questions to ask of the pictureChoose a part of the picture and what you expect, then read short advice.
Looking at the picture
Questions to ask of the picture
A latent-variable model trained by maximizing the evidence lower bound (ELBO). The bound falls short of the log-likelihood by the KL divergence from the encoder to the true posterior, so the encoder learns approximate inference, not just compression.
Which part of the picture should you look at first?
Pick the part of the picture you expect to explain Variational Autoencoders.
Sources
References for this page
A reference shows where an idea comes from; it does not vouch for every step on this page.Independently develops stochastic backpropagation for latent-variable generative models with learned inference networks (ICML 2014).
Weights the KL term by beta > 1 and reports more disentangled latent factors on several image datasets (ICLR 2017).
Proves that unsupervised disentanglement is impossible without inductive biases on model and data; in more than 12,000 trained models, well-disentangled ones could not be identified without supervision (ICML 2019).
A monograph-length treatment of VAEs: the ELBO and its gap, the reparameterization trick and common extensions (Foundations and Trends in Machine Learning, 2019).
What each claim rests on1 claim, each with its sources, where it appears on this page, and its caveats.
Claims and sources
A latent-variable model trained by maximizing the evidence lower bound (ELBO). The bound falls short of the log-likelihood by the KL divergence from the encoder to the true posterior, so the encoder learns approximate inference, not just compression.
The checks were made by this site, not by an independent reviewer.
Kingma/Welling and Rezende/Mohamed/Wierstra both ground VAEs/deep latent-variable models in ELBO optimization with learned inference networks and reparameterized or stochastic backpropagation estimators. The...
The reconstruction term is an expectation under q_phi and often Monte Carlo estimated. Pathwise gradients require a differentiable transform of parameter-independent noise, so this does not cover discrete or...
Kingma/Welling support q_phi(z|x) as a learned recognition model for the intractable posterior, derive log p_theta(x)=ELBO+KL(q||posterior), decompose the ELBO into E_q log p_theta(x|z)-KL(q||p(z)) and give Gaussian reparameterization. Rezende et al. independently support the recognition-model, lower-bound/free-energy, reconstruction/regularization and stochastic-backprop structure. The page's equations, code and demo show the ELBO gap, prior KL, a reparameterized sample and posterior checks shown once you predict.
Explain it without the pageWrite your own explanation before asking for feedback. Your draft and any hints stay while this page is open.
Practice · Variational Autoencoders
Try the idea in your own words
A latent-variable model trained by maximizing the evidence lower bound (ELBO). The bound falls short of the log-likelihood by the KL divergence from the encoder to the true posterior, so the encoder learns approximate inference, not just compression.
Concept · Selected for practice
Variational Autoencoders
What it rests on: Sources: Auto-Encoding Variational Bayes; Stochastic Backpropagation and Approximate Inference in Deep Generative Models; beta-VAE: Learning Basic...
Context and links
Generative Models
Explain the mechanism
For Variational Autoencoders: What is the smallest example of Variational Autoencoders you can work through by hand, and what does it show? Explain your answer, including what changes, why, and which assumption matters.
A rough first thought is enough. Your draft stays when you change tasks.
Your draft stays on this page and is cleared when you leave or reload.
A little help · Explain
Open one hint at a time. These are suggestions, not your answer or a grade.
0 of 3 hints shown for this question.
Clearing your answer keeps this help record. Outside help is your own report; this page cannot check it.
Where am I stuck? (optional)
Take your draft to a feedback conversation
No AI feedback runs here. You can copy a prompt to use elsewhere; nothing is sent automatically. Review the text before sharing, and leave out private information.
Write an attempt before copying a feedback prompt.
A draft, or an AI reply to it, is not a test of what you have learned. To check that, try a different case later without help.
Ask about this pagePick one item on this page and copy a prompt about it into an AI assistant you already use. Notes you write here stay in this browser.
assert abs(gap - kl_post) < 1e-10Your question
Choose what your question is about
Pick the idea, equation, source, code, claim, misconception or demo state you want to ask about. The prompt you copy includes it, so the answer can stay on that item.Open the draft below to save one note and next action in this browser.
Variational Autoencoders
What is the smallest example of Variational Autoencoders you can work through by hand, and what does it show?
Fixed prompts generated from this item. They are not comments from people or independent reviews.
The 5 cited sources must support this exact item, not just the surrounding topic.
Connect the definition to one equation, piece of code or demo before widening the discussion.
Change one input, then check that the same relationship holds in the mathematics, the code and the demo.
You can state the mechanism in your own words
Local action draftNo local draft saved yetOpen when you are ready to write one next action
This draft stays in this browser and is attached to this item only.
- What the 5 cited sources say about this exact item
- The definition, its prerequisites, and a contrasting concept
- The equation or code that makes the concept concrete
- One demo state that shows what stays the same as the inputs change
- You can state the mechanism in your own words
- You can name the prerequisite that would clear up the confusion
- You can predict how the result changes when one input changes
Reference id
concept/concept-notebook/generative-models/vaes
concept:generative-models/vaes