base_prior
base_prior
¶
Abstract base class for latent prior networks.
PriorLatentEncoder
¶
Bases: ModuleAttrMixin, ABC
Abstract base class for prior parametrizations over a latent space z, which can be either learned (through a NN) or fixed.
Latent priors model the conditional distribution p(z|s) where z is a latent variable and s is an optional conditioning of observations (if the prior distribution is learned).
Design
- forward() takes target latents and conditioning, returns predictions and targets
- sample_prior() generates latent samples for inference
- Loss computation happens in separate loss modules (not in forward())
Initialize latent prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimension
|
int
|
Dimension of latent variable z |
required |
device
|
str
|
Device to place prior on |
required |
Source code in src/versatil/models/decoding/latent/prior/base_prior.py
forward
abstractmethod
¶
Compute prior predictions for training.
This method takes clean latent samples (typically from a posterior encoder) and conditioning, and returns predictions and targets for loss computation. The actual loss is computed externally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_latents
|
Tensor | None
|
Clean latent samples, shape (B, latent_dim), or None when called from sample_prior() on learned priors. These are sampled from the approximate posterior q_phi(z|a,s) and should be detached to prevent gradients flowing twice to the approximate posterior. |
required |
observations
|
dict[str, Tensor]
|
Dictionary of conditioning features, typically the observations at current state. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing predictions and targets for loss computation. |
dict[str, Tensor]
|
Keys depend on the specific prior type (e.g., "predicted_prior", "target_prior"). |
Source code in src/versatil/models/decoding/latent/prior/base_prior.py
get_auxiliary_output_keys
¶
Return the set of keys this prior adds to the predictions dict.
Base implementation returns keys common to Gaussian priors. Subclasses override to add or remove keys.
Source code in src/versatil/models/decoding/latent/prior/base_prior.py
build_training_target
¶
Select and detach the posterior latent used to train this prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
posterior_output
|
dict[str, Tensor]
|
Posterior encoder output dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Detached posterior sample used as the default prior target. |
Source code in src/versatil/models/decoding/latent/prior/base_prior.py
sample_prior
abstractmethod
¶
Sample latent variable from learned prior p(z|s).
During inference, we don't have ground-truth actions (and thus no posterior samples), so we sample directly from the (conditional) prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Number of samples to generate |
required |
observations
|
dict[str, Tensor] | None
|
Optional dictionary of conditioning features |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Sampled latent embeddings, shape (batch_size, embedding_dimension) |