skip to content

Search

← From Noise to Image

Part I — Before Diffusion (2006–2020)

Variational Autoencoders

The ELBO, the reparameterization trick, and the first neural networks that could imagine — blurrily.

2013–2015 14 min read 3 papers

It is December 2013. Deep networks can classify images better than any previous method, but asking one to imagine a plausible new image — to sample from a model of the data rather than just discriminate it — remains mostly unsolved. A two-author paper posted to arXiv on December 20 shows how to train a directed latent-variable model and its approximate-inference network jointly, by backpropagation, using a trick as simple as adding noise after a multiplication. Within fourteen months the same idea has been rediscovered independently, generalized to deep hierarchies, and extended into a recurrent network that draws images stroke by stroke.

Where We Were

The previous chapter (The Generative Problem) framed the task: learn a model of p(x)p(x) that can both score images and sample new ones. By the early 2010s, deep learning had produced excellent discriminative models but the generative side of the field was still leaning on machinery from the 1990s. Directed latent-variable models — a prior over an unobserved code zz, a likelihood p(x∣z)p(x \mid z) generating the observed data — were the natural framework, but they carried a structural cost: for anything but the simplest priors and likelihoods, the true posterior p(z∣x)p(z \mid x) is intractable, because computing it requires integrating over every possible zz.

Two families of workaround existed. The wake-sleep algorithm for Helmholtz machines (Hinton, Dayan, Frey & Neal, 1995) trained a recognition network and a generative network in alternating passes, but the two networks optimized different objectives that did not correspond to a single joint bound — an awkward, two-headed compromise. Sampling-based alternatives such as Monte Carlo EM used expensive iterative sampling per datapoint, which scaled poorly to large datasets and did not amortize: every new image required fresh inference work from scratch. Mean-field variational Bayes offered a principled bound but usually demanded closed-form expectations that were themselves intractable for anything built from a multi-layer neural network. Deep belief nets and deep Boltzmann machines (Deep Belief Nets) sidestepped some of this with layer-wise pretraining, but inference in an undirected model brings its own expensive machinery.

The missing piece was a way to train an inference network — a function approximating p(z∣x)p(z \mid x) — jointly with the generative model, using plain stochastic gradient descent, with variance low enough to actually converge. The obvious estimator for a gradient through a sampling step, the score-function or REINFORCE-style estimator, has notoriously high variance. The field needed a different way to route gradients through a random variable.

Six attempts to get a gradient through a random variable, one that worked Wake-sleep and stacked Boltzmann machines worked around the intractable posterior. Reparameterization removed the need for a workaround in December 2013. Grey monospace text names the paper's own number or mechanism for that year. 1995 Wake-sleep Helmholtz machines two networks, two objectives no single joint bound 2006 Deep belief nets layer-wise pretraining composes RBMs into a stack inference still needs MCMC 2009 Deep Boltzmann nets undirected, two-way links joint sampling harder still deeper stack, harder MCMC 2013 VAE reparameterization trick tractable, low variance SGVB estimator, Dec 2013 2014 DLGM deep latent Gaussian model generalizes to L layers Bonnet and Price theorems 2015 DRAW recurrent, attention-based writes onto shared canvas 80.97 nats, best on MNIST workaround, still expensive to run reparameterized, trains by plain backpropagation Every card is a different answer to the same problem: get a low-variance gradient through a sampling step. Only 2013 removed the workaround entirely.
Every card is a different answer to the same problem: get a low-variance gradient through a sampling step. Only 2013 removed the workaround entirely. PNG ↓

The Breakthrough

Auto-Encoding Variational Bayes (Kingma & Welling, December 2013) supplied the missing piece by changing where the randomness sits, in what the paper calls, plainly, “quite simple” (Kingma & Welling, 2013). Rather than sampling zz directly from a distribution parameterized by the network — a step through which gradients cannot flow cleanly — the model samples a fixed noise variable ϵ\epsilon from a distribution with no learned parameters, and computes zz as a deterministic, differentiable function of ϵ\epsilon and the network’s output. This is the reparameterization trick. Because the only source of randomness is now independent of the parameters being optimized, ordinary backpropagation flows through the sampling step, and the resulting gradient estimator — the paper names it the Stochastic Gradient Variational Bayes (SGVB) estimator — has far lower variance than the score-function alternative.

