Skip to content
AI360XpertBeta
Core ML

Visual explainer

Variational Autoencoders

Trace Gaussian latent codes, reparameterized sampling and the reconstruction–KL objective, then see why prior sampling can still fail.

Reconstructing known codes does not define how new codes should be sampled.
Reconstructing known codes does not define how new codes should be sampled.

A variational autoencoder combines a latent generative model with an encoder that approximates its posterior. Training balances reconstruction likelihood with a KL term; generation draws a latent code from the prior and then uses the decoder.

The opening picture shows a plain autoencoder with known codes −2 and 2 rebuilding A and B. The question mark at zero is an unchecked output, not proof that it must be invalid. Reconstruction alone does not supply a sampling distribution. A VAE explicitly models a prior p(z)p(z) and likelihood pθ(x∣z)p_\theta(x\mid z), alongside an approximate posterior qϕ(z∣x)q_\phi(z\mid x). The AEVB paper, sections 2–3 establishes this construction.

Encode A Gaussian Distribution

A Gaussian encoder predicts a mean and a positive standard deviation.
A Gaussian encoder predicts a mean and a positive standard deviation.

Use a one-dimensional illustrative posterior q(z∣x)=N(0.5,0.22)q(z\mid x)=\mathcal N(0.5,0.2^2). Its mean is μ=0.5\mu=0.5, standard deviation σ=0.2\sigma=0.2, and variance 0.040.04. An encoder can predict a log variance and transform it to a positive standard deviation. In multiple dimensions, a diagonal Gaussian is a modeling choice, not a requirement of every VAE.

The bracket spans one standard deviation on either side of the mean, from 0.30.3 to 0.70.7. It is not a safe decoding interval or a hard boundary: Gaussian tails extend beyond the cropped drawing. The density outline interpolates checked Gaussian landmarks; it is a visual approximation between them. This encoder distribution approximates pθ(z∣x)p_\theta(z\mid x), not the prior itself.

Sample Through A Transformation

Mean .5 plus standard deviation .2 times noise 1 gives a sample of .7.
Mean .5 plus standard deviation .2 times noise 1 gives a sample of .7.

Draw ϵ∼N(0,1)\epsilon\sim\mathcal N(0,1) and form z=μ+σϵz=\mu+\sigma\epsilon. For the illustrative draw ϵ=1\epsilon=1, z=0.5+0.2×1=0.7z=0.5+0.2\times1=0.7. The displacement arrow is 0.20.2 latent units long; the filled mint point is the sample, not a new mean.

Reparameterization puts the random draw outside the learned transformation. With the draw held fixed, the sample remains differentiable in encoder parameters. It does not remove randomness from training. Repeated draws from the same encoded input can yield different codes.

Compare Posterior And Prior

The posterior and standard normal prior differ in both center and spread.
The posterior and standard normal prior differ in both center and spread.

Choose prior p(z)=N(0,1)p(z)=\mathcal N(0,1). Both rulers use the same latent scale. The upper posterior has mean 0.50.5 and one-standard-deviation span 0.30.3 to 0.70.7; the lower prior has mean zero and span −1 to 1. This ruler is a wider view of latent space than the density plot; its two rows share one scale.

The Gaussian KL has a closed form:

KL⁡(q∥p)=12(μ2+σ2−1−ln⁡σ2).\operatorname{KL}(q\Vert p)=\frac12(\mu^2+\sigma^2-1-\ln\sigma^2).

Here it is 12(0.25+0.04−1−ln⁡0.04)=1.254437912\tfrac12(0.25+0.04-1-\ln0.04)=1.254437912 nats. Mean shift and spread mismatch both contribute. Matching the mean alone would still leave KL about 1.12941.1294; matching both μ=0,σ=1\mu=0,\sigma=1 gives zero. This term encourages compatibility with the prior, not guaranteed useful outputs at every location.

Generate Without Encoding An Input

Generation draws a code from the prior and uses the learned decoder.
Generation draws a code from the prior and uses the learned decoder.

The two source whiskers use the same scale and mark one standard deviation, not a complete set of draws. Reconstruction samples q(z∣x)q(z\mid x), conditioned on an observed input. Generation samples p(z)p(z) without an input photo. Both paths use the same decoder D, which defines pθ(x∣z)p_\theta(x\mid z).

The output labels identify two uses of the model, not guarantees of exact copies or realistic new images. Evaluate held-out reconstruction and prior samples separately. A low reconstruction error at encoder-produced codes does not by itself establish prior-sample quality.

Weight The KL Cost

Doubling beta doubles the KL contribution for a fixed posterior.
Doubling beta doubles the KL contribution for a fixed posterior.

