Skip to main content

GSM-VI

Code for Variational Inference (VI) with Gaussian Score Matching (GSM).

This repo implements code for paper https://arxiv.org/abs/2307.07849.

GSM-VI fits a multivariate Gasussian distribution with dense covaraince matrix to the target distribution by score matching. It only requires access to the score function i.e. the gradient of the target log-probability distribution and implements analytic updates for the variational parameters (mean and covariance matrix).

Installation:

The code is available on PyPI

pip install gsmvi

Usage

The simplest version of the algorithm is written in numpy. The following is the minimal code to use GSM to fit the parameters x of a model given its log_prob and log_prob_grad functions. See example/example_gsm_numpy.py for a full example.

dimensions = D
def log_prob(x):
  # return log_prbability at sample x
  ...

def log_prob_grad(x):
  # return the score fuction i.e. the gradient of log_prbability at sample x
  ...

from gsmvi.gsm_numpy import GSM
gsm = GSM(D=D, lp=log_prob, lp_g=log_prob_grad)
random_seed = 99
number_of_iterations = 500
mean_fit, cov_fit = gsm.fit(key=random_seed, niter=number_of_iterations)

A more efficient version of the algorithm is implemented in Jax where it can benefit from jit compilation. The basic signature stays the same. See example/example_gsm.py for a full example.

dimensions = D
model =  setup_model(D=D)     # Ths example sets up a numpyro model which has log_prob attribute implemented
lp = jit(lambda x: jnp.sum(model.log_prob(x)))
lp_g = jit(grad(lp, argnums=0))

from gsmvi.gsm import GSM
gsm = GSM(D=D, lp=lp, lp_g=lp_g)
mean_fit, cov_fit = gsm.fit(key=random.PRNGKey(99), niter=500)

Other utilities:

  • For comaprison, we also provide implementation of ADVI algorithm (https://arxiv.org/abs/1603.00788), another common approach to fit a multivariate Gaussian variational distribution which maximizes ELBO.
  • We provide LBFGS initilization for the variational distribution which can be used with GSM and ADVI.
  • We also provide a Monitor class to monitor the KL divergence over iterations as the algorithms progress.

Code Dependencies

The vanilla code is written in python3 and does not have any dependencies.

Optional dependencies

These will not be installed with the package and should be installed by user depending on the use-case.

The Jax version of the code requires jax and jaxlib.
The target distributions in example files other than example_gsm_numpy.py are implemented in numpyro.
ADVI algorithm uses optax for maximizing ELBO.
LBFGS initialization for initializing variational distributions uses scipy.

Starting point

We provide simple examples in examples/ folder to fit a target multivariate Gaussian distribution with GSM and ADVI.

cd examples
python3 example_gsm_numpy.py  # vanilla example in numpy, no dependencies
python3 example_gsm.py        # jax version, requires jax and numpyro
python3 example_advi.py       # jax version, requires jax, numpyro and optax

An example on how to use the Monitor class and LBFGS initialization is in examples/example_initializers.py

cd examples
python3 example_initializers.py   # jax version, requires jax, numpyro, optax and scipy

Metadata

Release files for gsmvi 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gsmvi 0.1
File Size Uploaded Provenance
gsmvi-0.1.tar.gz 8.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for gsmvi 0.1
File Interpreter ABI Platform
gsmvi-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.1 kB

Release files / gsmvi-0.1.tar.gz

Download URL gsmvi-0.1.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
387b71903c267e195b83a13505dea572a77d667760e7cbc3a9bc7a820e439bcb
BLAKE2b-256 checksum
How to use checksums
8cdb62111a1dadbc442fff11b7fa9ed5f182f7082bf6723cd23c6bbdd8b29e9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Release files / gsmvi-0.1-py3-none-any.whl

Download URL gsmvi-0.1-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e702ecfd3cb97ef52dc8db7218cf3ddb010db5343b06f10c7a652110e89b32a
BLAKE2b-256 checksum
How to use checksums
53bf7d17c88da83b771b8107d888fa240152394527ebe35d8a2a55c3e83e4f4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Security for gsmvi 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.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page