vq_encoder
vq_encoder
¶
Posterior encoder with vector quantization for discrete latent variable models.
Uses the same transformer backbone as VAETransformerEncoder to produce a continuous embedding from actions and observations, then quantizes it via ResidualVQ to produce a discrete latent code. The quantized embedding is passed to the decoder via the VariationalAlgorithm. Commitment loss inputs (continuous z and quantized z) are stored in the output dict for external loss computation in the metrics module.
VQPosteriorEncoder
¶
VQPosteriorEncoder(latent_dimension, num_codes, num_residual_layers, embedding_dimension, prediction_horizon, observation_horizon, device, ema_decay=0.99, dead_code_threshold=1.0, number_of_heads=4, feedforward_dimension=128, number_of_encoder_layers=1, activation=value, dropout_rate=0.0, attention_dropout=0.0, normalization_type=value, attention_type=value, positional_encoding_type=None, exclude_keys=None)
Bases: PosteriorLatentEncoder
Transformer posterior encoder with residual vector quantization.
Encodes actions and observations into a continuous embedding via a transformer encoder with a CLS token, then quantizes the embedding through a ResidualVQ bottleneck. The quantized output is the latent z passed to the decoder. The continuous pre-quantization embedding and codebook indices are stored in the output dict for commitment loss computation and prior training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimension
|
int
|
Dimension of each codebook vector and the latent space passed to the decoder. |
required |
num_codes
|
int
|
Number of codebook entries per residual layer (K). |
required |
num_residual_layers
|
int
|
Number of cascading VQ layers. |
required |
ema_decay
|
float
|
EMA decay for codebook updates. |
0.99
|
dead_code_threshold
|
float
|
Cluster size below which codes are replaced. |
1.0
|
embedding_dimension
|
int
|
Transformer hidden dimension. |
required |
prediction_horizon
|
int
|
Number of action timesteps. |
required |
observation_horizon
|
int
|
Number of observation timesteps. |
required |
device
|
str
|
Device string. |
required |
number_of_heads
|
int
|
Number of attention heads. |
4
|
feedforward_dimension
|
int
|
Feedforward network dimension. |
128
|
number_of_encoder_layers
|
int
|
Number of transformer encoder layers. |
1
|
activation
|
str
|
Activation function name. |
value
|
dropout_rate
|
float
|
Dropout probability. |
0.0
|
attention_type
|
str
|
Attention mechanism type (use AttentionType enum values). |
value
|
exclude_keys
|
list[str] | None
|
Observation keys to exclude from encoding. |
None
|
Source code in src/versatil/models/decoding/latent/posterior/vq_encoder.py
get_auxiliary_output_keys
¶
VQ posterior outputs quantized latent, codebook indices, and continuous z.
Source code in src/versatil/models/decoding/latent/posterior/vq_encoder.py
encode
¶
Encode actions into a vector-quantized latent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
dict[str, Tensor]
|
Dictionary of action tensors, shape (B, prediction_horizon, action_dim) per component. |
required |
observations
|
dict[str, Tensor] | None
|
Optional observation features for conditioning. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing: - LatentKey.POSTERIOR_LATENT: Quantized latent z (B, code_dim), with straight-through gradient for decoder training. - LatentKey.VQ_INDICES: Per-layer codebook indices, list of (B,) tensors, for prior training. - LatentKey.VQ_Z_CONTINUOUS: Per-layer pre-quantization encoder outputs in code space (L, B, code_dim). Carries gradient; paired with VQ_QUANTIZED for commitment loss. - LatentKey.VQ_QUANTIZED: Per-layer hard-quantized codebook vectors in code space (L, B, code_dim), detached. Used with VQ_Z_CONTINUOUS for per-layer commitment loss. |
Source code in src/versatil/models/decoding/latent/posterior/vq_encoder.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |