The VQ-VAE turns each ECG into a sequence of discrete code indices (stage 1), and a separate
autoregressive model learns which index sequences are likely (stage 2). Generation samples indices
from the prior and decodes them. Code: src/ecgen/families/vqvae/, losses: src/ecgen/families/vqvae/losses.py.
Written reference: VQ-VAE + Prior.
Hover over the loss terms and the straight-through label.
β is on the codebook term here
The original VQ-VAE paper puts β on the encoder (commitment) term. In
VectorQuantizer.forward, β multiplies the term that updates the codebook, even though that
variable is named commitment_loss. The diagram shows what the code does.
PriorLightning loads the stage-1 checkpoint, freezes it, and turns every training ECG into its
625 code indices. PixelCNNPrior is then trained to predict each index from the ones before it.
Hover over a block. Use PriorLightning.compute_loss(indices); never shift the targets yourself — the shift is inside the model.
Hover over (or tap) a top node to see which codes it depends on. This toy stack has three layers with
dilations 1, 2, 4; the real prior has ten (1 … 512).
PixelCNNPrior.sample() fills the 625 positions left to right. At step t it runs the network on the
codes so far, takes the logits at t, divides by temperature, and draws one code. Then the frozen
VQ-VAE decodes all indices at once. Lower temperatures make the most likely code win more often.
The toy “prior” here prefers codes close to the previous one, so you can see the effect of temperature. Sampling 625 codes takes 625 network passes, which makes this the slowest generator of the three.