vector_quantization
vector_quantization
¶
Losses for vector-quantized latent variable models.
VQCommitmentLoss
¶
Bases: ScalarWeightedLoss
Commitment loss for vector-quantized latent variable models.
Penalizes the distance between the continuous encoder output and the quantized codebook vectors, preventing encoder outputs from drifting away from codebook entries. Reads pre-computed tensors from the predictions dict (put there by VQPosteriorEncoder).
Ref: van den Oord et al., "Neural Discrete Representation Learning" (2017)
Initialize VQ commitment loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_codes
|
int
|
Number of codebook entries per residual layer (K). Must match the VQ posterior's ResidualVQ configuration. |
required |
num_residual_layers
|
int
|
Number of residual VQ layers. Must match the VQ posterior's ResidualVQ configuration. |
required |
weight
|
float
|
Loss weight for the commitment term ||z_continuous - sg(z_quantized)||^2. |
1.0
|
Source code in src/versatil/metrics/losses/vector_quantization.py
get_required_keys
¶
forward
¶
Compute per-layer commitment loss between encoder outputs and quantized codebook vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Must contain: - LatentKey.VQ_Z_CONTINUOUS (L, B, code_dim): per-layer pre-quantization encoder outputs in code space. - LatentKey.VQ_QUANTIZED (L, B, code_dim, detached): per-layer hard-quantized codebook vectors. May also contain LatentKey.VQ_INDICES (list of L tensors shape (B,) with posterior choices) for codebook usage logging. |
required |
targets
|
dict[str, Tensor]
|
Unused. |
required |
is_pad
|
Tensor | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with weighted commitment loss averaged across layers |
LossOutput
|
(VQ-BeT style). Codebook usage metadata reports the |
LossOutput
|
fraction of total codebook capacity (num_codes * num_residual_layers) |
LossOutput
|
exercised across all layers in the batch. |
Source code in src/versatil/metrics/losses/vector_quantization.py
VQPriorCrossEntropyLoss
¶
Bases: ScalarWeightedLoss
Cross-entropy loss training a learned categorical prior to predict the posterior's codebook index choices.
Computes -log p(k|s) summed over residual VQ layers, where k is the index the posterior chose and p(k|s) is the prior's predicted categorical. This is the KL term in the ELBO for a delta posterior against a categorical prior.
Ref: van den Oord et al. (2017) train the prior in a second stage; this loss enables end-to-end joint training as a tighter ELBO.
Initialize VQ prior cross-entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Loss weight for the cross-entropy term. |
1.0
|
Source code in src/versatil/metrics/losses/vector_quantization.py
get_required_keys
¶
forward
¶
Compute cross-entropy between prior logits and posterior indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Must contain: - LatentKey.VQ_INDICES: list of (B,) long tensors (posterior choices) - LatentKey.PRIOR_CODE_LOGITS: list of (B, K) float tensors (prior logits) |
required |
targets
|
dict[str, Tensor]
|
Unused. |
required |
is_pad
|
Tensor | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with weighted cross-entropy loss. |