II · THE IDEA · ARTIFICIAL INTELLIGENCE
Vector-Quantized Variational Autoencoders (VQ-VAE)
▶ Listen · narrated
Likelihood models often yield blurry samples; adversarial ones can collapse. A learned codebook of discrete codes sits between them, giving sharp reconstructions a separate prior can still sample from.
At a glance
- What it is
- VAE whose latent space is a finite codebook of embedding vectors
- Quantisation
- Each encoder output is replaced by its nearest codebook entry
- Training trio
- Reconstruction loss, codebook loss, and a commitment term
- Generation
- A prior (often PixelCNN) is fitted afterwards over the discrete codes
- Hierarchy
- VQ-VAE-2 stacks codebooks at more than one spatial scale
Think of a paint-by-numbers kit whose palette is not printed in advance but learned from data. The encoder looks at an image and, at each patch, points to the nearest colour in that shared palette — the codebook. The decoder is only allowed to paint with those palette entries; it never sees the encoder’s raw continuous guesses. Training tugs three ways: the finished painting should match the original, each chosen palette colour should slide toward the encoder’s guess, and the encoder should keep guessing near colours that already exist so the palette is actually used. After the kit works, a second model learns which sequences of palette indices tend to appear in real images. To invent a new image you sample a grid of indices from that second model and hand them to the decoder. The palette is finite, so the second model deals in discrete choices rather than in fuzzy continuous coordinates — that is the whole point of the design.
VQ-VAE defines a latent codebook e ∈ R^{K×D}. An encoder E_φ maps x to z_e(x); quantisation sets z_q(x) = e_k where k = argmin_j ‖z_e(x) − e_j‖_2. The decoder D_θ reconstructs from z_q. The training objective combines reconstruction log p(x|z_q), a codebook term ‖sg[z_e] − e‖² that moves embeddings toward encoder outputs, and a commitment term β‖z_e − sg[e]‖² that keeps the encoder from drifting; sg is the stop-gradient operator. The argmin is non-differentiable, so a straight-through estimator copies ∂L/∂z_q into ∂L/∂z_e for the encoder path. After convergence, codes k are collected over the dataset and a prior p(k) (PixelCNN in the original work) is fitted; ancestral samples from p(k) are decoded for generation. VQ-VAE-2 introduces hierarchical latents: a top-level codebook models coarse structure and a bottom-level codebook, conditioned on the top codes, models local detail, with staged priors matching that hierarchy. Limitations include codebook collapse if commitment is mis-scaled, dependence on prior quality for sample fidelity, and the fact that the full generative model is not a single tight ELBO in the classical VAE sense because quantisation and the prior are staged.
Look closer
The latent is an index, not a continuous vector
The encoder still emits a continuous vector, but that vector is never passed to the decoder. It is replaced by the nearest entry in a finite table of embeddings — the codebook. What the decoder sees is therefore a discrete choice: which code, at which spatial position. Gradients cannot flow through the nearest-neighbour step in the ordinary way, so a straight-through estimator copies decoder gradients back to the encoder while the codebook itself is moved by a separate loss.
Three losses pull in different directions
Reconstruction asks the decoder to rebuild the input from the chosen codes. A codebook loss moves the selected embedding toward the encoder output. A commitment loss, scaled by a hyperparameter often called beta, pulls the encoder output toward the embedding it selected, so the encoder does not drift away from the table. Without commitment, the codebook can be under-used; without the codebook term, the embeddings lag behind the encoder.
The prior is deliberately separate
During autoencoding, codes are produced by the encoder. For unconditional generation a second model — in the original work a PixelCNN-style prior — is trained on sequences or maps of those codes. The autoencoder and the prior are not a single end-to-end likelihood in the usual VAE sense; the discrete bottleneck is what makes fitting a powerful prior tractable. VQ-VAE-2 extends this idea with hierarchical latents so coarse structure and fine detail can be modelled at different scales.
The story
A standard variational autoencoder compresses an input into a continuous latent distribution and reconstructs from a sample of that distribution. The continuous latent is convenient for gradient descent, yet it is an awkward partner for the kinds of prior that generate sharp, structured samples. PixelCNN-style models, for example, are natural over discrete symbols; over continuous vectors they are less so. Generative adversarial networks, by contrast, can produce crisp images, but they do not give a straightforward likelihood or a stable latent code one can edit and reuse.
Vector-quantised variational autoencoders attack that gap directly. The architecture keeps the encoder–decoder shell of a VAE, but inserts a learned codebook between the two. The encoder maps the input to a grid of continuous vectors. Each vector is replaced by the nearest prototype in a shared table of embeddings. The decoder sees only those chosen embeddings and must reconstruct the original signal — image, audio waveform, or other modality considered in the work. The latent that matters is therefore an array of discrete indices into the codebook, not a cloud of continuous coordinates.
Because nearest-neighbour selection has no useful gradient, training uses a straight-through estimator: decoder gradients are copied back to the encoder as if the quantisation step were the identity. The codebook itself is updated by a codebook loss that moves the selected embedding toward the encoder output. A commitment loss pulls the encoder the other way, discouraging it from wandering far from the table. Together with ordinary reconstruction error, these three terms keep encoder, codebook and decoder aligned without requiring an adversarial discriminator.
Once the autoencoder is trained, generation is a second stage. A prior is fitted to the discrete codes observed on the training set. In the original paper that prior is a PixelCNN-family model over the map of code indices. Sampling from the prior yields a field of codes; the frozen decoder turns those codes into an image or a waveform. The discrete bottleneck is what makes this second stage practical: the prior operates over a finite vocabulary rather than over continuous vectors.
VQ-VAE-2 pushes the same idea further by stacking codebooks at more than one resolution. A top-level codebook captures global structure; a lower-level codebook, conditioned on the top codes, fills in local detail. Priors are trained in stages to match that hierarchy. The result is a likelihood-oriented path to diverse, high-fidelity images that still rests on a learned discrete representation rather than on adversarial training alone.
The bridge the editorial angle names is therefore architectural, not rhetorical. Likelihood-based models gain a latent that is easy to model with powerful discrete priors; the reconstructions need not pay the usual continuous-VAE blur tax, because the decoder is fed sharp codebook vectors rather than noisy samples from a diagonal Gaussian. Whether that fully closes the gap with GANs depends on the prior and the codebook size — the papers are careful not to claim a universal win — but the discrete codebook is the concrete mechanism that makes the comparison possible.
Why it mattered then
When the first VQ-VAE paper appeared, the field was split between likelihood models that were stable to train but often produced blurry samples, and adversarial models that could look sharp yet were harder to evaluate and sometimes collapsed. A representation that was discrete by design, learned end-to-end with the autoencoder, and still usable by a separate pixel-level prior offered a third route. It also gave researchers a latent they could inspect as symbols rather than as unstructured continuous coordinates, which mattered for anyone who wanted to compose, edit or hierarchically model codes after the fact. VQ-VAE-2 then showed that stacking those codes at multiple scales was enough to push image fidelity much higher while staying inside the same non-adversarial training story.
Why it matters now
Discrete latent codebooks remain a standard building block wherever a model must compress a continuous signal into tokens a second network can read — image and audio tokenisers for large multimodal systems are direct descendants of this idea. The separation between a quantised autoencoder and a prior trained on its codes still structures how many pipelines are built: train the codebook until reconstructions are faithful, freeze it, then model the discrete sequences. Hierarchical quantisation, commitment losses and straight-through estimators continue to appear whenever someone needs a finite vocabulary over pixels or waveforms without handing the whole job to a GAN.
The surprising detail
The quantisation step that defines the method is, from the optimiser’s point of view, a lie told twice. No gradient passes through nearest-neighbour lookup; the straight-through estimator pretends the operation was the identity so the encoder can learn, while a separate codebook loss pretends the encoder output was a regression target so the table can move. The discrete latent everyone cites is therefore maintained by two surrogate objectives around a non-differentiable cut — and it works well enough that later hierarchical models reused the same cut rather than replacing it.
What is disputed
Claims about closing the fidelity gap with GANs depend on the prior, codebook size and hierarchy; the papers report strong results on their setups without establishing a universal ranking across all image and audio tasks.
Remember this
VQ-VAE replaces a continuous latent with nearest neighbours from a learned codebook, then trains a separate prior on the resulting discrete codes.
Test yourself
A colleague proposes folding the PixelCNN prior into the same end-to-end loss as the encoder, codebook and decoder, so everything trains jointly like a classical VAE. Which property of VQ-VAE would that change most, and what practical risk would you flag?
It would collapse the deliberate two-stage split: the discrete codes would no longer be a frozen vocabulary that a powerful prior can model in isolation. The practical risk is that the prior and the codebook start negotiating against each other — codes may drift to suit an immature prior, or the prior may never see a stable discrete distribution long enough to become sharp — undoing the reason the bottleneck was made discrete in the first place.
Go deeper
- [1711.00937] Neural Discrete Representation Learning · arxiv.org
- [1906.00446] Generating Diverse High-Fidelity Images with VQ-VAE-2 · arxiv.org
Image: Original diagram, The Daily Triptych. Licence: Original work. Source.