liveBook · Manning

Variational autoencoder

As explained in Math and Architectures of Deep Learning

An autoencoder that models the latent space probabilistically and is trained with regularizing constraints.

A variational autoencoder (VAE) is an autoencoder that learns a probabilistic latent representation. Like an ordinary autoencoder, an encoder compresses an input and a decoder reconstructs it. Unlike an ordinary autoencoder, the encoder emits parameters of a latent distribution rather than one fixed latent vector, adding constraints that make the learned space compact and smooth. section 14.7 section 14.7.1 section 14.8

For a Gaussian latent family, the encoder produces a mean μ and covariance or variance parameters. Training samples z from that distribution, then the decoder maps z back to a reconstruction. At inference time, the mean μ can be used directly as the input’s latent representation. A narrow distribution keeps sampled points near its mean, while stochastic mapping lets the latent representation model uncertainty around an input. section 14.7.1 section 14.7.3

The objective balances reconstruction against regularization. Reconstruction loss asks the decoder to preserve the input; KL-divergence loss pushes the encoder’s learned latent distribution toward a chosen prior, commonly a zero-mean, unit-variance Gaussian. Without that constraint, reconstruction alone can place encoded training points far apart. With it, the model is encouraged to occupy less latent-space volume. section 14.6 section 14.7.8

This objective can also be derived through the evidence lower bound (ELBO). Minimizing divergence between the encoder’s approximate posterior q(z|x) and the unknown posterior is equivalent to maximizing the ELBO. Its entropy term favors a diffuse approximate distribution, supporting smoothness, while its expected joint-probability term connects latent representations and observed inputs. section 14.7.3 section 14.7.6

Sampling appears to block backpropagation because a random draw is not differentiable. The reparameterization trick separates randomness from learned parameters: draw ε from a fixed standard Gaussian, then compute z = μ + εσ. Gradients can therefore flow through μ and σ while randomness comes from ε. The same idea extends from one Gaussian variable to multivariate Gaussians. section 14.7.8

MNIST latent-space comparison

With a two-dimensional latent space on unlabeled MNIST digit images, both an autoencoder and a VAE can reconstruct test images well. The reconstruction-only autoencoder learns a sparse, widely spread latent space. The VAE adds KL regularization toward a standard Gaussian, yielding a visibly more compact latent space constrained within a unit ball. section 14.7.8

A PyTorch VAE training path is:

  • Encode an image with convolutional layers, then produce separate μ and log-variance vectors. section 14.7.8
  • Reparameterize by scaling standard-normal noise and adding μ, then decode z with transposed convolutions. section 14.7.8
  • Optimize binary cross-entropy reconstruction loss plus a weighted KL-divergence term. section 14.7.8
Figure 14.6 Two examples of mapping from a 2D input space to a 1D latent space. Both show regularized solid) vs. unregularized (dashed) latent space manifolds. Solid little circles depict training data points.

(a) Linear latent space

(b) Curved latent space

Compare regularized and unregularized mappings from a two-dimensional input space into one latent dimension; the regularized mappings keep training representations more compact. section 14.6

How Math and Architectures of Deep Learning covers it

How deep it goes
Derives ELBO-based training, explains stochastic encoding and reparameterization, and implements convolutional PyTorch components.
Examples
Compares VAE and autoencoder reconstructions and two-dimensional MNIST latent spaces, alongside PyTorch encoder, decoder, loss, and training listings.
What it assumes
Builds from autoencoder reconstruction and relies on previously introduced KL divergence, entropy, and Bayesian probability concepts.
What you can do after
Build it from scratch with a reparameterized encoder, decoder, reconstruction loss, and weighted KL-divergence loss in PyTorch.
How it connects
Connects autoencoders, latent variables, Gaussian distributions, KL divergence, Bayes’ theorem, ELBO, and backpropagation.

Also explained in

Compare how 13 books explain variational autoencoder →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.