Skip to main content

Easy to use distributions, bijections and normalizing flows in JAX.

Project description

FlowJAX

Distributions, bijections and normalizing flows using Equinox and JAX

  • Includes a wide range of distributions and bijections.
  • Distributions and bijections are PyTrees, registered through Equinox modules, making them compatible with JAX transformations.
  • Includes many state of the art normalizing flow models.
  • First class support for conditional distributions and density estimation.

Documentation

Available here.

Short example

As an example we will create and train a normalizing flow model to toy data in just 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
import jax.random as jr
import jax.numpy as jnp

data_key, flow_key, train_key, sample_key = jr.split(jr.key(0), 4)

x = jr.uniform(data_key, (5000, 2))  # Toy data

flow = block_neural_autoregressive_flow(
    key=flow_key,
    base_dist=Normal(jnp.zeros(x.shape[1])),
)

flow, losses = fit_to_data(
    key=train_key,
    dist=flow,
    data=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:

  • Many simple bijections and distributions, implemented as Equinox modules.
  • coupling_flow (Dinh et al., 2017) and masked_autoregressive_flow (Kingma et al., 2016, Papamakarios et al., 2017) normalizing flow architectures.
    • These can be used with arbitrary bijections as transformers, such as Affine or RationalQuadraticSpline (the latter used in neural spline flows; Durkan et al., 2019).
  • block_neural_autoregressive_flow, as introduced by De Cao et al., 2019.
  • planar_flow, as introduced by Rezende and Mohamed, 2015.
  • triangular_spline_flow, introduced here.
  • Training scripts for fitting by maximum likelihood, variational inference, or using contrastive learning for sequential neural posterior estimation (Greenberg et al., 2019; Durkan et al., 2020).
  • A bisection search algorithm that allows inverting some bijections without a known inverse, allowing for example both sampling and density evaluation to be performed with block neural autoregressive flows.

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. Any breaking changes will be in the release notes for each major release.

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.
  • For applying parameterizations, we use paramax.

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},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flowjax-19.1.0.tar.gz (477.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flowjax-19.1.0-py3-none-any.whl (61.4 kB view details)

Uploaded Python 3

File details

Details for the file flowjax-19.1.0.tar.gz.

File metadata

  • Download URL: flowjax-19.1.0.tar.gz
  • Upload date:
  • Size: 477.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flowjax-19.1.0.tar.gz
Algorithm Hash digest
SHA256 0c22ace574ec9e22eac9a1abc2ab84264d5d7634a6b648a522575468cee89bcf
MD5 dcc55a1356cff9f077169588585ec625
BLAKE2b-256 4c37375042415ab43e26cea0cc3138f29767d09cb2506ed1cbb7d17f118f7112

See more details on using hashes here.

Provenance

The following attestation bundles were made for flowjax-19.1.0.tar.gz:

Publisher: publish.yml on danielward27/flowjax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flowjax-19.1.0-py3-none-any.whl.

File metadata

  • Download URL: flowjax-19.1.0-py3-none-any.whl
  • Upload date:
  • Size: 61.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flowjax-19.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ec9e08eb8e4956ae99d3261228df16096cb705dedec553108a0c22b35b5372d
MD5 e374a48752a3e7e22a6596cfcbe0df05
BLAKE2b-256 2719112666688ab1b389e2b5b463848a8b1a34846e2e2a15e0284fa17c7c5a8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for flowjax-19.1.0-py3-none-any.whl:

Publisher: publish.yml on danielward27/flowjax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page