Normalizing flows in PyTorch
Low maintenance activity. Last release 9 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install zukoZuko - Normalizing flows in PyTorch
Zuko is a Python package that implements normalizing flows in PyTorch. It relies as much as possible on distributions and transformations already provided by PyTorch. Unfortunately, the Distribution and Transform classes of torch are not sub-classes of torch.nn.Module, which means you cannot send their internal tensors to GPU with .to('cuda') or retrieve their parameters with .parameters(). Worse, the concepts of conditional distribution and transformation, which are essential for probabilistic inference, are impossible to express.
To solve these problems, zuko defines two concepts: the LazyDistribution and LazyTransform, which are any modules whose forward pass returns a Distribution or Transform, respectively. Because the creation of the actual distribution/transformation is delayed, an eventual condition can be easily taken into account. This design enables lazy distributions, including normalizing flows, to act like distributions while retaining features inherent to modules, such as trainable parameters. It also makes the implementations easy to understand and extend.
Acknowledgements
Zuko takes significant inspiration from nflows and Stefan Webb's work in Pyro and FlowTorch.
Installation
The zuko package is available on PyPI, which means it is installable via pip.
pip install zuko
Alternatively, if you need the latest features, you can install it from the repository.
pip install git+https://github.com/probabilists/zuko
Getting started
Normalizing flows are provided in the module. To build one, supply the number of sample and context features as well as the transformations' hyperparameters. Then, feeding a context $c$ to the flow returns a conditional distribution $p(x | c)$ which can be evaluated and sampled from.