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. Linear regression - Fit your first Bayesian regression with varying sample size.
  3. Image data - Learn parameters from or generate image data.
  4. Bayes estimators - From simple point estimates to fully Bayesian inference.
  5. From ABC to BayesFlow - Upgrade from sequential to amortized inference.
  6. SIR - Model infectious diseases through an end-to-end Bayesian workflow.
  7. Bayesian experimental design - Perform adaptive sequential experiments.
  8. Likelihood estimation and MCMC - Learn synthetic likelihood functions and plug them into the PyMC ecosystem.
  9. Multimodal data - Fuse different data types for more informative inference.
  10. Deep ensembles - Train different networks at the same time and combine their inferences.
  11. Ratio estimation and MCMC - Learn neural ratios for downstream MCMC sampling using the classifier trick.
  12. Compositional estimation - Estimate hierarchical Bayesian models via composition.
  13. T-Test and model comparison - 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. Time-varying parameters - Estimate latent trajectories with autoregressive estimators.
  16. Self-consistency with real data - Refine posterior estimates using unlabeled observations.

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.0

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.0
File Size Uploaded Provenance
bayesflow-2.1.0.tar.gz 429.5 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 1.1 MB

Release files / bayesflow-2.1.0.tar.gz

Download URL bayesflow-2.1.0.tar.gz
Size 429.5 kB
Tags Source
SHA-256 checksum
How to use checksums
71781c128ae5512533eb04b9d04e23ed63610932afe6c8337c26d1706e1764bf
BLAKE2b-256 checksum
How to use checksums
eba97d6332d559ff6a17df0716276ccae319fcb67cc209b577d917ff947ca311
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0-py3-none-any.whl

Download URL bayesflow-2.1.0-py3-none-any.whl
Size 648.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4114015e4d9b656e539e2fc82438f9e10e15d9d64883ef80db8fc60f506d23e7
BLAKE2b-256 checksum
How to use checksums
b80381086124434b5adf87ccc0672caade3b68822f12f750a1d4427b2171f751
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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.0

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