Bare discrete-latent auto-encoder — encoder, codebook, decoder.
Implements the VQ-VAE of van den Oord, Vinyals, and Kavukcuoglu, 2017.
An image is encoded to a spatial field of continuous vectors, each
snapped to its nearest entry in a learned codebook of
VQVAEConfig.num_embeddings codes, and decoded back. The
quantisation step is non-differentiable, so forward routes the
decoder's gradient onto the encoder with the straight-through
estimator.
This class computes no loss. Use
VQVAEForImageGeneration for the training objective, or read
the codebook_loss / commitment_loss terms off the returned
VQVAEOutput and combine them yourself.
The pair encode_indices / decode_indices is the
tokeniser interface: images in, an integer field out, and back again.
Downstream discrete models over the latent grid — the paper's own
PixelCNN prior among them — consume exactly that.
Parameters
configVQVAEConfigVQVAEConfig.Attributes
encodernn.Module(B, embedding_dim, H', W').quantizernn.VectorQuantizer(num_embeddings, embedding_dim) entries.decodernn.ModuleNotes
Reference: van den Oord, Vinyals, and Kavukcuoglu, "Neural Discrete Representation Learning", NeurIPS, 2017 (arXiv:1711.00937).
The straight-through estimator used here is
which equals in the forward pass and has the identity Jacobian with respect to in the backward pass.
Examples
>>> import lucid
>>> from lucid.models.generative.vqvae import VQVAEConfig, VQVAEModel
>>> cfg = VQVAEConfig(sample_size=32, hidden_channels=32,
... residual_hidden_channels=32, embedding_dim=16,
... num_embeddings=64)
>>> model = VQVAEModel(cfg).eval()
>>> x = lucid.randn((1, 3, 32, 32))
>>> out = model(x)
>>> out.sample.shape, out.indices.shape
((1, 3, 32, 32), (1, 8, 8))Used by 2
Constructors
1Properties
2Instance methods
6Decode a quantised latent field back to image space.
Detokenise an index field (B, H', W') back to an image.
Return the continuous pre-quantisation latent z_e(x).
Tokenise x into codebook indices shaped (B, H', W').
forward(x: Tensor)quantize(z_e: Tensor)Quantise an (B, D, H', W') latent field.
lucid.nn.VectorQuantizer follows the framework's
trailing-axis convention, so the channel axis is moved last for
the lookup and moved back afterwards; the returned quantized
is in image layout, while indices is (B, H', W').