Skip to main content

BayesFlow

GitHub Actions Workflow Status Codecov DOI PyPI - License NumFOCUS Affiliated Project

BayesFlow is a Python library for simulation-based inference (SBI) with deep learning. It provides users with:

Conceptual Overview

Overview graphic on using BayesFlow. It is split into three columns: 1. Simulate: generate data from any simulation you like. 2. Amortize: use BayesFlow to define your neural estimator with any deep learning backend you choose, as it is part of the Keras ecosystem. 3. Learn: with powerful generative AI and robust diagnostic features, BayesFlow is the workflow layer for simulation-based inference.

BayesFlow is a flexible framework for Bayesian inference with deep learning—from idea to results in just a few lines of code. Train neural posterior, likelihood, ratio, or point estimators; ensemble or compose them; tackle filtering, smoothing, or arbitrary hierarchies using modern generative AI. BayesFlow works with both complex simulators and traditional parametric models, with seamless PyMC integration and a rich suite of diagnostics for validating and understanding results.

Install

We currently support Python 3.12 and 3.13. You can install the latest stable version from PyPI using:

pip install "bayesflow>=2.0"

Or with uv (recommended for faster installs):

uv add bayesflow

If you want the latest features, you can install from source:

pip install git+https://github.com/bayesflow-org/bayesflow.git@dev

If you encounter problems or require more control, see the detailed setup instructions in CONTRIBUTING.md.

Backend

To use BayesFlow, you will also need to install one of the following machine learning backends. Note that BayesFlow will not run without a backend.

If you don't know which backend to use, we recommend JAX as it is currently the fastest backend. The backend is set automatically using a priority order. You can also manually set the backend environment variable as described by Keras. For example, inside your Python script write:

import os
os.environ["KERAS_BACKEND"] = "jax"
import bayesflow

If you use conda, you can alternatively set this individually for each environment in your terminal. For example:

conda env config vars set KERAS_BACKEND=jax

Or just plainly set the environment variable in your shell:

export KERAS_BACKEND=jax

Getting Started

Using the high-level interface is easy, as demonstrated by the minimal working example below:

import bayesflow as bf

workflow = bf.BasicWorkflow(
    inference_network=bf.networks.FlowMatching(),
    inference_variables=["parameters"],
    inference_conditions=["observables"],
    simulator=bf.simulators.SIR()
)

history = workflow.fit_online(epochs=20, batch_size=32, num_batches_per_epoch=200)

diagnostics = workflow.plot_default_diagnostics(test_data=300)

For an in-depth exposition, check out our expanding list of resources below.

Books

Many examples from Bayesian Cognitive Modeling: A Practical Course by Lee & Wagenmakers (2013) are implemented with BayesFlow in this online book.

Videos

Several video tutorials are available as part of the Learning Bayesian Statistics podcast:

  1. Marvin Schmitt on Amortized Bayesian Inference with Neural Networks
  2. Jonas Arruda on Diffusion Models for Simulation-Based Inference

Tutorial notebooks

  1. Diffusion starter - A small tutorial on the power of diffusion models for SBI.
  2. Minimal amortized workflow - Build an amortized inference workflow with an SIR simulator.
  3. Time-varying parameters - Estimate latent trajectories with autoregressive estimators.
  4. Image or other spatial data - Learn parameters from or generate image data.
  5. Generalized linear models - Estimate Bayesian GLMs with multiple predictors, interactions, and varying rows.
  6. Foundation models as backbones - Easily turn TabPFN or other foundation models into Bayesian estimators.
  7. Neural likelihood estimation - Learn synthetic likelihood functions and plug them into the PyMC ecosystem.
  8. Neural ratio estimation - Learn neural ratios for downstream MCMC sampling using the classifier trick.
  9. Compositional estimation - Estimate hierarchical Bayesian models via composition.
  10. Deep Ensembles - Train different networks at the same time and combine their inferences.
  11. Self-consistency - Refine posterior estimates using unlabeled observations.
  12. Changing priors at inference time - Reuse a posterior estimator after a prior change with importance sampling.
  13. Model comparisons - Learn the basic workflow of Bayesian model comparison.
  14. Model comparison deep dive - Explore different approaches and scoring rules in the Bayesian model comparison workflow.
  15. Multimodal data - Fuse different data types for more informative inference.
  16. Bayes estimators - From simple point estimates to fully Bayesian inference.
  17. From ABC to BayesFlow - Upgrade from sequential to amortized inference.
  18. Bayesian experimental design - Perform adaptive sequential experiments.
  19. Model predictive control - Use posterior uncertainty to guide robust control of an inverted double pendulum.

Tutorial papers

  1. Arruda, J., Bracher, N., Köthe, U., Hasenauer, J., & Radev, S. T. (2025). Diffusion Models in Simulation-Based Inference: A Tutorial Review. arXiv preprint arXiv:2512.20685; accepted in Transactions on Machine Learning Research (TMLR) with Survey Certification. Project page. Paper