The standard negative evidence lower bound is R+KL⁡(q∥p)R+\operatorname{KL}(q\Vert p), where R=Eq[−log⁡pθ(x∣z)]R=\mathbb E_q[-\log p_\theta(x\mid z)]. The beta-VAE variant uses R+βKL⁡(q∥p)R+\beta\operatorname{KL}(q\Vert p). Here assume R=0.8R=0.8 nats and hold the posterior fixed solely to compare the weighting.

At β=1\beta=1, total cost is 0.8+1.254437912=2.0544379120.8+1.254437912=2.054437912. At β=2\beta=2, it is 0.8+2.508875825=3.3088758250.8+2.508875825=3.308875825. The white segment stays fixed while the mint segment doubles in length. Retraining generally changes both terms. Beta does not promise a particular sharpness, disentanglement or reconstruction quality. The assumed RR is a separate objective illustration, not computed from the decoder example below.

Decode A Likelihood

The decoder turns a sampled code into parameters of an output distribution.
The decoder turns a sampled code into parameters of an output distribution.

For a concrete decoder, define the synthetic scalar likelihood p(x∣z)=N(2z,0.52)p(x\mid z)=\mathcal N(2z,0.5^2). At z=0.7z=0.7, it has mean 1.41.4, standard deviation 0.50.5, and a one-standard-deviation span from 0.90.9 to 1.91.9. The dot marks the mean; the whisker is not the full support and is not a sampled observation.

A decoder network generally predicts likelihood parameters. Returning its mean and sampling an observation from that likelihood are different operations. The target observation is unspecified here, so these numbers do not claim a reconstruction error or determine the preceding assumed expected NLL. Image blur depends on likelihood and decoder choices; randomness alone does not force it.

Hold Noise Fixed For Gradients

The pathwise effect of changing scale depends on the fixed noise draw.
The pathwise effect of changing scale depends on the fixed noise draw.

With μ=0.5\mu=0.5, compare σ=0.2\sigma=0.2 and 0.30.3 while holding each illustrative noise value fixed. For ϵ=0\epsilon=0, both codes equal 0.50.5: the old filled dot and new open ring coincide. For ϵ=1\epsilon=1, the code moves from 0.70.7 to 0.80.8. Both rulers share a latent scale; the lower shift is 0.10.1.

The pathwise derivatives are ∂z/∂μ=1\partial z/\partial\mu=1 and ∂z/∂σ=ϵ\partial z/\partial\sigma=\epsilon. Reconstruction gradients therefore flow through the decoder and this transformation to the encoder. There is no need to differentiate the random-number generator. This compares controlled parameter changes for a fixed draw, not an averaged training gradient or an optimizer step.

Try A Fresh Noise Draw

A fresh noise draw changes both the latent sample and the decoder mean.
A fresh noise draw changes both the latent sample and the decoder mean.

Restore σ=0.2\sigma=0.2 and draw ϵ=−1\epsilon=-1. Predict the latent sample first: z=0.5+0.2(−1)=0.3z=0.5+0.2(-1)=0.3. The same decoder gives mean 2(0.3)=0.62(0.3)=0.6 and standard deviation 0.50.5, so its whisker spans 0.10.1 to 1.11.1.

This output ruler uses the same position scale as the previous likelihood view: the mean moves left by 0.80.8 output units, from 1.41.4 to 0.60.6. The input and learned mapping have not changed; only the noise draw has. The output distribution still needs an additional draw if a sampled observation is wanted.

Where It Breaks

Posterior collapse can erase input information while the KL cost is zero.
Posterior collapse can erase input information while the KL cost is zero.

Consider an ideal collapsed boundary case: q(z∣A)=q(z∣B)=p(z)=N(0,1)q(z\mid A)=q(z\mid B)=p(z)=\mathcal N(0,1) and a decoder that ignores zz, p(x∣z)=N(0.6,0.52)p(x\mid z)=\mathcal N(0.6,0.5^2) for every code. Both input-conditioned distributions are identical, with KL zero. The flat decoder mean stays at 0.60.6 when the latent code changes from −1 to 1; the cross marks the absent influence.

In this case neither the encoded code distribution nor the decoder distinguishes the inputs. The random codes need not be numerically identical on individual draws; their distributions are identical. Encoder matching alone does not prove the decoder ignores zz: He et al., section 2.2 distinguishes inference collapse from full posterior collapse. Inspect latent usage and decoder sensitivity alongside KL, likelihood and samples. Zero KL alone is not evidence of a useful representation.

The Quick Version

  • A VAE defines a prior, likelihood and approximate posterior.
  • Standard deviation and variance are different quantities.
  • Reparameterization turns independent noise into a differentiable sample path.
  • KL penalizes posterior departure from the chosen prior.
  • Generation samples the prior, then uses the decoder.
  • Beta changes the weighting of a competing objective term.
  • Decoder parameters describe a likelihood, not necessarily one certain output.
  • Fixed noise exposes gradients through mean and scale.
  • Fresh noise can change the decoded distribution.
  • Posterior collapse can erase input information despite zero KL.