The paper packages this trick into the Auto-Encoding VB (AEVB) algorithm: a neural network (the encoder or recognition model) maps a datapoint xx to the parameters of an approximate posterior qϕ(z∣x)q_\phi(z \mid x); a sample zz is drawn via reparameterization; a second network (the decoder) maps zz back to a distribution over xx. Both networks train jointly by ascending a single objective, the evidence lower bound, computed on random minibatches. When the recognition and generative networks are both neural networks, Kingma and Welling give this construction a new name: the variational auto-encoder.

The design compresses cleanly into an architecture diagram, and the connection to ordinary autoencoders is deliberate: the KL term against the prior is what turns a bottleneck into a generative latent space rather than a mere compression code.

One training image becomes a distribution, then a new image Reparameterization lets a gradient pass through the sampling step. The KL term is what keeps the latent space usable rather than a lookup table. Grey monospace text names the concrete operation at each step. Input image x one training example 28x28 pixel image Encoder network maps x to q(z|x) params recognition network q_phi Mean and std dev two vectors from encoder diagonal Gaussian params Reparameterize z = mean + std * noise differentiable end to end Latent sample z passed to the decoder one sample, one pass Noise epsilon, standard normal no learned parameters KL regularizer pulls q(z|x) toward prior keeps latent space usable Decoder network maps z back to pixel space reconstruction likelihood Reconstruction x-hat compared to original x pixel-wise loss term data network step distribution parameters and the regularizer The KL term applies to the encoder's output distribution, not to the decoder path; both trace back to the same sample z.
The KL term applies to the encoder's output distribution, not to the decoder path; both trace back to the same sample z. PNG ↓

Twenty-seven days later, a second group reached the same destination from a different direction. Stochastic Backpropagation and Approximate Inference in Deep Generative Models (Rezende, Mohamed & Wierstra, January 2014) derived what it calls Gaussian backpropagation from two classical results in probability — Bonnet’s theorem and Price’s theorem — to obtain gradient rules for expectations under a Gaussian, then showed those rules reduce to the same location-scale reparameterization (Rezende et al., 2014). The paper is explicit about the coincidence: “Concurrently with this paper, Kingma & Welling (2014) present an alternative discussion of stochastic backpropagation. Our approaches were developed simultaneously and provide complementary perspectives on the derivation of stochastic backpropagation rules.” Where Kingma and Welling built a two-layer encoder-decoder pair, Rezende, Mohamed and Wierstra generalized immediately to deep latent Gaussian models (DLGMs) — stacks of LL layers of Gaussian latent variables, each layer’s activation a nonlinear transform of the layer above it plus Gaussian noise, with an explicit recognition model regularized by injected bit-flip and dropout-style noise at the input to keep it from overfitting to seen data.

Thirteen months after that, DRAW — A Recurrent Neural Network For Image Generation (Gregor, Danihelka, Graves, Rezende & Wierstra, February 2015) — took the same encoder-recognition-decoder pairing and made it iterative (Gregor et al., 2015). Instead of one encode-sample-decode pass per image, DRAW runs a pair of recurrent networks for a fixed number of timesteps: at each step the encoder reads a piece of the target image, updates a latent code, and the decoder writes an update onto a shared canvas that accumulates across steps. The paper frames the motivation directly: a person asked to “recreate a visual scene” does so “in a sequential, iterative fashion,” with “rough outlines… gradually replaced by precise forms” — not in a single shot. DRAW added a second ingredient beyond recurrence — a differentiable spatial attention mechanism built from a grid of two-dimensional Gaussian filters, which lets the network choose, at each timestep, exactly where on the canvas to read from and write to. Because the attention window’s center and zoom are themselves smooth functions of the decoder’s hidden state, the whole system — recurrence, attention, and the underlying VAE machinery — trains end to end with the same reparameterized gradient Kingma, Welling, Rezende, Mohamed and Wierstra had introduced over the preceding fourteen months.

The Mathematics

Notation follows Kingma & Welling (2013): xx is an observed datapoint, zz is a continuous latent variable, pθ(z)p_\theta(z) and pθ(x∣z)p_\theta(x \mid z) are the generative model’s prior and likelihood with parameters θ\theta, and qϕ(z∣x)q_\phi(z \mid x) is the recognition model — the encoder — with parameters ϕ\phi.

The marginal log-likelihood of a single datapoint decomposes exactly, with no approximation, into two terms:

