variational
variational
¶
Variational inference wrapper for decoding algorithms.
This module provides a compositional wrapper that adds variational inference capabilities to any base decoding algorithm. Variational Inference enables modeling of multi-modal action distributions by introducing a latent variable z that is conditioned on both observations and actions: p(a|s) = p(a|z,s) p(z|s) Where: - p(a|z,s) is the base algorithm's decoder (BC, FlowMatching, etc.) - p(z|s) is the learned conditional prior (Gaussian or learned through a NN)
VariationalAlgorithm
¶
VariationalAlgorithm(base_algorithm, posterior_encoder, prior=None, sampling_from_prior_probability=0.0, posterior_decoder_noise_std=0.0)
Bases: DecodingAlgorithm
Compositional wrapper adding variational inference to any decoding algorithm.
Wraps a base algorithm with variational inference, enabling multi-modal action prediction through a learned posterior q_phi(z|a,s) and prior p(z|s).
Training
- Encode actions via approximated posterior: z ~ q_phi(z|a,s)
- Train prior to match posterior (if learned prior)
- Decode actions via posterior + decoding algorithm: p(a|z,s)q_phi(z|a,s)
Inference
- Sample latent from prior: z ~ p(z|s)
- Decode actions via base algorithm: p(a|z,s)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_algorithm
|
DecodingAlgorithm
|
The underlying decoding algorithm (BC, FlowMatching, Diffusion, etc.) |
required |
posterior_encoder
|
PosteriorLatentEncoder
|
Latent action encoder for posterior q_phi(z|a,s) (e.g., a Transformer Encoder) |
required |
prior
|
PriorLatentEncoder | None
|
Latent prior for p(z|s). If None, auto-creates GaussianPrior. |
None
|
sampling_from_prior_probability
|
float
|
Probability of sampling from prior during training. |
0.0
|
posterior_decoder_noise_std
|
float
|
Fixed Gaussian jitter added to posterior latents before decoder training. This is applied only when training decodes from the posterior, not when decoding from prior samples. |
0.0
|
Initialize variational algorithm wrapper.
Source code in src/versatil/models/decoding/algorithm/variational.py
predicts_in_action_space
property
¶
Delegates to the wrapped base algorithm.
__setstate__
¶
Restore module state and reconnect posterior-owned prior references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
Pickled or deep-copied module state produced by PyTorch. |
required |
Source code in src/versatil/models/decoding/algorithm/variational.py
get_callbacks
¶
Provide callbacks for variational latent monitoring and prior fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_config
|
ExperimentConfig
|
Experiment-level callback configuration. |
required |
Returns:
| Type | Description |
|---|---|
list
|
Callbacks required by the variational algorithm. |
Source code in src/versatil/models/decoding/algorithm/variational.py
get_auxiliary_output_keys
¶
Variational algorithm adds latent variable keys to the output.
Source code in src/versatil/models/decoding/algorithm/variational.py
injected_feature_keys
¶
Latent keys added on top of the base algorithm's injections.
Source code in src/versatil/models/decoding/algorithm/variational.py
forward
¶
Forward pass with variational latent encoding.
Training mode (self.training=True): - Encodes actions via approximated posterior q_phi(z|a,s) - Trains learned prior to match approximated posterior (if learned) - Stochastic mixing: samples from prior with probability p_prior - Delegates to base algorithm
Validation/eval mode (self.training=False): - Still encodes posterior for loss computation (KL term) - But uses ONLY prior samples for action decoding (like inference) - Better evaluates actual inference performance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
ActionDecoder
|
Action decoder network |
required |
features
|
dict[str, Tensor]
|
Dictionary of input features from encoding pipeline |
required |
actions
|
dict[str, Tensor] | None
|
Optional ground-truth actions (for training) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing: - Action predictions from base algorithm - Latent variables (mu, logvar) if training - Prior outputs (prior_prediction, prior_target) if learned prior |
Source code in src/versatil/models/decoding/algorithm/variational.py
get_targets
¶
Delegate to the base algorithm's target selection.
Source code in src/versatil/models/decoding/algorithm/variational.py
predict
¶
Inference mode: sample latent z from prior and decode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
ActionDecoder
|
Action decoder network |
required |
features
|
dict[str, Tensor]
|
Dictionary of input features |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing action predictions |