More tutorials are always welcome! Please consider submitting a pull request if you have a cool application that you want to contribute.

Agentic AI Workflows

Turn your AI agent into an amortized Bayesian expert using Baygent Skills.

Contributing

If you want to contribute to BayesFlow, we recommend installing it from source. See CONTRIBUTING.md for more details.

Reporting Issues

If you encounter any issues, please don't hesitate to open an issue on GitHub or ask questions on our Discourse Forums.

Documentation & Help

Documentation is available at https://bayesflow.org. Please use the BayesFlow Forums for any BayesFlow-related questions and discussions, and GitHub Issues for bug reports and feature requests.

Citing BayesFlow

If you are using the new multi-backend version of BayesFlow, we recommend citing our new software paper (Kühmichel et al., 2026). If you use the legacy version, you can still reference Radev et al. (2023).

BibTeX:

@article{kuhmichel2026bayesflow,
  title={{BayesFlow} 2: Multi-backend amortized {B}ayesian inference in Python},
  author={Kühmichel, Lars and Huang, Jerry M and Pratz, Valentin and Arruda, Jonas and Olischläger, Hans and Habermann, Daniel and Kucharsky, Simon and Elsemüller, Lasse and Mishra, Aayush and Bracher, Niels and Jedhoff, Svenja and Schmitt, Marvin and Bürkner, Paul-Christian and Radev, Stefan T},
  journal={arXiv preprint arXiv:2602.07098},
  year={2026}
}

@article{bayesflow_2023_software,
  title = {{BayesFlow}: Amortized {B}ayesian workflows with neural networks},
  author = {Radev, Stefan T and Schmitt, Marvin and Schumacher, Lukas and Elsemüller, Lasse and Pratz, Valentin and Schälte, Yannik and Köthe, Ullrich and Bürkner, Paul-Christian},
  journal = {Journal of Open Source Software},
  volume = {8},
  number = {89},
  pages = {5702},
  year = {2023}
}

FAQ


Question: I am starting with BayesFlow. Which backend should I use?

Answer: We recommend JAX as it is currently the fastest backend.


Question: I am getting ModuleNotFoundError: No module named 'tensorflow' when I try to import BayesFlow.

Answer: One of these applies:

  • You want to use TensorFlow as your backend, but you have not installed it. See the TensorFlow installation guide.

  • You want to use a backend other than TensorFlow, but have not set the environment variable correctly. See the Keras backend configuration guide.

  • You have set the environment variable, but it is not being picked up by Python. This can happen silently in some development environments (e.g., VSCode or PyCharm). Try setting the backend as shown here in your Python script via os.environ.


Question: What is the difference between BayesFlow 2 and previous versions?

Answer: BayesFlow 2.0+ is a complete rewrite of the library. It shares the same overall goals as previous versions but offers much better modularity and extensibility. Additionally, BayesFlow 2 supports multiple backends through Keras 3, whereas the previous version was based on TensorFlow.


Awesome Amortized Inference

If you are interested in reviews, software, papers, and other resources related to amortized inference, feel free to explore our community-driven list. If you'd like a paper (by yourself or someone else) featured, please add it to the list with a pull request, an issue, or a message to the maintainers.

Acknowledgments

This project is currently managed by researchers from Rensselaer Polytechnic Institute, TU Dortmund University, and Heidelberg University. It is partially funded by the National Science Foundation (NSF) Award Number 2448380 and the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) Projects 528702768 and 508399956 as well as DFG Collaborative Research Center 391.

BayesFlow is a NumFOCUS Affiliated Project.

Metadata

Release files for bayesflow 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bayesflow 2.1.1
File Size Uploaded Provenance Actions
bayesflow-2.1.1.tar.gz 435.7 kB Without provenance Details for bayesflow-2.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bayesflow 2.1.1
File Interpreter ABI Platform Actions
bayesflow-2.1.1-py3-none-any.whl Python 3 none any Details for bayesflow-2.1.1-py3-none-any.whl

Total release size: 1.1 MB

Release files / bayesflow-2.1.1.tar.gz

Download URL bayesflow-2.1.1.tar.gz
Size 435.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5eae7e8b8ba8b8bbe846ddbd4090737859b76185fa34247eb5f62f78fba94717
BLAKE2b-256 checksum
How to use checksums
5914152b8009b738633559482d67ae9dd5449139eb95a82b791025c0690cb6a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / bayesflow-2.1.1-py3-none-any.whl

Download URL bayesflow-2.1.1-py3-none-any.whl
Size 656.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ecffc5cc7e66a8d95acdf5d3a3a1ab3342ae77a78d00188ca508e8673aa30bf
BLAKE2b-256 checksum
How to use checksums
335d3eb21bea0e8a7839282523b45999a6ad1d6b3da5fec941cee80b5ea8a013
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for bayesflow 2.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page