normflows

normflows: A PyTorch Package for Normalizing Flows - Published in JOSS (2023)

https://github.com/vincentstimper/normalizing-flows

Science Score: 100.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 13 DOI reference(s) in README and JOSS metadata
  • Academic publication links
    Links to: arxiv.org, joss.theoj.org
  • Committers with academic emails
    4 of 14 committers (28.6%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
    Published in Journal of Open Source Software

Keywords

density-estimation glow invertible-neural-networks neural-spline-flow normalizing-flow pytorch real-nvp residual-flow variational-autoencoder variational-inference

Scientific Fields

Physics Physical Sciences - 40% confidence
Engineering Computer Science - 40% confidence
Last synced: 4 months ago · JSON representation ·

Repository

PyTorch implementation of normalizing flow models

Basic Info
Statistics
  • Stars: 873
  • Watchers: 13
  • Forks: 129
  • Open Issues: 12
  • Releases: 12
Topics
density-estimation glow invertible-neural-networks neural-spline-flow normalizing-flow pytorch real-nvp residual-flow variational-autoencoder variational-inference
Created almost 6 years ago · Last pushed over 1 year ago
Metadata Files
Readme Funding License Citation

README.md

normflows: A PyTorch Package for Normalizing Flows

documentation unit-tests code coverage License: MIT DOI PyPI Downloads

normflows is a PyTorch implementation of discrete normalizing flows. Many popular flow architectures are implemented, see the list below. The package can be easily installed via pip. The basic usage is described here, and a full documentation is available as well. A more detailed description of this package is given in our accompanying paper.

Several sample use cases are provided in the examples folder, including Glow, a VAE, and a Residual Flow. Moreover, two simple applications are highlighed in the examples section. You can run them yourself in Google Colab using the links below to get a feeling for normflows.

| Link | Description | |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------| | Open In Colab | Real NVP applied to a 2D bimodal target distribution | | Open In Colab | Modeling a distribution on a cylinder surface with a neural spline flow | | Open In Colab | Modeling and generating CIFAR-10 images with Glow |

Implemented Flows

| Architecture | Reference | |--------------|---------------------------------------------------------------------------------------------------------------------------| | Planar Flow | Rezende & Mohamed, 2015 | | Radial Flow | Rezende & Mohamed, 2015 | | NICE | Dinh et al., 2014 | | Real NVP | Dinh et al., 2017 | | Glow | Kingma et al., 2018 | | Masked Autoregressive Flow | Papamakarios et al., 2017 | | Neural Spline Flow | Durkan et al., 2019 | | Circular Neural Spline Flow | Rezende et al., 2020 | | Residual Flow | Chen et al., 2019 | | Stochastic Normalizing Flow | Wu et al., 2020 |

Note that Neural Spline Flows with circular and non-circular coordinates are supported as well.

Installation

The latest version of the package can be installed via pip

pip install normflows

At least Python 3.7 is required. If you want to use a GPU, make sure that PyTorch is set up correctly by following the instructions at the PyTorch website.

To run the example notebooks clone the repository first

git clone https://github.com/VincentStimper/normalizing-flows.git

and then install the dependencies.

pip install -r requirements_examples.txt

Usage

A normalizing flow consists of a base distribution, defined in nf.distributions.base, and a list of flows, given in nf.flows. Let's assume our target is a 2D distribution. We pick a diagonal Gaussian base distribution, which is the most popular choice. Our flow shall be a Real NVP model and, therefore, we need to define a neural network for computing the parameters of the affine coupling map. One dimension is used to compute the scale and shift parameter for the other dimension. After each coupling layer we swap their roles.

```python import normflows as nf

Define 2D Gaussian base distribution

base = nf.distributions.base.DiagGaussian(2)

Define list of flows

numlayers = 32 flows = [] for i in range(numlayers): # Neural network with two hidden layers having 64 units each # Last layer is initialized by zeros making training more stable parammap = nf.nets.MLP([1, 64, 64, 2], initzeros=True) # Add flow layer flows.append(nf.flows.AffineCouplingBlock(param_map)) # Swap dimensions flows.append(nf.flows.Permute(2, mode='swap')) ```

Once they are set up, we can define a nf.NormalizingFlow model. If the target density is available, it can be added to the model to be used during training. Sample target distributions are given in nf.distributions.target.

```python

If the target density is not given

model = nf.NormalizingFlow(base, flows)

If the target density is given

target = nf.distributions.target.TwoMoons() model = nf.NormalizingFlow(base, flows, target) ```

The loss can be computed with the methods of the model and minimized.

```python

When doing maximum likelihood learning, i.e. minimizing the forward KLD

with no target distribution given

loss = model.forward_kld(x)

When minimizing the reverse KLD based on the given target distribution

loss = model.reversekld(numsamples=512)

Optimization as usual

loss.backward() optimizer.step() ```

Examples

We provide several illustrative examples of how to use the package in the examples directory. Among them are implementations of Glow, a VAE, and a Residual Flow. More advanced experiments can be done with the scripts listed in the repository about resampled base distributions, see its experiments folder.

Below, we consider two simple 2D examples.

Real NVP applied to a 2D bimodal target distribution

Open In Colab

In this notebook, which can directly be opened in Colab, we consider a 2D distribution with two half-moon-shaped modes as a target. We approximate it with a Real NVP model and obtain the following results.

2D target distribution and Real NVP model

Note that there might be a density filament connecting the two modes, which is due to an architectural limitation of normalizing flows, especially prominent in Real NVP. You can find out more about it in this paper.

Modeling a distribution on a cylinder surface with a neural spline flow

Open In Colab

In another example, which is available in Colab as well, we apply a Neural Spline Flow model to a distribution defined on a cylinder. The resulting density is visualized below.

Neural Spline Flow applied to target distribution on a cylinder

This example is considered in the paper accompanying this repository.

Support

If you have problems, please read the package documentation and check out the examples section above. You are also welcome to create issues on GitHub to get help. Note that it is worthwhile browsing the existing open and closed issues, which might address the problem you are facing.

Contributing

If you find a bug or have a feature request, please file an issue on GitHub.

You are welcome to contribute to the package by fixing the bug or adding the feature yourself. If you want to contribute, please add tests for the code you added or modified and ensure it passes successfully by running pytest. This can be done by simply executing pytest within your local version of the repository. Make sure you code is well documented, and we also encourage contributions to the existing documentation. Once you finished coding and testing, please create a pull request on GitHub.

Used by

The package has been used in several research papers. Some of them are listed below.

Andrew Campbell, Wenlong Chen, Vincent Stimper, José Miguel Hernández-Lobato, and Yichuan Zhang. A gradient based strategy for Hamiltonian Monte Carlo hyperparameter optimization. In Proceedings of the 38th International Conference on Machine Learning, pp. 1238–1248. PMLR, 2021.

Code available on GitHub.

Vincent Stimper, Bernhard Schölkopf, and José Miguel Hernández-Lobato. Resampling Base Distributions of Normalizing Flows. In Proceedings of The 25th International Conference on Artificial Intelligence and Statistics, volume 151, pp. 4915–4936, 2022.

Code available on GitHub.

Laurence I. Midgley, Vincent Stimper, Gregor N. C. Simm, Bernhard Schölkopf, and José Miguel Hernández-Lobato. Flow Annealed Importance Sampling Bootstrap. The Eleventh International Conference on Learning Representations, 2023.

Code available on GitHub.

Arnau Quera-Bofarull, Joel Dyer, Anisoara Calinescu, J. Doyne Farmer, and Michael Wooldridge. BlackBIRDS: Black-Box Inference foR Differentiable Simulators. Journal of Open Source Software, 8(89), 5776, 2023.

Code available on GitHub.

Utkarsh Singhal, Carlos Esteves, Ameesh Makadia, and Stella X. Yu. Learning to Transform for Generalizable Instance-wise Invariance. Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV), pp. 6211-6221, 2023.

