virgil.svi
variational fits a guide (an approximate posterior) to the posterior of
numpyro_model with numpyro's SVI, taking the same
arguments as fit and starting from a fit and its
Gauss–Newton covariance. See Variational inference.
Variational inference with numpyro's SVI, started from a fit.
Provisional
May change in any 0.4.x release without a deprecation cycle.
variational takes the same arguments as
fit and
numpyro_model, and fits a guide (an
approximate posterior) to the posterior by maximizing the evidence lower
bound (ELBO) with numpyro's SVI. The guide works in the unconstrained
coordinates NUTS samples, and starts at the Laplace approximation: its
centre at a FitResult and its width from
gauss_newton_mass. The default guide
is a normalizing flow that bends that Gaussian to follow curved ridges and
skewed scales; a Gaussian guide is close to the Laplace approximation, for
which see laplace_cov. Every guide covers
one mode.
See Variational inference for the choice of guide and how to check the result. References: ADVI (Kucukelbir et al. 2017, https://arxiv.org/abs/1603.00788), the block neural autoregressive flow (De Cao, Titov & Aziz 2019, https://arxiv.org/abs/1904.04676), the inverse autoregressive flow (Kingma et al. 2016, https://arxiv.org/abs/1606.04934) and Pareto-smoothed importance sampling (Vehtari et al. 2024, https://arxiv.org/abs/1507.02646).
VariationalResult
dataclass
The result of variational.
Attributes:
| Name | Type | Description |
|---|---|---|
samples |
dict
|
Draws from the guide, |
losses |
ndarray
|
The loss (the negative ELBO, a Monte Carlo estimate) at each step. |
guide |
AutoGuide
|
The fitted guide; |
params |
dict
|
The guide's fitted parameters. |
converged |
bool
|
Whether the loss stopped falling: the mean loss over the last |
info |
dict
|
|
samples
instance-attribute
losses
instance-attribute
guide
instance-attribute
params
instance-attribute
converged
instance-attribute
info
instance-attribute
__init__(samples, losses, guide, params, converged, info)
variational(model, priors, data, regularizers=(), *, noise=None, likelihoods=(), start=None, guide='bnaf', steps=3000, optimizer=None, learning_rate=0.003, num_particles=8, num_samples=2000, key=None, flat_coordinates=False, dense_start=True, init_scale=0.1, window=None, psis=True, dtype='float64', **options)
Approximate the posterior with a guide fitted by numpyro's SVI.
Maximizes the ELBO with Adam on the posterior of
numpyro_model. The guide starts at
start with the Gauss–Newton width, or width init_scale given
regularizers, dense_start=False or start=None (then at the
priors' medians). Guide choice and checks:
Variational inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
As for |
required | |
priors
|
As for |
required | |
data
|
As for |
required | |
regularizers
|
As for |
required | |
noise
|
As for |
required | |
likelihoods
|
As for |
required | |
start
|
FitResult or dict
|
Where the guide starts: a fit or its |
None
|
guide
|
str or callable
|
|
'bnaf'
|
steps
|
int
|
Steps (3000), draws per ELBO estimate (8), draws returned (2000). |
3000
|
num_particles
|
int
|
Steps (3000), draws per ELBO estimate (8), draws returned (2000). |
3000
|
num_samples
|
int
|
Steps (3000), draws per ELBO estimate (8), draws returned (2000). |
3000
|
optimizer
|
optional
|
Optimizer (default |
None
|
learning_rate
|
optional
|
Optimizer (default |
None
|
key
|
optional
|
Optimizer (default |
None
|
flat_coordinates
|
optional
|
Optimizer (default |
None
|
dtype
|
optional
|
Optimizer (default |
None
|
dense_start
|
optional
|
Start the width at the Gauss–Newton covariance (default |
True
|
init_scale
|
optional
|
Start the width at the Gauss–Newton covariance (default |
True
|
window
|
optional
|
Convergence-test window (default |
None
|
psis
|
optional
|
Convergence-test window (default |
None
|
**options
|
Fixed error terms and |
{}
|
Returns:
| Type | Description |
|---|---|
VariationalResult
|
|
Examples:
>>> vi = variational(scene, priors, data, start=result)