Skip to content

Variational Autoencoder

An autoencoder whose compressed space is smooth and continuous, so you can sample or walk through it to generate new, realistic data.

Also VAE · latent space

Fig. / How it works
inputsample a codeoutput
Domain 06
Generative AI
Role · kind
Models and algorithmsModel
Level
Advanced

Kingma and Welling, 2013

A map of every possible handwritten digit, where nearby places look alike. Walk slowly from the '1' neighborhood to the '7' neighborhood and you watch one digit morph smoothly into the other.

Where it fits

+All relationships as a list

See it on the full map

How it works

A VAE keeps the encoder-decoder shape of an autoencoder but changes what the encoder outputs. Instead of one exact code, it outputs a small cloud: a center and a spread. During training, a random point is drawn from that cloud and decoded.

Two forces shape training:

  1. Reconstruction: the decoded point should look like the input.
  2. Regularity: every cloud should stay close to a standard bell curve around the origin.

The second force pushes all the clouds to overlap and fill the space, with no empty gaps. The result is a latent space where every point decodes to something plausible and nearby points decode to similar things. Now you can generate: sample a random point, decode it. Or interpolate: walk in a straight line between two codes and watch one image morph into another.

VAE images are often a bit blurry, and GANs and diffusion models produce sharper results. But VAEs remain important: Stable Diffusion uses an autoencoder of this kind to shrink images before running diffusion.

The math

The VAE maximizes the evidence lower bound (ELBO):

L(x)=Eqϕ(z∣x)[log⁡pθ(x∣z)]−KL(qϕ(z∣x) ∥ N(0,I))\mathcal{L}(x) = \mathbb{E}_{q_\phi(z \mid x)}\big[\log p_\theta(x \mid z)\big] - \mathrm{KL}\big(q_\phi(z \mid x) \,\|\, \mathcal{N}(0, I)\big)

The reparameterization trick writes z=μ+σ⊙ϵz = \mu + \sigma \odot \epsilon with ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, I), so gradients flow through μ\mu and σ\sigma.

Everyday use cases

  • Case 01

    Morphing handwritten digits or faces

    Interpolating between two points in the latent space produces a smooth transition.

  • Case 02

    Inside Stable Diffusion

    Stable Diffusion uses a VAE-style autoencoder to compress images into a small latent space where diffusion runs.

Famous examples

Strengths, weaknesses and when to use it

Strengths

  • Smooth, meaningful latent space you can sample and interpolate.
  • Stable training with a clear objective.

Weaknesses

  • Generated images tend to be blurrier than those from GANs or diffusion models.

When to use itWhen you need a well-organized latent space (for interpolation, compression or as the first stage of a latent diffusion model).

Compare with

Check yourself

  1. Q01

    What does a VAE's encoder output, instead of a single code?

    Show answer

    B: A distribution (mean and variance) over codes

  2. Q02

    Why can you generate new data with a VAE but not reliably with a plain autoencoder?

    Show answer

    A: The VAE's latent space is pushed to be smooth and filled in, so random points decode to sensible outputs

Go deeper