Git Product home page Git Product logo

normalizing-flows's Introduction

Normalizing Flows Models

Last update: December 2022.


Lightweight normalizing flows for generative modeling in PyTorch.

Setup

$$\begin{align*} \mathbf{x} & = f_\theta^{-1}(\mathbf{z}) & \mathbf{z} & = f_\theta(\mathbf{x}), \end{align*}$$

where $f:\mathbb{R}^d \mapsto \mathbb{R}^d$ is an invertible function. The Change of Variables formula tells us that

$$\begin{align*} \underbrace{p(\mathbf{x})}_{\text{over }\mathbf{x}} &= \underbrace{p\left(f_\theta(\mathbf{x})\right)}_{\text{over }\mathbf{z}} \left|\mathrm{det}\left(\frac{\partial f_\theta \mathbf{x}}{\partial \mathbf{x}}\right)\right| \end{align*}$$

Here $\frac{\partial f_\theta\mathbf{x}}{\partial \mathbf{x}}$ denotes the $d \times d$ Jacobian (this needs to be easy to compute).

We typically choose a simple distribution over the latent space, $p(\mathbf{z})\sim N(\mathbf{0},\mathbf{I})$.

Suppose we compose functions $f_\theta(\mathbf{x}) = f_1\circ f_2 \circ \dots f_k(\mathbf{x};\theta)$. The log-likelihood decomposes nicely.

$$\begin{align*} \log p(\mathbf{x}) & = \log p\left(f_\theta(\mathbf{x}\right)) + \sum_{i=1}^k\log\left|\mathrm{det}\frac{\partial f_i(\mathbf{x};\theta)}{\partial \mathbf{x}}\right| \end{align*}$$

Sampling can be done easily, as long as $f_\theta^{-1}$ is tractable.

Implemented Flows

Planar and radial flows [1]. Note these have no algebraic inverse $f^{-1}(\mathbf{x})$.

$$\begin{align*} f(\mathbf{x}) & = \mathbf{x} + \mathbf{u}h(\mathbf{w}^\top \mathbf{z} + b)\\\ f(\mathbf{x}) & = \mathbf{x} + \frac{\beta(\mathbf{x}-\mathbf{x}_0)}{\alpha + \|\mathbf{x}-\mathbf{x}_0\|} \end{align*}$$

Real NVP [2]. Partition the vector $\mathbf{x}$ into components $\mathbf{x}^{(1)},\mathbf{x}^{(2)}$. Let $s,t$ be arbitrary neural networks $\mathbb{R}^d \mapsto \mathbb{R}^d$.

$$\begin{align*} f(\mathbf{x}^{(1)}) &= t(\mathbf{x}^{(2)}) + \mathbf{x}^{(1)}\odot \exp s(\mathbf{x}^{(2)})\\\ f(\mathbf{x}^{(2)}) &= t(\mathbf{x}^{(1)}) + \mathbf{x}^{(2)}\odot \exp s(\mathbf{x}^{(1)}) \end{align*}$$

Here the diagonal of the Jacobian is simply $[\exp s(\mathbf{x}^{(2)}) \exp s(\mathbf{x}^{(1)})]$.

Invertible 1x1 Convolution [3]. Use an LU decomposition for computational efficiency.

$$f(\mathbf{x})= W\mathbf{x}, \text{ where }W \text{ is square}$$

ActNorm [3]. Even more straightforward.

$$f(\mathbf{x}) = W\mathbf{x} + b, \text{ where }W \text{ is diagonal}$$

Masked Autoregressive Flow [4]. For each dimension of $\mathbf{x}$, use a neural network to predict scalars $\mu,\alpha$.

$$f(x_i) = (x_i - \mu(x_{< i})) / \mathrm{exp}(\alpha(x_{< i}))$$

Here the diagonal of the Jacobian is $\exp^{-1}(\alpha)$.

Neural Spline Flow [5]. Two versions: auto-regressive and coupling.

$$\begin{align*} f(x_i) & = \mathrm{RQS}_{g(x_{< i})}(x_i), \text{ (autoregressive) }\\\ f(\mathbf{x}^{(1)}) & = \mathrm{RQS}_{g(\mathbf{x}^{(2)})}(\mathbf{x}^{(1)}) \text{ (coupling)}\\\ f(\mathbf{x}^{(2)}) & = \mathrm{RQS}_{g(\mathbf{x}^{(1)})}(\mathbf{x}^{(2)}) \end{align*}$$

Examples

Below we show examples (in 1D and 2D) transforming a mixture of Gaussians into a unit Gaussian.

References

[1] Rezende, D. J. & Mohamed, S. Variational Inference with Normalizing Flows. in Proceedings of the 32nd International Conference on Machine Learning - Volume 37 - Volume 37 1530–1538 (JMLR.org, 2015).

[2] Dinh, L., Krueger, D., and Bengio, Y. (2014). NICE: Non-linear Independent Components Estimation.

