danielward27 / flowjax

https://danielward27.github.io/flowjax/
MIT License
82 stars 10 forks source link
jax normalising-flows normalizing-flows
logo

FlowJax: Distributions and Normalizing Flows in Jax

Documentation

Available here.

Short example

Training a flow can be done in a few lines of code:

from flowjax.flows import block_neural_autoregressive_flow
from flowjax.train import fit_to_data
from flowjax.distributions import Normal
from jax import random
import jax.numpy as jnp

data_key, flow_key, train_key, sample_key = random.split(random.PRNGKey(0), 4)

x = random.uniform(data_key, (5000, 2))  # Toy data
base_dist = Normal(jnp.zeros(x.shape[1]))
flow = block_neural_autoregressive_flow(flow_key, base_dist=base_dist)
flow, losses = fit_to_data(
    key=train_key,
    dist=flow,
    x=x,
    learning_rate=5e-3,
    max_epochs=200,
    )

# We can now evaluate the log-probability of arbitrary points
log_probs = flow.log_prob(x)

# And sample the distribution
samples = flow.sample(sample_key, (1000, ))

The package currently includes:

Installation

pip install flowjax

Warning

This package is in its early stages of development and may undergo significant changes, including breaking changes, between major releases. Whilst ideally we should be on version 0.y.z to indicate its state, we have already progressed beyond that stage.

Development

We can install a version for development as follows

git clone https://github.com/danielward27/flowjax.git
cd flowjax
pip install -e .[dev]
sudo apt-get install pandoc  # Required for building documentation

Related

We make use of the Equinox package, which facilitates defining models using a PyTorch-like syntax with Jax.

Citation

If you found this package useful in academic work, please consider citing it using the template below, filling in [version number] and [release year of version] to the appropriate values. Version specific DOIs can be obtained from zenodo if desired.

@software{ward2023flowjax,
  title = {FlowJax: Distributions and Normalizing Flows in Jax},
  author = {Daniel Ward},
  url = {https://github.com/danielward27/flowjax},
  version = {[version number]},
  year = {[release year of version]},
  doi = {10.5281/zenodo.10402073},
}