Vector-quantized VAE (VQ-VAE) spec #
VQ-VAE replaces a continuous latent sample with a discrete codebook lookup. This file exposes the core mechanism in a theorem-friendly way:
- an encoder produces a continuous latent
z_e(x); - a code index selects a codebook vector
z_q; - a decoder reconstructs from
z_q; - the loss combines reconstruction, codebook, and commitment terms.
The nearest-neighbor assignment is deliberately an explicit Fin numCodes argument. That keeps
the spec total and avoids hiding tie-breaking policy in the mathematical layer; runtime code can
compute the index however it likes and then pass the verified index into this spec.
Reference:
- van den Oord, Vinyals, and Kavukcuoglu (2017), "Neural Discrete Representation Learning".
The loss definitions below describe scalar values. Training also needs a rule for which
parameters receive each gradient. trainingGradients states the latent gradient estimator:
reconstruction passes through the encoder, codebook loss updates the selected embedding, and
commitment loss updates the encoder. The index remains fixed during that backward pass. This
estimator is specified separately from the classical derivative of a nearest-neighbor lookup.
NN.Runtime.Autograd.Model.VqVae implements these boundaries with the runtime's detach
operation and a straight-through decoder input. Callers supply the encoder, decoder, and code
assignment; there is no fixed-backbone VQ-VAE builder. The theorems in
NN.MLTheory.Generative.Latent.VQVAE establish loss decomposition, monotonicity in the commitment
weight, and nearest-code minimization for the value definitions.
Encoder producing the pre-quantized latent vector z_e(x).
- forward : TorchLean.Tensor α obs → TorchLean.Tensor α latent
Continuous encoder output before codebook lookup.
Instances For
Decoder mapping a codebook vector back to observation space.
- forward : TorchLean.Tensor α latent → TorchLean.Tensor α obs
Decode a quantized latent vector.
Instances For
VQ-VAE model: encoder, codebook, and decoder.
- encoder : Encoder α obs latent
Continuous encoder.
- codebook : Latent.Codebook α numCodes latent
Finite codebook.
- decoder : Decoder α latent obs
Decoder from quantized latent vectors.
Instances For
Pre-quantized latent z_e(x).
Instances For
Quantized latent z_q, using an explicit code index.
Instances For
VQ-VAE reconstruction from an explicit code assignment.
Instances For
Mean squared reconstruction error between dec(z_q) and the observation.
Instances For
Mean squared distance from the selected embedding to the encoder output.
This definition records the loss value. The training program treats the encoder output as a fixed target for this term, so only the selected codebook embedding receives its gradient.
Instances For
Mean squared distance from the encoder output to the selected embedding.
The training program treats the embedding as a fixed target for this term. Its gradient therefore
updates the encoder alone, with weight β in the total objective.
Instances For
VQ-VAE objective: reconstruction + codebook + β commitment.
Instances For
Cotangents at the encoder output and the selected codebook embedding for one assignment.
- encoder : TorchLean.Tensor α latent
Reconstruction cotangent plus the weighted commitment contribution.
- codebook : TorchLean.Tensor α latent
Codebook-loss contribution; other embeddings receive zero for this assignment.
Instances For
The VQ-VAE latent gradient estimator for a fixed code assignment.
Let g be the reconstruction cotangent at the decoder input and n the number of latent
coordinates. The straight-through rule sends g to the encoder and zero to the embedding.
Adding the two auxiliary terms gives
encoder = g + β * (2 / n) * (encoded - selected) and
codebook = (2 / n) * (selected - encoded).
mseDerivSpec supplies the same mean reduction as the value losses, including their empty-shape
convention. These are prescribed training signals. A hard nearest-neighbor assignment does not
have this classical derivative; the runtime constructs the estimator with explicit stop-gradient
boundaries. Decoder parameters receive their usual reconstruction gradients outside this pair.
Instances For
Quantization by explicit index is exactly codebook lookup.
The VQ-VAE objective decomposes into the three standard terms.