transformer_encoder
transformer_encoder
¶
Transformer encoder that takes as input a chunk of actions plus observation tokens and uses a transformer encoder with a CLS token to produce a latent embedding (split into mean and log variance), which is then reparameterized to produce a latent sample.
VAETransformerEncoder
¶
VAETransformerEncoder(embedding_dimension, latent_dimension, prediction_horizon, observation_horizon, device, number_of_heads=8, feedforward_dimension=512, number_of_encoder_layers=4, activation=value, dropout_rate=0.1, attention_dropout=0.0, normalization_type=value, attention_type=value, positional_encoding_type=None, exclude_keys=None, min_logvar=None, deterministic=False, mu_tanh_bound=None, max_logvar=None)
Bases: PosteriorLatentEncoder
Transformer-based posterior encoder for encoding actions into latent space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Transformer hidden dimension |
required |
number_of_heads
|
int
|
Number of attention heads |
8
|
feedforward_dimension
|
int
|
Feedforward network dimension |
512
|
number_of_encoder_layers
|
int
|
Number of transformer encoder layers |
4
|
activation
|
str
|
Activation function name |
value
|
dropout_rate
|
float
|
Dropout probability |
0.1
|
attention_type
|
str
|
Attention mechanism type (use AttentionType enum values) |
value
|
latent_dimension
|
int
|
Dimension of VAE latent space (z) |
required |
prediction_horizon
|
int
|
Number of action timesteps |
required |
device
|
str
|
Device to place encoder on |
required |
Initialize VAE latent action encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Dimension of the output embedding |
required |
latent_dimension
|
int
|
Dimension of VAE latent space, i.e. the dimension of the output z |
required |
prediction_horizon
|
int
|
Number of action timesteps |
required |
observation_horizon
|
int
|
Number of observation timesteps |
required |
device
|
str
|
Device to place encoder on |
required |
number_of_heads
|
int
|
Number of attention heads |
8
|
feedforward_dimension
|
int
|
Feedforward network dimension |
512
|
number_of_encoder_layers
|
int
|
Number of transformer encoder layers |
4
|
activation
|
str
|
Activation function name |
value
|
dropout_rate
|
float
|
Dropout probability |
0.1
|
attention_type
|
str
|
Attention mechanism type (use AttentionType enum values) exclude_keys: List of keys to exclude from encoding |
value
|
min_logvar
|
float | None
|
Minimum log variance for avoiding variance collapse |
None
|
deterministic
|
bool
|
If True, output deterministic embeddings without reparameterization. Use with MMD or OT regularizers instead of KL divergence. |
False
|
mu_tanh_bound
|
float | None
|
Optional symmetric bound for posterior mu. When set, applies
|
None
|
max_logvar
|
float | None
|
Optional maximum log variance for avoiding variance explosion. |
None
|
Source code in src/versatil/models/decoding/latent/posterior/transformer_encoder.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
get_auxiliary_output_keys
¶
Gaussian posterior keys, excluding logvar when deterministic.
Source code in src/versatil/models/decoding/latent/posterior/transformer_encoder.py
encode
¶
Encode actions into latent space using VAE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
dict[str, Tensor]
|
Dictionary of action tensors Shape: (B, prediction_horizon, action_dim) for each component |
required |
observations
|
dict[str, Tensor] | None
|
Optional observation features to condition encoding |
None
|
Note
Image observations are automatically excluded from encoding, plus any additional custom key.
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing: - LatentKey.POSTERIOR_LATENT: Latent embedding z (B, vae_latent_dimension) - LatentKey.POSTERIOR_MU: Latent distribution mean (B, vae_latent_dimension) - LatentKey.POSTERIOR_LOGVAR: Latent distribution log variance (B, vae_latent_dimension) - STATE_FEATURE_KEYS: Input observations used for encoding (dict or None) |
Source code in src/versatil/models/decoding/latent/posterior/transformer_encoder.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |