pavi.distributions¶
pavi.distributions.affine_modules
¶
Affine transforms: the building block of PAVI's normalizing flows.
With a standard Normal base distribution, an affine transform yields a Gaussian approximate posterior; with a diagonal scale, this is the mean-field approximation. This module provides:
AffineModule(factoryaffine): learnable location and scale, for sites outside plates.ConditionalAffineModule(factoryconditional_affine): location and scale produced from the instance's encoding by shared networks. This is PAVI's plate-amortized transform,z = beta(E) + alpha(E) * u.
Both support a diagonal scale (scale_type="diag") or a lower-triangular
Cholesky factor (scale_type="tril", full covariance).
Note: importing this module patches
pyro.distributions.transforms.LowerCholeskyAffine.log_abs_det_jacobian
process-wide, so that it handles batched inputs.
AffineModule
¶
Bases: PyroTransformModule
An affine (location-scale) transform with configurable scale structure.
Supports two scale parameterizations
"diag": Diagonal scale matrix (mean-field approximation). Each event dimension has an independent scale parameter."tril": Full lower-triangular Cholesky factor. Captures pairwise dependencies between event dimensions.
The transform applies the standard affine operation: y = x * scale + loc.
When scale_type="tril", the operation is y = x @ scale_tril + loc.
scale
property
¶
Compute the scale tensor from registered parameters.
When scale_type="diag", returns the diagonal scale with
scale_eps added for numerical stability.
When scale_type="tril", assembles a full lower-triangular matrix
from the diagonal and off-diagonal (nondiag) scale parameters.
Returns:
| Type | Description |
|---|---|
Tensor
|
The scale tensor: a 1-D tensor for diagonal mode, or a |
Tensor
|
2-D lower-triangular matrix for triangular mode. |
ConditionalAffineNN
¶
Bases: Module
Neural network that maps context tensors to affine transform parameters.
This network learns to predict location (shift) and scale (multiplicative)
parameters conditioned on an external context tensor. Two scale
parameterizations are supported:
- "diag": Predicts a vector of per-dimension scales.
- "tril": Predicts a full lower-triangular matrix, including
both diagonal and off-diagonal elements.
Attributes:
| Name | Type | Description |
|---|---|---|
event_dim |
Number of event dimensions (output dimensionality). |
|
scale_eps |
Small constant added to the scale for numerical stability. |
ConditionalAffineModule
¶
Bases: ConditionalTransform, PyroModule
A conditional affine transform driven by a neural network.
Wraps a ConditionalAffineNN that maps context tensors to location
and scale parameters, then constructs the appropriate Pyro transform
(diagonal or triangular affine) from those parameters.
This module is designed for use in conditional normalizing flows where the transform parameters depend on an external context (e.g., plate encodings).
Attributes:
| Name | Type | Description |
|---|---|---|
bijective |
Always |
|
scale_type |
Literal['diag', 'tril']
|
Either |
condition(context)
¶
Build and return an affine transform conditioned on the given context.
The internal neural network predicts location and scale parameters from the context, which are then used to construct either a diagonal or lower-triangular affine transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Tensor
|
Conditioning tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Transform
|
A Pyro |
Transform
|
diagonal mode or |
Transform
|
ready to be applied to data. |
log_abs_det_jacobian(self, x, y)
¶
Monkey-patched Jacobian determinant computation for batched inputs.
Overrides LowerCholeskyAffine.log_abs_det_jacobian to correctly handle
batched transforms. Instead of computing the full Jacobian matrix, this
function extracts the log of the diagonal elements of the scale_tril
matrix (which determine the determinant for triangular transforms) and
broadcasts them to match the input batch shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
The |
required | |
x
|
Input tensor to the transform. |
required | |
y
|
Output tensor from the transform (unused in computation). |
required |
Returns:
| Type | Description |
|---|---|
|
Tensor of log absolute determinant values, broadcast to the input |
|
|
batch shape with the sum over the event dimensions. |
affine(scale_type='diag', batch_shape=None, loc_init=None, scale_init=None, scale_eps=1e-05, cache_size=0)
¶
Factory function to create a partial AffineModule constructor.
Returns a partial function with all initialization parameters pre-bound,
suitable for passing to PAVIGuide module. The returned partial accepts
only event_dim as its remaining argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale_type
|
Literal['diag', 'tril']
|
Type of scale parameterization — |
'diag'
|
batch_shape
|
Size | None
|
Optional batch shape for broadcasting. Only
supported when |
None
|
loc_init
|
float | Tensor | None
|
Initial value for the location (shift) parameter.
Can be a scalar, a |
None
|
scale_init
|
float | Tensor | None
|
Initial value for the scale (multiplicative) parameter.
Can be a scalar, a |
None
|
scale_eps
|
float
|
Small constant added to the scale to ensure numerical
stability. Defaults to |
1e-05
|
cache_size
|
int
|
Cache size for the underlying Pyro transform.
Defaults to |
0
|
Returns:
| Type | Description |
|---|---|
|
A |
|
|
arguments pre-bound except |
|
|
|
Example
from pavi.distributions.affine_modules import affine make_affine = affine(scale_type="diag", scale_eps=1e-5) transform = make_affine(event_dim=64)
conditional_affine(hidden_layers, activation_fn='LeakyReLU', scale_type='diag', scale_eps=1e-05, compile_module=False, cache_size=0)
¶
Factory function to create a partial ConditionalAffineModule constructor.
Returns a partial function with all neural network and transform
configuration parameters pre-bound, suitable for passing to
PAVIGuide. The returned partial accepts context_dim and event_dim
as its remaining arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_layers
|
list[int]
|
List of hidden layer sizes for the internal DenseNN networks used to predict location and scale from context. |
required |
activation_fn
|
str
|
Name of the PyTorch activation module to use
between hidden layers. Defaults to |
'LeakyReLU'
|
scale_type
|
Literal['diag', 'tril']
|
Scale parameterization — |
'diag'
|
scale_eps
|
float
|
Small constant added to the scale for numerical
stability. Defaults to |
1e-05
|
compile_module
|
bool
|
If |
False
|
cache_size
|
int
|
Cache size for the underlying Pyro transform.
Defaults to |
0
|
Returns:
| Type | Description |
|---|---|
|
A |
|
|
with all arguments pre-bound except |
|
|
which must be provided at instantiation time. |
Example
from pavi.distributions.affine_modules import conditional_affine make_cond_affine = conditional_affine( ... hidden_layers=[128, 64], ... scale_type="diag", ... ) transform = make_cond_affine(context_dim=32, event_dim=64)
pavi.distributions.householder_modules
¶
Custom transform modules for the pavi_pytorch probabilistic modeling framework.
This module provides a collection of Pyro-compatible Householder reflections (conditional and unconditional transform modules) used in normalizing flows and variational inference.
HouseholderModule
¶
Bases: Householder, PyroTransformModule
A Householder reflection transform for normalizing flows.
Wraps Pyro's Householder transform with PyroTransformModule semantics.
Householder reflections are orthogonal transformations that can be composed
to approximate arbitrary rotations, providing a flexible building block
for invertible neural networks.
Attributes:
| Name | Type | Description |
|---|---|---|
input_dim |
The dimensionality of the input space. |
|
count_transforms |
Number of Householder reflections to compose. |
ConditionalHouseholderModule
¶
Bases: ConditionalHouseholder, ConditionalTransform, PyroModule
A conditional Householder reflection transform for normalizing flows.
Wraps Pyro's ConditionalHouseholder with PyroTransformModule semantics,
enabling the reflection parameters to depend on an external context tensor.
This is useful in conditional normalizing flows where the transformation
adapts based on conditioning information.
The module constructs a DenseNN that outputs the Householder
reflection parameters, with the number of parameters per reflection
equal to event_dim.
Attributes:
| Name | Type | Description |
|---|---|---|
event_dim |
Dimensionality of the input space. |
|
cache_size |
Cache size for the underlying Pyro transform. |