Bayesflow
A Python library for efficient Bayesian modeling with deep learning
Last verified:
What is Bayesflow?
BayesFlow is a Python library for efficient simulation-based Bayesian workflows that enables amortized Bayesian inference using deep learning. The library allows users to create specialized neural networks that perform rapid statistical inference after a simulation-based training phase, solving both forward and inverse problems in Bayesian analysis.
Key features include four main capabilities: amortized posterior estimation (training generative networks to infer full posteriors for all existing and future data), amortized likelihood estimation (emulating simulation models for all parameter configurations), amortized model comparison (training neural classifiers to identify the best model among candidates), and model misspecification detection (ensuring posteriors are faithful approximations even when simulations don't perfectly represent reality). The library provides a user-friendly API for rapid Bayesian workflows, a rich collection of neural network architectures including inference networks and summary networks, and multi-backend support via Keras3 allowing users to choose between JAX, PyTorch, or TensorFlow.
BayesFlow is designed for researchers, data scientists, and statisticians working with complex stochastic models that are too complex to be described analytically. It is particularly useful for those working with intractable simulators, hierarchical models, time series data, and any Bayesian inference problem where traditional MCMC methods are computationally prohibitive. The library is open source and maintained by researchers from Rensselaer Polytechnic Institute, TU Dortmund University, and Heidelberg University.
Bayesflow pricing
Pricing model: Freemium
BayesFlow is completely free and open source, released under an open source license. There are no paid plans, subscription tiers, or premium features. The library can be installed via pip install bayesflow or from the development branch on GitHub. All features are included in the free version, including amortized posterior estimation, amortized likelihood estimation, amortized model comparison, and model misspecification detection.
Bayesflow pros
- Open source and free to use with no paid tiers
- User-friendly API for rapid Bayesian workflows
- Multi-backend support via Keras3 (JAX, PyTorch, TensorFlow)
- JAX backend is currently the fastest option
- Rich collection of neural network architectures
- Amortized inference provides rapid statistical inference after training
- Solves both forward and inverse Bayesian problems
- Includes model misspecification detection capabilities
- No hand-crafted summary statistics required
- Supports time series data with TimeSeriesTransformer
- Works with intractable simulators too complex for analytical description
- Active development community welcoming contributions
- Comprehensive documentation with tutorial notebooks
- Permutation-invariant architectures for set-based data
- Compatible with Python 3.9 and later
Bayesflow cons
- Requires installation of a machine learning backend (JAX, PyTorch, or TensorFlow)
- Longer simulation-based training phase before rapid inference
- Python 3.9 or later required (not compatible with older Python versions)
- Backend environment variables can be silently overwritten by IDEs like VSCode or PyCharm
- TensorFlow and TensorFlow Probability installation can be problematic
- Version 2.0+ is a complete rewrite incompatible with version 1.x projects
- NoGUI - command line and code-based interface only
- Steep learning curve for users unfamiliar with Bayesian inference and neural networks
Frequently asked questions about Bayesflow
Which backend should I use for BayesFlow?
We recommend JAX as it is currently the fastest backend. You can also use PyTorch or TensorFlow. Once installed, set the environment variable KERAS_BACKEND to your chosen backend (jax, torch, or tensorflow). You can set this via os.environ in your Python script, via conda env config vars, or via export command.
I am getting ModuleNotFoundError: No module named tensorflow when importing BayesFlow
This applies if: you want to use TensorFlow as your backend but haven't installed it; you want to use a backend other than TensorFlow but haven't set the environment variable correctly; or you have set the environment variable but it is not being picked up by Python (this can happen silently in development environments like VSCode or PyCharm). Try setting the backend in your Python script via os.environ['KERAS_BACKEND'] = '<your-backend>'.
What is the difference between BayesFlow 2.0+ and previous versions?
BayesFlow 2.0+ is a complete rewrite of the library. It shares the same overall goals with previous versions but has much better modularity and extensibility. The new BayesFlow has multi-backend support via Keras3, while the old version was based solely on TensorFlow.
I still need the old BayesFlow for some projects. How can I install it?
You can find and install the old BayesFlow version via the stable-legacy branch on GitHub. Select stable-legacy in the version picker on the documentation site to access the documentation for version 1.x.
What Python version does BayesFlow require?
BayesFlow requires Python version 3.9 or later. It will not work with older Python versions.
Can BayesFlow run without a machine learning backend installed?
No, BayesFlow will not run without a backend. You must install at least one of the following machine learning backends: JAX, PyTorch, or TensorFlow. The installer should automatically choose the appropriate TensorFlow version depending on your operating system, but you still need to install your chosen backend separately.
What are the four key capabilities of BayesFlow?
BayesFlow features: (1) Amortized posterior estimation - train a generative network to efficiently infer full posteriors for all existing and future data compatible with a simulation model; (2) Amortized likelihood estimation - train a generative network to efficiently emulate a simulation model for all possible parameter configurations; (3) Amortized model comparison - train a neural classifier to recognize the best model in a set of competing candidates; (4) Model misspecification detection - ensure resulting posteriors are faithful approximations even when simulations do not perfectly represent reality.
How do I install BayesFlow?
You can install BayesFlow via pip install bayesflow for the stable version, or pip install git+https://github.com/bayesflow-org/bayesflow.git@dev for the development version. After installation, you must also install at least one machine learning backend (JAX, PyTorch, or TensorFlow) and set the KERAS_BACKEND environment variable.
Where can I get help or report bugs for BayesFlow?
Please use the BayesFlow Forums for any BayesFlow-related questions and discussions. For bug reports and feature requests, open an issue on GitHub. The team also welcomes contributions from the community.
Who maintains and develops BayesFlow?
BayesFlow is currently managed by researchers from Rensselaer Polytechnic Institute, TU Dortmund University, and Heidelberg University. The project is partially funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) and supported by Germany's Excellence Strategy clusters, as well as the Informatics for Life initiative funded by the Klaus Tschira Foundation.