ADVI in JAX
Design considerations
- ADVI class is an object but
ADVI.objective_funis a pure function that can be optimized withoptaxorjaxoptor any other jax supported optimizers. - variational distribution parameters can be initialized with
ADVI.initusing adistraxortfpdistribution as an initializer (or any jax distribution that implements.sample()method in a similar way). - Users can pass the suitable bijectors of class
distrax.Bijectorto the variational distribution. - Transformation is directly applied to posterior and thus prior and likelihood stay untouched during the entire process. This way, after the training, the variational distribution is ready for sampling without any additional transformations. Also, this gives freedom to variational distribution to be constructed in more complex way as it is separated from the other parts of the model (see the example below).
- If we do not change the
keyduring the training, the method is called the deterministic ADVI. - Users can implement their own
likelihood_log_prob_funbecause likelihood does not necessarily have to be a distribution.
A Coin Toss Example
import jax
import jax.numpy as jnp
from advi_jax import ADVI
from advi_jax.variational_distributions import MeanField
from advi_jax.init import initialize
import tensorflow_probability.substrates.jax as tfp
dist = tfp.distributions
# Data
tosses = jnp.array([0, 1, 0, 0, 1, 0])
# Prior and likelihood
prior_dist = dist.Beta(2.0, 3.0)
likelihood_log_prob_fun = lambda theta: dist.Bernoulli(probs=theta).log_prob(tosses).sum()
# ADVI model
model = ADVI(prior_dist, likelihood_log_prob_fun, tosses)
# Variational distribution and bijector
bijector = distrax.Sigmoid()
variational_dist = MeanField(u_mean = jnp.array(0.0), u_scale = jnp.array(0.0), bijector = bijector)
# Initialize the parameters of variational distribution
key = jax.random.PRNGKey(0)
variational_dist = initialize(key, variational_dist, initializer=dist.Normal(0.0, 1.0))
# Define the value and grad function
value_and_grad_fun = jax.jit(jax.value_and_grad(model.objective_fun, argnums=1), static_argnums=2)
# Do gradient descent!
learning_rate = 0.01
for i in range(100):
key = jax.random.PRNGKey(i) # If this is constant, this becomes deterministic ADVI
loss_value, grads = value_and_grad_fun(key, variational_dist, n_samples=10)
variational_dist = variational_dist - learning_rate * grad
# Get the posterior samples
key = jax.random.PRNGKey(2)
posterior_samples = variational_dist.sample(seed=key, sample_shape=(100,))
Metadata
Release files for advi-jax 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| advi_jax-0.0.1.tar.gz | 1.2 MB | Without provenance | Details for advi_jax-0.0.1.tar.gz |
Release files / advi_jax-0.0.1.tar.gz
| Download URL | advi_jax-0.0.1.tar.gz |
|---|---|
| Size | 1.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
006a616b270bd0a435e406a24627f02af69a98dc14303ab7582fbb2ce606f5ce
|
|
BLAKE2b-256 checksum How to use checksums |
7b2704fe3a570e2b8315435135282a19111007fc8ce36d414a2f6d40024390d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.4
|
Security for advi-jax 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.