[3] Kingma, D.P., and Dhariwal, P. (2018). Glow: Generative Flow with Invertible 1x1 Convolutions. In Advances in Neural Information Processing Systems 31, S. Bengio, H. Wallach, H. Larochelle, K. Grauman, N. Cesa-Bianchi, and R. Garnett, eds. (Curran Associates, Inc.), pp. 10215–10224.

[4] Papamakarios, G., Pavlakou, T., and Murray, I. (2017). Masked Autoregressive Flow for Density Estimation. In Advances in Neural Information Processing Systems 30, I. Guyon, U.V. Luxburg, S. Bengio, H. Wallach, R. Fergus, S. Vishwanathan, and R. Garnett, eds. (Curran Associates, Inc.), pp. 2338–2347.

[5] Durkan, C., Bekasov, A., Murray, I., and Papamakarios, G. (2019). Neural Spline Flows.

License

This code is available under the MIT License.

normalizing-flows's People

Contributors

avati avatar janosh avatar tonyduan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

normalizing-flows's Issues

plotting issue

Hi @tonyduan !

love the library.

I am getting an error on the plotting.

I was also wondering if its possible to add a jupyter notebook.
Kind regards,
Andrew

``python
plotlib.font_manager:findfont: score(<Font 'Liberation Sans' (LiberationSans-Bold.ttf) normal normal bold normal>) = 10.335
DEBUG:matplotlib.font_manager:findfont: score(<Font 'Source Code Pro' (SourceCodePro-Semibold.otf) normal normal semibold normal>) = 10.24
DEBUG:matplotlib.font_manager:findfont: score(<Font 'Droid Arabic Kufi' (DroidKufi-Bold.ttf) normal normal bold normal>) = 10.335
DEBUG:matplotlib.font_manager:findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=12.0 to DejaVu Sans ('/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf') with score of 0.050000.
Traceback (most recent call last):
File "flow_1d.py", line 78, in
plt.savefig("./examples/ex_1d.png")
File "/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/pyplot.py", line 722, in savefig
res = fig.savefig(*args, **kwargs)
File "/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/figure.py", line 2180, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2089, in print_figure
**kwargs)
File "/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 530, in print_png
cbook.open_file_cm(filename_or_obj, "wb") as fh:
File "/usr/lib/python3.7/contextlib.py", line 112, in enter
return next(self.gen)
File "/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/cbook/init.py", line 447, in open_file_cm
fh, opened = to_filehandle(path_or_file, mode, True, encoding)
File "/home/andrewcz/tutorial-env11/lib/python3.7/site-packages/matplotlib/cbook/init.py", line 432, in to_filehandle
fh = open(fname, flag, encoding=encoding)
FileNotFoundError: [Errno 2] No such file or directory: './examples/ex_1d.png'

``

GPU Usage

Hi @tonyduan,

Great implementation. I was wondering, is this all easily used on the GPU as well? I'm trying some experiments, but some components do not seem to like it that much. For example, i'm not sure how to handle the normal distribution.

Thanks!

Flow models for 1-dimensional data

I was looking at the modules and wasn't exactly clear on which flows are suitable for which purposes. I've tried most of them by now, but for example the NSF_CL and OneByOneConv do not work for 1-dimensional data.

The NSF_AR does work, but it only initializes the neural net from dim>1, so there's still very few parameters that it can train when it is 1-dimensional data, and thus the results are not that impressive in my testing.

Any thoughts in how you could use more parameters in the 1D setting?

Requesting Advice on NF Methods

I am working on a project where I sample a set of n-dimensional points from a Gaussian distribution (of learnt parameters) as follows and then evaluate those points based on a loss function to update model parameters with gradient descent.

mu, std = self.lin_1(z), self.lin_2(z)
eps = torch.Tensor(*img_shape).normal_()
return self.act((eps.cuda() * std) + mu)

I would like to transform the Gaussian distribution for being able to sample those points from a more complex learnt distribution. In other words, the model needs to learn how to best transform points obtained from the Gaussian distribution.

I would be glad if you can suggest the best normalizing flows method (transform) to employ considering the following scalability requirements (whether or not it is available in this repo). Thank you very much in advance for your suggestion.

  • I am sampling 100K-dimensional points with a batch-size of 5K; hence, the scalability is crucial.
  • The method should be memory efficient and fast to train on a RTX series desktop Nvidia GPU.
  • There should not ideally be an additional regularization parameter to my current loss function.
  • Expressiveness of the method is not as important as scalability and robustness in the training.

Request help for a flow model

Hello, tonyduan.
I want to perform inference of parameters of sin waves (phase, amplitude, frequency) based on 24 time steps so my data is of the form [batch_size,1,24] and I need to end up with [batch_size,1,3]. For that I want to make a network which can sample very fast, so I went for Inverse Autoregressive spline flow. I took parts of your code for inspiration, however I am stuck at some parts.

  1. For example can the RQS function deal with input which has some input channels?
  2. How would you make the Inverse Autoregressive spline flow? Would you simply swap Inverse and forward or would you do it in a different way (for example how it is done in the paper but then without s_t and m_t but using splines)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.