Code available on GitHub.

Ba-Hien Tran, Giulio Franzese, Pietro Michiardi, and Maurizio Filippone. One-Line-of-Code Data Mollification Improves Optimization of Likelihood-based Generative Models. Advances in Neural Information Processing Systems 36, pp. 6545–6567, 2023.

Code available on GitHub.

Moreover, the boltzgen package has been build upon normflows.

Citation

If you use normflows, please cite the corresponding paper as follows.

Stimper et al., (2023). normflows: A PyTorch Package for Normalizing Flows. Journal of Open Source Software, 8(86), 5361, https://doi.org/10.21105/joss.05361

Bibtex

@article{Stimper2023, author = {Vincent Stimper and David Liu and Andrew Campbell and Vincent Berenz and Lukas Ryll and Bernhard Schölkopf and José Miguel Hernández-Lobato}, title = {normflows: A PyTorch Package for Normalizing Flows}, journal = {Journal of Open Source Software}, volume = {8}, number = {86}, pages = {5361}, publisher = {The Open Journal}, doi = {10.21105/joss.05361}, url = {https://doi.org/10.21105/joss.05361}, year = {2023} }

Owner

  • Name: Vincent Stimper
  • Login: VincentStimper
  • Kind: user
  • Location: Lausanne, Switzerland
  • Company: Isomorphic Labs

Research Scientist @ Isomorphic Labs

JOSS Publication

normflows: A PyTorch Package for Normalizing Flows
Published
June 24, 2023
Volume 8, Issue 86, Page 5361
Authors
Vincent Stimper ORCID
University of Cambridge, Cambridge, United Kingdom, Max Planck Institute for Intelligent Systems, Tübingen, Germany
David Liu
University of Cambridge, Cambridge, United Kingdom
Andrew Campbell
University of Cambridge, Cambridge, United Kingdom
Vincent Berenz
Max Planck Institute for Intelligent Systems, Tübingen, Germany
Lukas Ryll
University of Cambridge, Cambridge, United Kingdom
Bernhard Schölkopf ORCID
Max Planck Institute for Intelligent Systems, Tübingen, Germany
José Miguel Hernández-Lobato
University of Cambridge, Cambridge, United Kingdom
Editor
Marcel Stimberg ORCID
Tags
PyTorch Machine Learning Normalizing Flows Density Estimation

Citation (CITATION.cff)

cff-version: "1.2.0"
authors:
- family-names: Stimper
  given-names: Vincent
  orcid: "https://orcid.org/0000-0002-4965-4297"
- family-names: Liu
  given-names: David
- family-names: Campbell
  given-names: Andrew
- family-names: Berenz
  given-names: Vincent
- family-names: Ryll
  given-names: Lukas
- family-names: Schölkopf
  given-names: Bernhard
  orcid: "https://orcid.org/0000-0002-8177-0925"
- family-names: Hernández-Lobato
  given-names: José Miguel
doi: 10.5281/zenodo.8027667
message: If you use this software, please cite our article in the
  Journal of Open Source Software.
preferred-citation:
  authors:
  - family-names: Stimper
    given-names: Vincent
    orcid: "https://orcid.org/0000-0002-4965-4297"
  - family-names: Liu
    given-names: David
  - family-names: Campbell
    given-names: Andrew
  - family-names: Berenz
    given-names: Vincent
  - family-names: Ryll
    given-names: Lukas
  - family-names: Schölkopf
    given-names: Bernhard
    orcid: "https://orcid.org/0000-0002-8177-0925"
  - family-names: Hernández-Lobato
    given-names: José Miguel
  date-published: 2023-06-24
  doi: 10.21105/joss.05361
  issn: 2475-9066
  issue: 86
  journal: Journal of Open Source Software
  publisher:
    name: Open Journals
  start: 5361
  title: "normflows: A PyTorch Package for Normalizing Flows"
  type: article
  url: "https://joss.theoj.org/papers/10.21105/joss.05361"
  volume: 8
title: "normflows: A PyTorch Package for Normalizing Flows"

GitHub Events

Total
  • Issues event: 3
  • Watch event: 160
  • Issue comment event: 2
  • Pull request event: 2
  • Fork event: 23
Last Year
  • Issues event: 3
  • Watch event: 160
  • Issue comment event: 2
  • Pull request event: 2
  • Fork event: 23

Committers

Last synced: 5 months ago

All Time
  • Total Commits: 712
  • Total Committers: 14
  • Avg Commits per committer: 50.857
  • Development Distribution Score (DDS): 0.162
Past Year
  • Commits: 2
  • Committers: 2
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.5
Top Committers
Name Email Commits
Vincent Stimper v****r@g****m 597
Lukas Ryll 4****l 40
VincentStimper v****8@c****k 27
davindicode D****9@g****m 20
Vincent Berenz v****z 8
L Ryll l****7@w****k 7
Andrew Campbell b****k 5
mattcleigh 3****h 2
Timothy Gebhard t****d 1
Kaze Wong k****s@g****m 1
Adam Li a****2@g****m 1
laurence l****y@i****m 1
Vincent Berenz v****z@t****e 1
Vincent Berenz v****z@t****e 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 45
  • Total pull requests: 27
  • Average time to close issues: about 2 months
  • Average time to close pull requests: 5 days
  • Total issue authors: 37
  • Total pull request authors: 14
  • Average comments per issue: 1.84
  • Average comments per pull request: 0.89
  • Merged pull requests: 20
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 4
  • Pull requests: 2
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 4
  • Pull request authors: 2
  • Average comments per issue: 0.25
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • maulberto3 (4)
  • kazewong (3)
  • guzy0324 (2)
  • matejgrcic (2)
  • timothygebhard (1)
  • prclibo (1)
  • ybernaerts (1)
  • 12jerek34jeremi (1)
  • mjack3 (1)
  • yarinbar (1)
  • ZhengtingJiang (1)
  • alexmiltenberger (1)
  • dgjung0220 (1)
  • adam2392 (1)
  • odunkel (1)
Pull Request Authors
  • VincentStimper (8)
  • vincentberenz (5)
  • prclibo (2)
  • adam2392 (2)
  • jonas-eschle (2)
  • mstoelzle (2)
  • timothygebhard (1)
  • lollcat (1)
  • mbaddar1 (1)
  • mattcleigh (1)
  • Donglin-Wang2 (1)
  • kazewong (1)
  • arc82 (1)
  • rudolfwilliam (1)
Top Labels
Issue Labels
question (16) enhancement (6) good first issue (5) bug (4) documentation (3) duplicate (1) wontfix (1) paper (1)
Pull Request Labels
bug (2) enhancement (2) documentation (2)

Packages

  • Total packages: 3
  • Total downloads:
    • pypi 2,923 last-month
  • Total dependent packages: 1
    (may contain duplicates)
  • Total dependent repositories: 2
    (may contain duplicates)
  • Total versions: 22
  • Total maintainers: 1
proxy.golang.org: github.com/VincentStimper/normalizing-flows
  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.4%
Average: 5.6%
Dependent repos count: 5.7%
Last synced: 4 months ago
proxy.golang.org: github.com/vincentstimper/normalizing-flows
  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.4%
Average: 5.6%
Dependent repos count: 5.7%
Last synced: 4 months ago
pypi.org: normflows

Pytorch implementation of normalizing flows

  • Versions: 10
  • Dependent Packages: 1
  • Dependent Repositories: 2
  • Downloads: 2,923 Last month
Rankings
Stargazers count: 2.8%
Downloads: 4.6%
Dependent packages count: 4.8%
Forks count: 4.9%
Average: 5.7%
Dependent repos count: 11.5%
Maintainers (1)
Last synced: 4 months ago

Dependencies

requirements.txt pypi
  • numpy *
  • torch *
requirements_examples.txt pypi
  • ipywidgets *
  • jupyterlab *
  • matplotlib *
  • sklearn *
  • tqdm *
setup.py pypi
  • x.strip *
.github/workflows/mkdocs.yaml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
.github/workflows/pytest.yaml actions
  • JamesIves/github-pages-deploy-action v4 composite
  • actions/checkout v3 composite
  • actions/setup-python v3 composite
  • tj-actions/coverage-badge-py v1.8 composite