log⁡pθ(x)=DKL ⁣(qϕ(z∣x) ∥ pθ(z∣x))+L(θ,ϕ;x)\log p_\theta(x) = D_{KL}\!\left(q_\phi(z \mid x) \,\|\, p_\theta(z \mid x)\right) + \mathcal{L}(\theta, \phi; x)
SymbolMeaning
xxan observed datapoint, e.g. one image
zzcontinuous latent variable for that datapoint
θ\thetaparameters of the generative model, decoder
ϕ\phiparameters of the recognition model, encoder
qϕ(z∣x)q_\phi(z \mid x)approximate posterior produced by the encoder
pθ(z∣x)p_\theta(z \mid x)true posterior — intractable, never computed
DKLD_{KL}Kullback-Leibler divergence between two distributions
L(θ,ϕ;x)\mathcal{L}(\theta, \phi; x)the evidence lower bound, ELBO — what training maximizes

The ELBO itself can be rewritten in a form that separates a regularizer from a reconstruction term:

L(θ,ϕ;x)=−DKL ⁣(qϕ(z∣x) ∥ pθ(z))+Eqϕ(z∣x) ⁣[log⁡pθ(x∣z)]\mathcal{L}(\theta, \phi; x) = -D_{KL}\!\left(q_\phi(z \mid x) \,\|\, p_\theta(z)\right) + \mathbb{E}_{q_\phi(z \mid x)}\!\left[\log p_\theta(x \mid z)\right]

Sampling zz inside that expectation is exactly the step that ordinary backpropagation cannot pass through. The reparameterization trick rewrites the sample as a deterministic function of noise:

z=μϕ(x)+σϕ(x)⊙ϵ,ϵ∼N(0,I)z = \mu_\phi(x) + \sigma_\phi(x) \odot \epsilon, \qquad \epsilon \sim \mathcal{N}(0, \mathbf{I})
SymbolMeaning
zzthe latent sample actually passed to the decoder
μϕ(x)\mu_\phi(x)encoder’s output mean, function of xx and ϕ\phi
σϕ(x)\sigma_\phi(x)encoder’s output standard deviation
ϵ\epsilonauxiliary noise, sampled once per forward pass
⊙\odotelementwise, Hadamard, multiplication

When the prior and posterior are both Gaussian, the KL term integrates in closed form, and Kingma and Welling’s full per-datapoint training objective becomes:

L(θ,ϕ;x(i))≃12∑j=1J(1+log⁡((σj(i))2)−(μj(i))2−(σj(i))2)+1S∑s=1Slog⁡pθ ⁣(x(i)∣z(i,s))\mathcal{L}(\theta, \phi; x^{(i)}) \simeq \frac{1}{2}\sum_{j=1}^{J}\left(1 + \log\big((\sigma_j^{(i)})^2\big) - (\mu_j^{(i)})^2 - (\sigma_j^{(i)})^2\right) + \frac{1}{S}\sum_{s=1}^{S} \log p_\theta\!\left(x^{(i)} \mid z^{(i,s)}\right)
SymbolMeaning
JJnumber of latent dimensions
μj(i),σj(i)\mu_j^{(i)}, \sigma_j^{(i)}encoder’s mean and standard deviation, dimension jj, point ii
SSnumber of reparameterized samples per datapoint, per gradient step
z(i,s)z^{(i,s)}the ss-th reparameterized sample for datapoint ii

DRAW extends this same objective across timesteps rather than deriving a new one. With TT recurrent steps and a per-step diagonal-Gaussian latent, the summed KL term reduces to:

Lz=12(∑t=1Tμt2+σt2−log⁡σt2)−T2\mathcal{L}^{z} = \frac{1}{2}\left(\sum_{t=1}^{T} \mu_t^2 + \sigma_t^2 - \log \sigma_t^2\right) - \frac{T}{2}

What It Enabled

The direct, measurable payoff was inference that scaled and converged faster than every prior alternative. Kingma and Welling’s own comparison against wake-sleep and Monte Carlo EM on MNIST showed AEVB converging faster and to a better solution across every latent-dimensionality setting they tested, and doing so without the overfitting that adding more latent capacity would naively predict — a result the paper attributes to the regularizing effect of the KL term in the lower bound itself. Their MNIST and Frey Face experiments ran on ordinary CPU hardware, at what the paper reports as an effective 40 GFLOPS on a single Intel Xeon CPU — a detail worth sitting with given what training runs would look like within a decade.

