Git Product home page Git Product logo

bnaf's Introduction

BNAF

Pytorch implementation of Block Neural Autoregressive Flow based on our paper:

De Cao Nicola, Titov Ivan and Aziz Wilker, Block Neural Autoregressive Flow (2019)

Requirements

  • python>=3.6 (it will probably work on older versions but I have not tested on them)
  • pytorch>=1.0.0

Optional for visualization and plotting: numpy, matplotlib and tensorboardX

Structure

  • bnaf.py: Implementation of Block Neural Normalzing Flow.
  • toy2d.py: Experiments of 2d toy task (density estimation and energy matching).
  • density_estimation.py: Experiments on density estimation on real datasets.
  • optim: A custom extension of torch.optim.Adam and torch.optim.Adamax with Polyak averaging. A custom extension of torch.optim.lr_scheduler.ReduceLROnPlateau with callbacks.
  • data: Data classes to handle the real datasets.

Usage

Below, example commands are given for running experiments.

Download datasets

Run the following command to download the datasets:

./download_datasets.sh

Run 2D toy density estimation

This example runs density estimation on the 8 Gaussians dataset using 1 flow of BNAF with 2 layers and 100 hidden units (50 * 2 since the data dimensionality is 2).

python toy2d.py --dataset 8gaussians \    # which dataset to use
                --experiment density2d \  # which experiment to run
                --flows 1 \               # BNAF flows to concatenate
                --layers 2 \              # layers for each flow of BNAF
                --hidden_dim 50 \         # hidden units per dimension for each hidden layer
                --save                    # save the model after training
                --savefig                 # save the density plot on disk

Imgur

Run 2D toy energy matching

This example runs energy matching on the t4 function using 1 flow of BNAF with 2 layers and 100 hidden units (50 * 2 since the data dimensionality is 2).

python toy2d.py --dataset t4 \            # which dataset to use
                --experiment energy2d \   # which experiment to run
                --flows 1 \               # BNAF flows to concatenate
                --layers 2 \              # layers for each flow of BNAF
                --hidden_dim 50 \         # hidden units per dimension for each hidden layer
                --save                    # save the model after training
                --savefig                 # save the density plot on disk

Imgur

Run real dataset density estimation

This example runs density estimation on the MINIBOONE dataset using 5 flows of BNAF with 0 layers.

python density_estimation.py --dataset miniboone \  # which dataset to use
                             --flows 5 \            # BNAF flows to concatenate
                             --layers 0 \           # layers for each flow of BNAF
                             --hidden_dim 10 \      # hidden units per dimension for each hidden layer
                             --save                 # save the model after training

Citation

De Cao Nicola, Titov Ivan, Aziz Wilker,
Block Neural Autoregressive Flow,
35th Conference on Uncertainty in Artificial Intelligence (UAI19) (2019).

BibTeX format:

@article{bnaf19,
  title={Block Neural Autoregressive Flow},
  author={De Cao, Nicola and
          Titov, Ivan and
          Aziz, Wilker},
  journal={35th Conference on Uncertainty in Artificial Intelligence (UAI19)},
  year={2019}
}

Feedback

For questions and comments, feel free to contact Nicola De Cao.

License

MIT

bnaf's People

Contributors

gauenk avatar nicola-decao 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

bnaf's Issues

Silent error in handling of univariate case

The forward pass of the BNAF class should be changed to the following (or similar) to properly handle the univariate case or calling model will reduce the gradients (grad) to a single number when it should have a first dim size of batch_size. I confirmed the following will work though.

   ```
    grad = grad.squeeze()
    reduce_sum = len(grad.shape) > 1

    if reduce_sum:
        if self.res == 'normal':
            return inputs + outputs, torch.nn.functional.softplus(grad.squeeze()).sum(-1)
        elif self.res == 'gated':
            return self.gate.sigmoid() * outputs + (1 - self.gate.sigmoid()) * inputs, \
                   (torch.nn.functional.softplus(grad.squeeze() + self.gate) - \
                    torch.nn.functional.softplus(self.gate)).sum(-1)
        else:
            return outputs, grad.squeeze().sum(-1)
    else:
        if self.res == 'normal':
            return inputs + outputs, torch.nn.functional.softplus(grad)
        elif self.res == 'gated':
            return self.gate.sigmoid() * outputs + (1 - self.gate.sigmoid()) * inputs, \
                   (torch.nn.functional.softplus(grad + self.gate) - \
                    torch.nn.functional.softplus(self.gate))
        else:
            return outputs, grad

scalability

Hi @nicola-decao very nice work! I am thinking of using BNAF to do variational inference where the posterior is over a few thousand to tens of thousands dimensional space. I wonder if the current implementation can scale up to that many dimension. My concern is that the model might not fit into the GPU memory. Can you provide an estimate of the space complexity a given architecture consisting of, say, n stacked flows of m hidden layers each? I know you gave an estimate of number of parameters in table 2 in the paper but how does that translate into memroy requirement? I appreciate your insight in this because I am more of a tensorflow person so trying this out in pytorch will likely take me a while. Thanks in advance!

Error in weight normalization code?

In the BNAF-->maskedweights-->get_weights function, there is a line as follows...

w = torch.exp(self._weight) * self.mask_d + self._weight * self.mask_o

I believe the torch.exp(self._weight) * self.mask_d should be torch.exp(self._diag_weight) * self.mask_d though right?

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.