liveBook · Manning

Discrete code

As explained in Build a Text-to-Image Generator (from Scratch)

A discrete latent vector is a latent representation whose components are selected from a finite set of learned codebook vectors.

A discrete code is a latent representation selected from a finite learned codebook. Unlike a VAE's continuous, probabilistic latent vector, it uses the nearest codebook vector and can be stored as an integer index, improving interpretability and reconstruction sharpness. section 11.3.1 section 11.3.2

A VQ-VAE produces discrete codes through a sequential pipeline:

  • The encoder compresses an input into a grid of continuous latent vectors. section 11.3.2
  • Vector quantization replaces each vector with its nearest learned codebook embedding. section 11.3.1 section 11.3.2
  • The selected embeddings form the discrete latent code, while their indices form a sequence of integers. section 11.3.2
  • The decoder reconstructs the input from the quantized embeddings, using reconstruction and quantization losses during training. section 11.3.2

Encoding a fish image

A pretrained VQGAN converts a fish image into 576 integer tokens and decodes those tokens into a reconstruction that closely resembles the original. This demonstrates how an image's compressed visual information can be represented as a discrete sequence. section 11.1

Figure 11.5 The architecture of a VQ-VAE and its three components—encoder, codebook, and decoder—in the training process

Trace how the encoder, codebook, and decoder transform an image into quantized latent representations and back again. section 11.3.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Traces encoding, nearest-codebook quantization, decoding, and reconstruction and quantization losses.
Examples
Uses a fish image, 576 integer tokens, a pretrained VQGAN, and original-versus-reconstructed image comparisons.
What it assumes
Assumes familiarity with VAEs and GANs while defining codebooks, quantization, and discrete representations.
What you can do after
Understand and apply discrete image encoding and decoding through a pretrained VQGAN workflow.
How it connects
Connects vector quantization, codebooks, VQ-VAEs, GANs, perceptual loss, and transformer architectures.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.