The clearest single number is the progression in test-set negative log-likelihood on binarized MNIST, tabulated by Gregor et al. against every contemporary method:

ModelTest NLL, natsSource
Wake-sleep91.3as tabulated in Rezende et al., 2014
DLGM, diagonal covariance87.30Rezende et al., 2014
DLGM, rank-one covariance86.60Rezende et al., 2014
DRAW, without attention87.40Gregor et al., 2015
DRAW, with attention80.97Gregor et al., 2015

Lower is better — fewer nats needed to describe a held-out test image under the model. The single-shot DLGM, an amortized-inference latent Gaussian model with no recurrence, already beat wake-sleep by roughly four nats. Adding recurrence without attention gained almost nothing over the single-shot DLGM. Adding the differentiable attention mechanism on top of recurrence produced the largest jump on the table — a result the DRAW paper reads as evidence that where to look mattered as much as how many looks. The same recurrent, attentive DRAW architecture, trained on Street View House Numbers, produced samples the paper reports as visually indistinguishable from real data — a claim about 2015-era 64x64 house-number crops, not a general claim about DRAW’s later CIFAR-10 output, which the paper describes as blurry but structurally recognizable.

Pre-2013 latent-variable inferenceReparameterized VAE, 2013 onward
Gradient through the sampling stephigh-variance score-function estimators, or nonelow-variance, exact backpropagation
Training loop per datapointoften iterative, MCMC-style, expensivesingle forward and backward pass
Recognition and generative objectivesseparate, as in wake-sleep’s two phasesone joint evidence lower bound
Architecture depthshallow, one layer of latent variables typicalarbitrarily deep, DLGM generalizes to LL layers
Extension to sequential generationnot addressedDRAW, recurrence plus attention, February 2015

The lineage from here runs in several directions this book returns to. The discrete-latent counterpart to this chapter’s continuous Gaussian codes — VQ-VAE’s learned codebook — is covered in Pixels One at a Time. The evidence lower bound itself, generalized from a single denoising step to a full noise-adding Markov chain, becomes the training objective for diffusion models in Diffusion Foundations — a different generative mechanism built on the same inequality proved above. The perceptual-compression autoencoder sitting in front of every Stable Diffusion forward pass, turning a 512-pixel image into a small latent grid before diffusion ever runs, descends architecturally from the encoder-decoder pairing introduced here, as described in Latent Diffusion and Stable Diffusion. Outside this book, the same VAE block became the perception component of learned world models for control and simulation, a thread picked up in Predicting the Next Frame. And the alternative path taken by adversarial training, published within the same year, is the subject of The Adversarial Game.

One reparameterization trick, five later uses The encoder-decoder pair introduced for the VAE turns up again, unmodified in spirit, wherever a system needs a compact learned latent. Grey monospace text names the date or place the branch is covered. VAE, Dec 2013 reparameterized latent code Kingma and Welling DLGM deep Gaussian hierarchy Jan 2014, Gaussian backprop DRAW recurrent, attentive generation Feb 2015, sequential canvas VQ-VAE discrete codebook latents Chapter 5, 2017 World Models latent simulators for control video-generation book Latent diffusion perceptual compression stage Chapter 13, 2022 Attention-driven sequential, iterative drawing carried into later chapters the original result what it fed, in this book and beyond All five branches reuse the same encoder-decoder pairing. None of them descend from each other; they descend from the VAE directly.
All five branches reuse the same encoder-decoder pairing. None of them descend from each other; they descend from the VAE directly. PNG ↓

Key Papers

PaperAuthorsYearLinkWhy it matters
Auto-Encoding Variational BayesKingma & Welling2013arXiv:1312.6114Introduces the reparameterization trick, the SGVB estimator, and the term “variational auto-encoder”
Stochastic Backpropagation and Approximate Inference in Deep Generative ModelsRezende, Mohamed & Wierstra2014arXiv:1401.4082Independently derives the same reparameterization from Gaussian gradient identities; generalizes to deep latent Gaussian models
DRAW: A Recurrent Neural Network For Image GenerationGregor, Danihelka, Graves, Rezende & Wierstra2015arXiv:1502.04623Extends the VAE to a recurrent, differentiably-attentive process; best 2015 result on binarized MNIST

Papers referenced