Skip to main content

Normalizing flows in PyTorch. An extension of nflows.

Project description

FlowConductor: (Conditional) Normalizing Flows and
bijective Layers for Pytorch

Build status Documentation

Documentation

About

FlowConductor provides a collection of normalizing flows in PyTorch. It's core logic and transformations were originally based on the nflows package. The main focus lies in implementing more flow layers from the literature in one consistent framework, and adding support for conditional normalizing flows. In the original nflows package, conditional networks were restricted to using a conditional base distribution. In FlowConductor, nearly every layer can be conditional :). In particular, we support conditional transformations based on hypernetworks.

While using the package, we extensively expanded it, implementing layers such as invertible residual networks, and focusing on conditional normalizing flows. In addition, we improved the testing, so that the Jacobian determinants are actually reliably compared to a reference based on brute-force autograd.

The bijective layers we additionally provide includes but are not limited to Planar Flows, (conditional) invertible ResNets/DenseNets, a variant of neural autoregressive flows, and a basic support of continuous normalizing flows (and FFJORD) based on the torchdiffeq package.

Install

PIP

FlowConductor is installable via pip. We recommend using a virtual environment, where you set up your pytorch version beforehand. You can check out in ./docker which pytorch versions we test for, but in general there shouldn't be any complications for any version after 1.13.

You may either install the latest release from pipy:

$  pip install flowcon

or install it directly from github via pip

$  pip install git+https://github.com/FabricioArendTorres/FlowConductor.git

Of course, you may also just download the repo and install it locally

$ git clone https://github.com/FabricioArendTorres/FlowConductor
$ cd FlowConductor
$ pip install . 

Docker

We provide some basic Dockerfiles in ./docker, which are very simple extensions of the pytorch docker images. The dockerfiles we list are the ones used for testing, so you can be sure they work. If you are unfamiliar with Docker, you can use our package with it as follows (assuming it is at least installed).

This also works on Windows (cpu at least)!

$ git clone https://github.com/FabricioArendTorres/FlowConductor
$ cd FlowConductor

# Build the docker image, see the ./docker dir for different versions.
$ docker build -f ./docker/Dockerfile-pytorchlatest -t flowc-pytorchlatest .

# you can run the tests with
docker run flowc-pytorchlatest pytest /flowc

For working with this container, you may either choose to adapt our Dockerfiles, or simply bind the current directory when starting the container interactively. For the latter, you can run a script (here examples/toy_2d.py) with

$ docker run --rm -it -v .:/app flowc-pytorchlatest python examples/toy_2d.py Or you may swap an interactive shell within the container with

$ docker run --rm -it -v .:/app flowc-pytorchlatest
$ python examples/toy_2d.py

Package Usage

As the core is based on nflows, its usage is similar. To define a flow:

from flowcon import transforms, distributions, flows

# Define an invertible transformation.
transform = transforms.CompositeTransform([
  transforms.MaskedAffineAutoregressiveTransform(features=2, hidden_features=4),
  transforms.RandomPermutation(features=2)
])

# Define a base distribution.
base_distribution = distributions.StandardNormal(shape=[2])

# Combine into a flow.
flow = flows.Flow(transform=transform, distribution=base_distribution)

To evaluate log probabilities of inputs:

log_prob = flow.log_prob(inputs)

To sample from the flow:

samples = flow.sample(num_samples)

Additional examples of the workflow are provided in examples folder.

Changes and added features compared to nflows

The core logic of the code for LFlows (i.e. the nflows/ directory) is based on the nflows package. Aside from added features, our extension provides tests for the calculation of the Jacobian log-determinant, which is at the heart of Normalizing Flow.

Added Layers / Flwos:

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

flowcon-0.0.4.tar.gz (114.7 kB view details)

Uploaded Source

Built Distribution

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

flowcon-0.0.4-py3-none-any.whl (127.3 kB view details)

Uploaded Python 3

File details

Details for the file flowcon-0.0.4.tar.gz.

File metadata

  • Download URL: flowcon-0.0.4.tar.gz
  • Upload date:
  • Size: 114.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for flowcon-0.0.4.tar.gz
Algorithm Hash digest
SHA256 20683e661a4d25c2d8cdf70fbfdf69faa76edf8ac732671406dd7e552aab7efe
MD5 5168217f3d68d81d26091924bade2bed
BLAKE2b-256 fea0d21199eee400e1ea75979488aa3eb517bb5d4beda72b2a192af1998b3a28

See more details on using hashes here.

File details

Details for the file flowcon-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: flowcon-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 127.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for flowcon-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 894243b50ca9db0dcad6be01d6101b95ea77d4502aecc57b8f017826a847a46f
MD5 1ad1524b7e65095d8f9be0db684ab0d4
BLAKE2b-256 b0efd7f005b55e3215cf0240f7fff4894d02860f1517adf56e41e60b5842dde8

See more details on using hashes here.

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