Coder Social home page Coder Social logo

pydelfi's Introduction

pydelfi

Density Estimation Likelihood-Free Inference with neural density estimators and adaptive acquisition of simulations. The implemented methods are described in detail in Alsing, Charnock, Feeney and Wandelt 2019, and are based closely on Papamakarios, Sterratt and Murray 2018, Lueckmann et al 2018 and Alsing, Wandelt and Feeney, 2018. Please cite these papers if you use this code!

Installation:

The code is in python3. There is a Tensorflow 1 (most stable, see below) and Tensorflow 2 version that can be installed as follows:

Tensorflow 1 (stable)

This can be found on the master branch and has the following dependencies:
tensorflow (<=1.15)
getdist
emcee (>=3.0.2)
tqdm
mpi4py (if MPI is required)

You can install the requirements and this package with,

pip install tensorflow==1.15
pip install git+https://github.com/justinalsing/pydelfi.git

(tensorflow-gpu==1.15 for GPU acceleration instead of tensorflow==1.15)

or alternatively, pip install the requirements and then clone the repo and run python setup.py install

Tensorflow 2

The Tensorflow 2 version can be found on the tf2-tom branch and can be installed as follows. We reccommend you do the install inside a virtual environment to keep version conflicts under control, ie.,

mkdir ~/envs
virtualenv ~/envs/pydelfi
source ~/envs/pydelfi/bin/activate

Followed by a pip install of pydelfi:

pip install git+https://github.com/justinalsing/pydelfi.git@tf2-tom

Note: the Mixture Density Networks (MDN) in the tf2 version are currently not performing as well as in the tf1 version (but the Masked Autoregressive Flows are fine). We are getting ot the bottom of this, and also working on expanding the suite of conditional density estimators in a coming update. Watch this space.

Documentation and tutorials:

Once everything is installed, try out either cosmic_shear.ipynb or jla_sne.ipynb as example templates for how to use the code; plugging in your own simulator and letting pydelfi do it's thing.

If you have a set of pre-run simulations you'd like to throw in rather than allowing pydelfi to run simulations on-the-fly, look at cosmic_shear_prerun_sims.ipynb as a template for how to do this.

If you are interested in using pydelfi with nuisance hardened data compression to project out nuisances (Alsing & Wandelt 2019), take a look at jla_sne_marginalized.ipynb.

Documentation can be found here (work in progress).

If you are interested in applying pydelfi to your problem but need some help getting started, or have an application that requires adaptations of the code, don't hesitate to get in touch with us (at [email protected]) or open an issue - we welcome collaboration!

pydelfi's People

Contributors

dfm avatar jameslzhu avatar justinalsing avatar milescranmer avatar nialljeffrey avatar sfeeney 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

pydelfi's Issues

fisher bug

In "score/score.py", line 121 should be:

if F is None: self.compute_fisher()

At the moment it is set to none, which, if I remember, was a placeholder once?

use os.path.join to join path and filename

pydelfi/pydelfi/delfi.py

Lines 160 to 161 in 5c4e7ed

self.graph_restore_filename = results_dir + graph_restore_filename
self.restore_filename = results_dir + restore_filename

It would be better to use https://docs.python.org/2/library/os.path.html#os.path.join and thus not assume that results_dir has a trailing separator. At present, without a trailing separtor, e.g., results_dir = 'here', the results_dir just becomes a prefix for filenames, not a directory.

Setup file

The current setup.py file has its Tensorflow requirement as
"tensorflow>=v1.1.0",
meaning that when I pip install the package from source in a fresh conda environment it installs the newest TensorFlow version (2.1).

I think what you want here is
"tensorflow>=1.1.0, <=1.15".

Additionally, after having done this, my install threw an error relating to mpi4py. This was fixed by me installing this package separately (not sure whether this is a problem with the setup or just an issue with my environment).

issue for n_procs>1

Hi!

I was running inferences for simplistic linear regression models. This may be my ignorance in understanding the code, but even for simplistic models like

y=mx+c, the NDE takes a lot of time to train. I do not enable any kind of fisher training for now.

additionally, if i want to use the power of n_procs>1:
it shows me an error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_1564073/1911766202.py in <module>
      1 # Do the SNL training
----> 2 DelfiEnsemble.sequential_training(simulator, compressor, n_initial, n_batch, n_populations, patience=10, save_intermediate_posteriors=True)

~/miniconda3/envs/sbi/lib/python3.7/site-packages/pydelfi/delfi.py in sequential_training(self, simulator, compressor, n_initial, n_batch, n_populations, proposal, simulator_args, compressor_args, safety, plot, batch_size, validation_split, epochs, patience, seed_generator, save_intermediate_posteriors, sub_batch)
    390             ps = np.zeros((safety * n_initial, self.npar))
    391         if self.use_mpi:
--> 392             self.comm.Bcast(ps, root=0)
    393         self.inds_prop = self.allocate_jobs(safety * n_initial)
    394         self.inds_acpt = self.allocate_jobs(n_initial)

AttributeError: 'NoneType' object has no attribute 'Bcast'

I checked my mpi installations are fine, and even works for other codes.

bug with run_mcmc returning State not 3 tuple

I get the "ValueError: too many values to unpack (expected 3)" when I try to use the emcee_sample method at the following line:

pos, prob, state = sampler.run_mcmc(x0, burn_in_chain)

Which I think means it is returning blobs as well from the emcee documentation (https://emcee.readthedocs.io/en/latest/user/sampler/#emcee.State).

With the newest version of emcee it works to instead return the state:
state = sampler.run_mcmc(x0, burn_in_chain)
and then use
sampler.run_mcmc(state.coords, main_chain)
instead of:

sampler.run_mcmc(pos, main_chain)

Reorganization of code

Can we put all python source files into a delfi folder at the root of the project? So it would be, e.g., /delfi/ndes/... , /delfi/delfi/..., etc.

Then, instead of running:

import ndes

one would run

from delfi import ndes

This is in line with other python projects.

Btw, you can now run pip install git+https://github.com/justinalsing/pydelfi.git to install this project and the requirements.

Bugs in examples (after new edits)

The latest "jla_sne.ipynb" example doesn't work.
The line with DelfiEnsemble.fisher_pretraining() now gives an error:

Traceback (most recent call last): File ".../python3.6/site-packages/emcee/ensemble.py", line 519, in __call__ return self.f(x, *self.args, **self.kwargs) TypeError: log_posterior_stacked() missing 1 required positional argument: 'data'

Also, the initial module loads are now wrong (this isn't a huge issue), they should be:

import pydelfi.ndes as ndes
import pydelfi.delfi as delfi
import pydelfi.score as score
import pydelfi.priors as priors

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.