\pkg BayesFlow 2.0: Multi-Backend Amortized Bayesian Inference in \proglang Python Lars Kühmichel TU Dortmund University, Jerry M. Huang Rensselaer Polytechnic Institute, Valentin Pratz Heidelberg University, Jonas Arruda University of Bonn, Hans Olischläger TU Dortmund University, Daniel Habermann TU Dortmund University, Šimon Kucharský TU Dortmund University, Lasse Elsemüller Independent Scientist, Aayush Mishra TU Dortmund University, Niels Bracher Rensselaer Polytechnic Institute, Svenja Jedhoff TU Dortmund University, Marvin Schmitt Independent Scientist, Paul-Christian Bürkner TU Dortmund University, Stefan T. Radev Rensselaer Polytechnic Institute Abstract \Plainauthor Lars Kühmichel et al. \Plaintitle BayesFlow 2.0: Multi-Backend Amortized Bayesian Inference in Python \Shorttitle BayesFlow 2.0 \Abstract Modern Bayesian inference involves a mixture of computational methods for estimating, validating, and drawing conclusions from probabilistic models as part of principled workflows. An overarching motif of many Bayesian methods is that they are relatively slow, which often becomes prohibitive when fitting complex models to large data sets. Amortized Bayesian inference (ABI) offers a path to solving the computational challenges of Bayes. ABI trains neural networks on model simulations, rewarding users with rapid inference of any model-implied quantity, such as point estimates, likelihoods, or full posterior distributions. In this work, we present the \proglang Python library \pkg BayesFlow, Version 2.0, for general-purpose ABI. Along with direct posterior, likelihood, and ratio estimation, the software includes support for multiple popular deep learning backends, a rich collection of generative networks for sampling and density estimation, complete customization and high-level interfaces, as well as new capabilities for hyperparameter optimization, design optimization, and hierarchical modeling. Using a case study on dynamical system parameter estimation, combined with comparisons to similar software, we show that our streamlined, user-friendly workflow has strong potential to support broad adoption. \Keywords Amortized Bayesian Inference, Simulation-Based Inference, Deep Learning \Plainkeywords Amortized Bayesian Inference, Simulation-Based Inference, Deep Learning \Address Lars Kühmichel Department of Statistics TU Dortmund University 44227 Dortmund, Germany E-mail: URL: https://github.com/LarsKue 1 Introduction Simulation-based inference ( Cranmer et al. 2020 ; Deistler et al. 2025 , SBI;) has become an increasingly prevalent element in computational science ( Lavin et al. 2021 ) . SBI addresses the problem of estimating unknown parameters \theta from data \mathcal{D} based on a probabilistic model p(\theta,\mathcal{D}) that can be simulated. Although it was originally motivated in settings where traditional Bayesian methods are intractable or infeasible ( Diggle and Gratton 1984 ) , SBI is steadily emerging as a general approach to Bayesian computation ( Zammit-Mangion et al. 2025 ) . In many instances, we may want to fit a model to multiple data sets, be it to analyze big data ( von Krause et al. 2022 ) or to verify key properties of the model (e.g., identifiability) in silico ( Bürkner et al. 2025 ) . Thus, it becomes desirable to pool or “compile” computations into global estimators that can produce near-instant results for arbitrary queries, unless we dispose of infinite time of computational resources. As a general framework for efficient probabilistic reasoning and model inversion ( Gershman and Goodman 2014 ; Stuhlmüller et al. 2013 ; Kingma and Welling 2013 ; Paige and Wood 2016 ; Le et al. 2017 ) , amortized inference achieves this by learning estimators to answer many queries with minimal recomputation. As the emerging standard of SBI workflows, amortized Bayesian inference (ABI) trains neural networks on simulations from p(\theta,\mathcal{D}) . The cost of this training amortizes by reusing the networks for rapid inference of any model-implied quantity, such as point estimates, likelihoods, or full posterior distributions. As such, ABI offers both alternative and complementary solutions to established methods for approximate inference, such as Markov chain Monte Carlo ( Brooks et al. 2011 , MCMC;) , variational inference ( Ranganath et al. 2014 ) , or Laplace approximation ( Rue et al. 2009 ) . Version 2.0 of \pkg BayesFlow represents a complete redesign that substantially extends and improves upon the initial release ( Radev et al. 2023b ) . It adopts the widespread shift from isolated analyses to iterative Bayesian workflows ( Gelman et al. 2020 ; Li et al. 2024 ) . Further, it aligns with the growing interest in AI-assisted statistics ( Musslick et al. 2025 ) , where neural surrogates can replace any performance-critical component of classical workflows ( Ye et al. 2025 ) . With a streamlined high-level user interface for ABI workflows, \pkg BayesFlow addresses the demand for accessible and flexible software solutions. As part of the \pkg Keras 3 ecosystem ( Chollet et al. 2025 ) , \pkg BayesFlow supports all popular deep-learning backends and integrates smoothly into modern ML workflows within \proglang Python. It is highly portable and can also be accessed from \pkg R ( R Core Team 2025 ) , enhancing interoperability with the \pkg R ecosystem. With robust default settings, it lowers the entry barrier for new users. Most importantly, \pkg BayesFlow covers the full Bayesian workflow—from building and fitting complex models (e.g., likelihood-free or likelihood-based, hierarchical or flat, dynamic or stationary, etc ) to model comparison, criticism, and deployment. 2 Amortized Bayesian Inference Figure 1: Side-by-side comparison of likelihood-based ( explicit ) and simulation-based ( implicit ) models from a Bayesian perspective. In likelihood-based models, the data model p(\mathcal{D}\mid\theta) and prior p(\theta) can be explicitly sampled and evaluated. In simulation-based models, the data model p_{\text{implicit}}(\mathcal{D}\mid\theta) and prior p(\theta) can be sampled through stochastic simulation, but cannot be (easily) evaluated. 2.1 Learning From Simulations BayesFlow expects the modeler to specify a probabilistic model p(\theta,\mathcal{D}) over empirical (observable) quantities \mathcal{D} and latent (unobservable) quantities \theta . Traditionally, the joint model is completely defined by a likelihood function p(\mathcal{D}\mid\theta) and a prior p(\theta) : p(\theta,\mathcal{D})=p(\mathcal{D}\mid\theta)p(\theta). (1) In contrast to modern MCMC-based software such as \proglang Stan ( Stan Development Team 2025 ) or \pkg PyMC ( Abril-Pla et al. 2023 ) , \pkg BayesFlow does not need to evaluate any of these distributions for inference. Instead, it only relies on the ability to simulate from the joint model p(\theta,\mathcal{D}) according to: \mathcal{D}=\text{Sim}(\theta,\xi),\quad\theta\sim p(\theta),\;\xi\sim\text{RNG}(\cdot)\quad\Longleftrightarrow\quad\mathcal{D}\sim p_{\text{implicit}}(\mathcal{D}\mid\theta),\quad\theta\sim p(\theta), (2) where \xi\in\Xi denotes random (outsourced) noise variates and p_{\text{implicit}}(\mathcal{D}\mid\theta) denotes the likelihood implied by the left-hand side. Under certain conditions, this implicit likelihood can be written as an integral over the stochastic components of the simulator: p_{\text{implicit}}(\mathcal{D}\mid\theta)=\int_{\Xi}p(\mathcal{D},\xi\mid\theta)\,\text{d}\xi, (3) which is typically intractable and motivates the application of simulation-based inference ( Cranmer et al. 2020 , SBI;) . In modern SBI, simulations serve as training data for specialized neural networks. Once trained, these networks can infer any model quantity of interest (e.g., parameters) with minimal overhead, amortizing the initial investment in training the networks. This approach applies both to traditional models with closed-form likelihoods and to “likelihood-free” models that can only be simulated (see Figure 1 ). 2.2 Bayesian Parameter Estimation BayesFlow adopts a Bayesian framework for parameter estimation, enabling inference over the full posterior distribution: p(\theta\mid\mathcal{D})\propto p(\mathcal{D}\mid\theta)p(\theta). (4) We cast posterior learning as minimizing an expected objective over joint draws (\theta,\mathcal{D})\sim p(\theta,\mathcal{D}) : \displaystyle\hat{q} \displaystyle=\operatorname*{argmin}_{q}\mathbb{E}_{(\theta,\mathcal{D})\,\sim\,p(\theta,\mathcal{D})}\left[\mathcal{J}\big(q(\cdot\mid\mathcal{D}),\,\theta\big)\right] (5) \displaystyle\approx\operatorname*{argmin}_{q}\frac{1}{B}\sum_{b=1}^{B}\mathcal{J}(q(\cdot\mid\mathcal{D}^{(b)}),\theta^{(b)}), (6) where B is the simulation budget , \mathcal{J} is a strictly proper scoring rule (e.g., the log score), and q is the approximate distribution defined by a generative neural network. Different choices of the objective \mathcal{J} recover well-known estimators. Using the log score yields the maximum likelihood objective ( Papamakarios et al. 2021 ) , whereas using a score-based objective corresponds to denoising score matching ( Arruda et al. 2025 ) . Although the latter does not rely on proper scoring rules, it can be interpreted as a surrogate objective that approximates maximum likelihood training under certain conditions ( Song et al. 2021 ) . \pkg BayesFlow can also learn arbitrary Bayesian point estimators ( Sainsbury-Dale et al. 2024 ) , such as posterior means, variances, or quantiles. The resulting optimization objective simply replaces the approximate distribution q in Eq. 5 with the desired point estimate \hat{f}(\mathcal{D}) : \hat{f}=\operatorname*{argmin}_{f}\mathbb{E}_{(\theta,\mathcal{D})\,\sim\,p(\theta,\mathcal{D})}\left[\mathcal{J}\big(f(\mathcal{D}),\,\theta\big)\right], (7) where \mathcal{J} is now a scoring rule for point estimators. In either case, inference is amortized: once trained, \hat{q} can efficiently sample from the approximate posterior or \hat{f} can produce the point estimates for any upcoming data \mathcal{D}_{\rm new} compatible with the joint model p(\theta,\mathcal{D}) . 2.3 Bayesian Model Verification Model verification refers to assessing a model’s assumptions in silico , prior to applying it to real data where those assumptions may not hold ( Bürkner et al. 2025 ) . It typically involves estimating posteriors from simulated data and verifying key properties such as calibration (i.e., whether credible intervals are trustworthy) and recovery (i.e., whether parameters can be accurately estimated). Traditionally associated with the high computational cost of model refitting, model verification in \pkg BayesFlow can be performed essentially for free due to amortization. Below, we discuss two key aspects of model verification. Calibration Consider a target quantity of interest T=T(\theta) that is a function of the parameters \theta . This could include the parameters themselves or any pushforward quantity, such as posterior predictions. If the model is well-specified (i.e., is equal to the true data-generating distribution), then the following equality holds: \alpha=\mathbb{E}_{(\tilde{\theta},\mathcal{D})\,\sim\,p(\theta,\mathcal{D})}\left[\mathbb{I}(T(\tilde{\theta})\in U_{\alpha}(T(\theta)\mid\mathcal{D}))\right], (8) for all target quantities T and all uncertainty regions U_{\alpha}(T(\theta)\mid\mathcal{D}) obtained from the analytic posterior p(\theta\mid\mathcal{D}) with nominal coverage probability \alpha , where \tilde{\theta} denotes a simulated ground truth ( Bürkner et al. 2023 ) . In other words, the probability that an uncertainty region (e.g., a credible interval) with coverage probability \alpha contains the true value must be equal to \alpha on average. This property can be used to verify the calibration of an approximate posterior: If the approximation is good enough, then Eq. ( 8 ) should hold for the uncertainty regions implied by the approximation. However, verifying calibration requires fitting the model to many (usually hundreds of) simulated data sets ( Modrák et al. 2023 ) . This can be computationally challenging for MCMC-based or other non-amortized algorithms. In \pkg BayesFlow, amortization provides a decisive advantage: once trained, a posterior estimator can self-diagnose efficiently across the entire sample space, making simulation-based calibration (SBC) routine. Parameter recoverability Another key aspect of model verification concerns whether the model can accurately recover its own parameters. Formally, recoverability can be expressed as \text{Recovery}(\mathcal{L}):=\mathbb{E}_{(\tilde{\theta},\mathcal{D})\,\sim\,p(\theta,\mathcal{D})}\left[\int_{\Theta}\mathcal{L}(\tilde{\theta},\theta)\,p(\theta\mid\mathcal{D})\,\text{d}\theta\right], (9) where \mathcal{L} denotes a loss metric (e.g., root mean square error; RMSE). Analogous to calibration, traditional recovery analyses are computationally demanding due to repeated posterior inference over many simulated datasets ( Schad et al. 2021 ) . In contrast, \pkg BayesFlow can analyze recovery essentially instantaneously after training. 2.4 Likelihood Estimation In some applications, learning a surrogate q(\mathcal{D}\mid\theta) for the intractable likelihood p_{\text{implicit}}(\mathcal{D}\mid\theta) may be desirable. Learning the likelihood decouples model training from prior specification and thus enables flexible reuse, for instance, in hierarchical models or regression models where parameters \theta are functions of other variables ( Papamakarios et al. 2019 ; Lueckmann et al. 2019 ; Fengler et al. 2021 ; Boelts et al. 2022 ) . Additionally, a neural likelihood can serve as a fast, differentiable emulator for an otherwise expensive simulator. From the perspective of \pkg BayesFlow, likelihood estimation merely amounts to swapping the arguments in Eq. 5 , where now the approximate density q(\cdot\mid\theta) is conditioned on the parameters. Learning the likelihood may require larger simulation budgets if the dimensionality of \mathcal{D} is high relative to that of \theta . Moreover, if the goal is to eventually estimate posteriors, the resulting pipeline is not amortized, since it requires downstream MCMC for every \mathcal{D} ( Radev et al. 2023a ) . Related to likelihood estimation is likelihood-to-evidence ratio estimation, approximating the density ratio p(\mathcal{D}\mid\theta)\,/\,p(\mathcal{D}) ( Hermans et al. 2020 ; Durkan et al. 2020b ; Miller et al. 2022b ) . Since this ratio is proportional to the likelihood, a likelihood-to-evidence ratio estimate can be used in place of the likelihood density, for example, for subsequent posterior estimation with MCMC. \pkg BayesFlow implements the stable contrastive training of Miller et al. 2022b , which uses a simple multiclass classifier. 2.5 Bayesian Model Comparison The preceding probabilistic quantities depend implicitly on various model assumptions. These can be abstractly denoted as M , and comparing the utility of different assumptions requires Bayesian model comparison ( MacKay 2003 , BMC;) . Formally, prior predictive BMC depends on a model’s marginal likelihood (i.e., the normalizer of Eq. 4 ): p(\mathcal{D}\mid M)=\int_{\Theta}p_{\text{implicit}}(\mathcal{D}\mid\theta,M)\,p(\theta\mid M)\,\text{d}\theta, (10) which is doubly intractable for implicit models. Since marginal likelihoods are normalized probability distributions, they penalize model complexity by trading off sharpness for dispersion: complex models can generate a broader range of data, but must distribute their probability mass across a greater volume of the sample space. The ratio of marginal likelihoods for two models is known as the Bayes factor ( Kass and Raftery 1995 , BF;) : \text{BF}_{12}:=\frac{p(\mathcal{D}\mid M_{1})}{p(\mathcal{D}\mid M_{2})}=\frac{p(M_{1}\mid\mathcal{D})}{p(M_{2}\mid\mathcal{D})}\;\Big/\;\frac{p(M_{1})}{p(M_{2})}. (11) The first term on the right expresses the posterior odds, while the second term represents the prior odds. For a set of J models, we can equivalently compute the corresponding J posterior model probabilities p(M_{j}\mid\mathcal{D}) assuming that the true model is within the set of considered models. \pkg BayesFlow estimates the posterior model probabilities by treating BMC as a probabilistic classification task: \hat{q}=\operatorname*{argmin}_{q}\mathbb{E}_{(M,\theta,\mathcal{D})\,\sim\,p(M)p(\theta,\mathcal{D}\,\mid\,M)}\left[\mathcal{J}\big(q(\cdot\mid\mathcal{D}),M\big)\right], (12) where \mathcal{J} denotes a proper scoring rule (e.g., loss or log score) and M is the true model index. In essence, we define a supervised learning problem over simulated pairs (M,\mathcal{D}) and train a probabilistic classifier to predict model labels ( Radev et al. 2021 ; Pudlo et al. 2016 ; Jeffrey and Wandelt 2024 ) . Alternatively, \pkg BayesFlow can approximate the log marginal likelihood (LML) using a pair of trained posterior and likelihood estimators and rearranging Bayes’ rule: \log\hat{q}(\mathcal{D}\mid M)=\log\hat{q}(\mathcal{D}\mid\theta,M)+\log p(\theta\mid M)-\log\hat{q}(\theta\mid\mathcal{D},M). (13) Since the quantity on the left-hand side is constant with respect to \theta , we can theoretically plug in every \theta\in\Theta in the right-hand side and obtain an estimate of the LML. However, due to imperfect posterior and likelihood estimation, different values of \theta will lead to different values of \log\hat{q}(\mathcal{D}\mid M) , providing a measure of approximation error in addition to a point estimate of the LML. This error can be reduced by using the analytic likelihood whenever available ( Radev et al. 2023a ; Kucharský et al. 2025 ) . Finally, we can also rank models according to posterior predictive quantities computed from new data \tilde{\mathcal{D}} , such as the expected log predictive density ( Vehtari and Ojanen 2012 , ELPD;) : \text{ELPD}(M)=\mathbb{E}_{\tilde{D}\,\sim\,p(\mathcal{D})}\left[\log\int_{\Theta}p_{\text{implicit}}(\tilde{\mathcal{D}}\mid\theta,M)\,p(\theta\mid\mathcal{D})\text{d}\theta\right]. (14) The ELPD is triply intractable, but the outer expectation can be estimated using cross-validation, whereas the inner expectation can be estimated by averaging a likelihood surrogate over approximate posterior samples ( Radev et al. 2023a ) . 2.6 Bayesian Sensitivity Analysis The results of any (Bayesian) analysis are sensitive to modeling choices related to likelihood specification, prior elicitation, and data processing. For instance, a common question in Bayesian analysis is: How would the results look under a different prior? Sensitivity analysis provides a formal answer to such questions and typically requires refitting the model under different assumptions. The resulting computational bottleneck can be resolved in \pkg BayesFlow by extending the amortization scope with context variables C that indicate potential factors of inferential variation. For example, C can encode the width of the prior or represent an entirely different model specification. To enable amortized, sensitivity-aware analysis, we can extend the joint model to p(C,\theta,\mathcal{D}) and make C an additional input to the neural estimator. Sensitivity-aware estimation then simply extends the objective in Eq. ( 5 ) to include C : \hat{q}=\operatorname*{argmin}_{q}\mathbb{E}_{(C,\theta,\mathcal{D})\,\sim\,p(C,\theta,\mathcal{D})}\left[\mathcal{J}\big(q(\cdot\mid\mathcal{D},C),\theta\big)\right]. (15) During inference, the modeler can supply different values of C and assess the resulting differences qualitatively or quantitatively ( Elsemüller et al. 2024a ) . Additionally, in order to estimate the sensitivity of results with regard to network and training hyperparameters, one can train an ensemble of networks and subsequently analyze the variation across ensemble members. 2.7 Handling Model Misspecification In countless settings, researchers model complex phenomena with misspecified models ( Walker 2013 ) . There are many criteria for determining whether a misspecified (read: wrong) model is nevertheless a useful one ( Bürkner et al. 2023 ) . In the context of SBI, model misspecification translates into simulated data that lacks relevant structure present in the real data. Simulation gaps are not just evidence that the model is misspecified; when they are severe, the trained network may no longer estimate the same quantity it was designed to estimate during training ( Schmitt et al. 2023 ; Gloeckler et al. 2023 ; Frazier et al. 2024 ) . A useful way to see this as a unique problem is to contrast amortized inference with MCMC. If an MCMC sampler mixes and converges, it will (by construction) return the posterior implied by Bayes’ rule for the assumed model, even if that posterior is a poor representation of reality. Amortized neural estimators are different: under a simulation gap, they can drift away from the Bayes posterior of the nominal model and instead output something that reflects the training distribution and the network’s inductive biases. In that case, the posterior estimate q(\theta\mid\mathcal{D}) may no longer correspond to any coherent Bayesian update for the model, which undermines how we interpret the reported uncertainties and point estimates. Consequently, detecting and mitigating model misspecification is now widely viewed as an integral part of an amortized Bayesian workflow ( Li et al. 2024 ) . \pkg BayesFlow provides tools to detect potential simulation gaps by inspecting the typicality of real data relative to the distribution of learned data embeddings or summary statistics \phi(\mathcal{D}) . This can be done using any out-of-distribution (OOD) or outlier detection score as a proxy measure of potential “extrapolation bias” ( Frazier et al. 2024 ) . The embedding distribution p(\phi(\mathcal{D})) can be further “Gaussianized” via an additional training loss ( Schmitt et al. 2023 ) to meet the assumptions of simple parametric methods, such as the Mahalanobis distance ( Li et al. 2024 ) . Mitigating the impact of model misspecification in amortized estimators remains an active field of research. Sometimes, the simplest solution is to assume a corrupted model p_{\epsilon}(\theta,\mathcal{D}) that can simulate outliers and induce a robust neural estimator ( Ward et al. 2022 ) , trading off some accuracy for much higher breakdown points ( Wu et al. 2024 ) . Additional loss functions, such as Bayesian self-consistency ( Mishra et al. 2025 ) or unsupervised domain adaptation ( Huang et al. 2023 ; Elsemüller et al. 2025 ) can improve robustness as well. Alternatively, for datasets flagged as OOD, one can try to correct the posteriors post hoc using Pareto-smoothed importance sampling (PSIS) when a likelihood is available ( Vehtari et al. 2024 ) , or more generally via inference-time adaptation ( Siahkoohi et al. 2023 ) . Crucially, \pkg BayesFlow provides an interface for implementing and testing various methods for increasing the robustness and trustworthiness of ABI ( Schmitt et al. 2023 ; Huang et al. 2023 ; Mishra et al. 2025 ; Elsemüller et al. 2025 ; Elsemüller et al. 2024a ; Li et al. 2024 ) . 3 Software Figure 2: Overview of the basic amortized Bayesian workflow provided by \pkg BayesFlow. The \code Simulator interface provides users with automated grouping of data-generating functions and simulated data. The \code Adapter interface transforms the raw simulator outputs for training the neural approximator. The neural approximator typically consists of a \code SummaryNetwork that encodes observables into a latent summary vector, and an \code InferenceNetwork conditioned on this vector to approximate target distributions. The resulting estimates are then used for model validation through \pkg BayesFlow’s \code diagnostics module, with a wide array of graphical and numerical diagnostics for checking computational faithfulness and model sensitivity. The entire workflow is encapsulated at the high level via the \code Workflow object, which allows users to rapidly iterate through the Bayesian workflow. The \pkg BayesFlow software is divided into modules, which together enable complete end-to-end ABI workflows. Below, we introduce all the main modules of \pkg BayesFlow (see also Figure 2 ). 3.1 Simulators Since ABI performs simulation-based inference, simulators play a critical role in \pkg BayesFlow. The package offers both lower and higher level simulator interfaces that provide users with different options for balancing expressivity, computational efficiency, and ease-of-use. On a lower-level, simulators in \pkg BayesFlow are specified as a class with a \code sample(batch_size) method, returning a dictionary of data in the form of \pkg NumPy arrays. The keys of the dictionary are arbitrary, user-chosen strings, denoting the variable names. For example, consider the simple probabilistic model given by \displaystyle\mu \displaystyle\sim\text{Normal}(0,1) \displaystyle\sigma \displaystyle\sim\text{Exponential}(1) \displaystyle x_{n} \displaystyle=\text{Normal}(\mu,\sigma)\quad n=1,\ldots,N This can be coded in the form of a \pkg BayesFlow simulator as {Code} import numpy as np import bayesflow as bf class MySimulator(bf.simulators.Simulator): def sample(self, batch_size, N=10): mu = np.random.normal(size=(batch_size, 1)) sigma = np.random.exponential(size=(batch_size, 1)) x = np.random.normal(size=(batch_size, N), loc=mu, scale=sigma) return "mu": mu, "sigma": sigma, "x": x We can then simulate (a batch of) parameter-data pairs from the simulator via {Code} simulator = MySimulator() sims = simulator.sample(batch_size=5) For computational efficiency, \pkg BayesFlow assumes that a single call to \code sample(batch_size) will return a batched dictionary of data, that is, the shape of all value arrays is \code (batch_size, …). For the above example, \code sims["theta"] has shape \code (batch_size, 2) and \code sims["x"] has shape \code (batch_size, N). The need for a \code batch_size may not be intuitive for users without a deep learning background, but is required for mini-match optimization, essential for most deep learning applications. For convenience, \pkg BayesFlow also supplies users with a utility to auto-batch simulators starting from unbatched functions: \code bf.make_simulator. Using this higher-level simulator interface, the same probabilistic model as above can now be coded as {Code} def prior(): mu = np.random.normal() sigma = np.random.exponential() return "mu": mu, "sigma": sigma def likelihood(mu, sigma, N=10): x = np.random.normal(size=N, loc=mu, scale=sigma) return "x": x simulator = bf.make_simulator([prior, likelihood]) In the above simulator, the sample size \code N is considered fixed for the whole batch. This is important as \code N influences the shape of \code x and simulations can only be safely concatenated into batches if they have the same shape for each simulator run. In order to also vary \code N within the simulator, we need to vary it by batch and not by simulation within batch. For this purpose, \code bf.make_simulator provides the optional \code meta_fn argument, which takes a function that returns a dictionary of variables that should only vary by batch (and be constant within batches). If provided, \pkg BayesFlow calls \code meta_fn once per batch and prepends its outputs to the regular simulator call. Extending our above simulator to sample N uniformly between 10 and 100 , we can write {Code} def meta(): return "N": np.random.randint(10, 100) simulator = bf.make_simulator([prior, likelihood], meta_fn=meta) The output of \code simulator.sample() now contains a new entry \code "N" whose value is just a single integer: the sample size used for all simulations within the batch. To increase speed, simulations can also be run in parallel with the \code simulator.sample_parallel() method. For models with more complex probabilistic factorizations than just a single set of parameters implying data, one can use the \code HierarchicalSimulator class, among others, enabling simulations for multilevel and mixture models ( Habermann et al. 2024 ; Kucharskỳ and Bürkner 2025 ) . 3.2 Adapters The raw simulator outputs are not immediately usable to train neural networks. Rather, multiple pre-processing steps have to be performed first. This specifically includes (a) bringing variables into correct shapes (e.g., broadcasting, splitting, or concatenating variables), (b) constraining variables (e.g., enforcing non-negativity), and (c) informing the networks which variables are to be used for which purpose (e.g., which variables are parameters and which are data). \pkg BayesFlow provides a single interface for all these pre-processing tasks: the \code Adapter, which takes a dictionary as input (usually produced by a \code Simulator) and subsequently returns a dictionary of transformed variables. To make this more concrete, for our simple model above, we need to (1) broadcast \code "N" to the shape of \code "x" since we eventually want to combine both as neural network conditions, (2) indicate that \code "x" is a set of exchangeable values whose order does not matter for inference, (3) constrain \code "sigma" to always be predicted as positive, and (4) square-root transform \code "N" for improved numerical stability. Additionally, we need to indicate which variables will have which role in the amortized inference task. In our example, \code "mu" and \code "sigma" are the parameters of interest, whose posterior we will eventually seek to infer conditional on data. As such, these two variables will be concatenated as \code "inference_variables", a protected variable name understood by subsequent modules. The data \code "x" shall be first summarized by a summary network, which we indicate by renaming \code "x" to \code "summary_variables" in the adapter. Lastly, we also want to condition our inference on the sample size \code "N", but intend to pass it to the inference network directly as no summarization is needed, achieved by renaming \code "N" to \code "inference_conditions". Putting all of these transforms together in the form of a single adapter is achieved by the following code: {Code} adapter = ( bf.Adapter() .broadcast("N", to="x") .as_set("x") .constrain("sigma", lower=0) .sqrt("N") .concatenate(["mu", "sigma"], into="inference_variables") .rename("x", "summary_variables") .rename("N", "inference_conditions") ) In each of the above lines, an additional transform is added to the adapter, to be executed sequentially once fed with data. The shown syntax avoids having to repeat \code adapter. at every line. In more verbose form, the above code would read {Code} adapter = bf.Adapter() adapter = adapter.broadcast("N", to="x") adapter = adapter.as_set("x") … Once defined, we can directly pass the output of \code simulator.sample to \code adapter: {Code} sims = simulator.sample(batch_size=5) adapted_sims = adapter(sims) That said, users will rarely have to call adapters directly like this. Rather, adapters will be called automatically within the training and inference phase by other modules. An important additional property of adapters is that most transforms are invertible . That is, we could call {Code} adapter(adapted_sims, inverse=True) to get back our original simulations. Some transforms are not invertible by definition and will thus be ignored during inversion. For example \code adapter.drop("") drops a given variable from the dictionary, a transform that cannot be undone by inversion. In practice, invertibility is only required for transforms involving inference variables (here, parameters \code "mu" and \code "sigma"). This way, at inference time, the neural network predictions can traverse the adapter in inverse order, yielding the inferred variables with their original names and scales. Generative Family Architecture Sampling Density Evaluation Normalizing Flows Constrained Single-step Fast Free-Form Flows Semi-Constrained Single-step Moderate Diffusion Models Unconstrained Multi-step Slow Flow Matching Unconstrained Multi-step Slow Consistency Models Unconstrained Few-step N/A Table 1: Generative neural network families available in BayesFlow. 3.3 Networks In most applications, \pkg BayesFlow employs two neural networks that together enable learning the target distribution: a \code SummaryNetwork that encodes (potentially variable-length) observations into a fixed-length latent summary vector, and an \code InferenceNetwork that conditions on this latent summary to produce an approximation of the target (see also Figure 2 ). For a given application, the specific choice of \code SummaryNetwork should depend on the data that is supposed to be summarized. For example, when the observation set is exchangeable (as is x in our simple model), common choices for the \code SummaryNetwork include pooling-based encoders ( Zaheer et al. 2018 , e.g., \code DeepSet;) , and attention-based set-encoders such as the \code SetTransformer ( Lee et al. 2019 ) . These options trade computational cost for expressivity: pooling-based encoders are usually cheaper, whereas attention-based encoders may recover more complex inter-sample interactions at higher memory and compute cost. As another example, when the observations form a time-series, temporal neural networks, \code TimeSeriesNetwork ( Zhang and Mikelsons 2023 , LSTN;) or \code TimeSeriesTransformer ( Wen et al. 2022 ) are appropriate choices. A \code wrappers module allows for the incorporation or arbitrary summary networks written in a custom backend ( Gu and Dao 2024 , e.g., Mamba,) . The \code InferenceNetwork implements a conditional density estimator that (at minimum) allows sampling from the target distribution. Density evaluation is also supported, depending on the choice of architecture (see Table 1 ). Usually, inference networks such as \code FlowMatching ( Lipman et al. 2023 ; Tong et al. 2024 ) are based on some sub-network architecture, such as a Multi-Layer Perceptrons (MLP), or a Convolutional Neural Network (CNN), that performs the actual feed-forward pass, while the wrapping \code InferenceNetwork implements the specific loss function, sampling process, and optionally density evaluation. Appropriate choices for the \code InferenceNetwork depend strongly on the application. Usually, more expressive architectures, such as multi-step \code DiffusionModel ( Ho et al. 2020 ; Rombach et al. 2022 ; Song et al. 2020 ; Kingma and Gao 2023 ; Arruda et al. 2025 ) , few-step \code ConsistencyModel ( Song et al. 2023 ; Song and Dhariwal 2023 ; Schmitt et al. 2024b ) or (optimal transport) \code FlowMatching trade sampling speed for accuracy. Single-step models such as \code CouplingFlows ( Dinh et al. 2015 ; Dinh et al. 2017 ) allow for faster inference, but may struggle with dimensionality and multi-modality. In general, larger sub-networks can improve the expressivity of any \code InferenceNetwork, within the corresponding limits of the method. Finally, free-form models (e.g., diffusion, flow matching) can implement arbitrary sub-nets that absorb the summary network inside their architectures. To provide a concrete code example, we can define a pair of a \code SetTransformer summary network and a \code FlowMatching inference network as follows: {Code} summary_network = bf.networks.SetTransformer(summary_dim=8) inference_network = bf.networks.FlowMatching() The \code summary_dim argument of \code SummaryNetworks is of particular importance since it defines the number of learned summaries to be extracted. As a rule of thumb, we recommend \code summary_dim to be at least twice or even four times as large as the combined dimensionality of the inference variables. For our example, we have \mu and \sigma as inference variables – both of which are scalar, so their combined dimensionality is 2 . Choosing \code summary_dim=8 should be a safe choice here. 3.4 Approximators The \code Approximator class combines the target of inference, given by \code Simulator and \code Adapter, and the method of inference, given by the \code Networks. Within an \code Approximator, neural networks can be trained on the simulated data to subsequently perform inference on any new data. In most applications, all of our inference variables are continuous, so will use the \code ContinuousApproximator subclass. There are also other approximator types, such as the \code ScoringRuleApproximator for learning arbitrary Bayes estimators (e.g., for posterior point estimation), \code RatioApproximator for learning likelihood-to-evidence ratios, and \code GraphicalApproximator for learning posteriors of models with complex probabilistic factorizations, such as multilevel and mixture models. 3.4.1 Training of Approximators We initialize an approximator by supplying it with the previously defined \code Networks and the \code Adapter: {Code} approximator = bf.approximators.ContinuousApproximator( inference_network=inference_network, summary_network=summary_network, adapter=adapter, ) After initialization, the \code approximator needs to be compiled with an appropriate optimizer. Since \pkg BayesFlow is built on \pkg Keras3, we use \code keras optimizers for this purpose. For example: {Code} import keras optimizer = keras.optimizers.AdamW(learning_rate=1e-4) approximator.compile(optimizer=optimizer) After compilation, we are ready to train the neural networks via the \code approximator.fit method, which requires details on the training procedure, including the number of epochs (argument \code epochs), number of batches per epoch ( \code num_batches) and the batch size ( \code batch_size; number of simulations per batch). These are all standard arguments in deep learning pipelines. Generally speaking, there are two types of training procedures: online training and offline training. Passing a \code Simulator to the \code simulator argument of \code approximator.fit implies online training, where simulated training data is generated on-the-fly by repeatedly calling the simulator: {Code} approximator.fit( simulator=simulator, epochs=50, num_batches=100, batch_size=64 ) The arguments \code epochs, \code num_batches, and \code batch_size are exemplarily chosen here and do not necessarily reflect good defaults. When we provide simulation data directly to \code approximator.fit, this implies offline training. For this purpose, we first define an \code OfflineDataset object, which hosts the simulated training data and knows how to split them into batches during each epoch. Then, we pass the data to the \code dataset argument of \code approximator.fit: {Code} sims = simulator.sample(1024) data = bf.OfflineDataset(sims, batch_size=64, adapter=adapter) approximator.fit(dataset=data, epochs=50) Offline training is relevant for cases where the training data was pre-simulated. It directly stores the simulated data as a dictionary of variables rather than the simulator object. Using offline training is often preferable when the simulator is slow or cannot be easily executed within \proglang Python. Moreover, training tends to be faster with offline training even for fast simulators since training data can be pre-loaded into the GPU for more efficient neural network training. However, since the pre-simulated training data is fixed and finite, each simulation will be seen multiple times during neural network training (more precisely: once per epoch). As such, an offline training strategy is susceptible to overfitting. To already see potential overfitting during training, we can pass other simulated data to the \code validation_data argument of \code approximator.fit: {Code} val_sims = simulator.sample(128) val_data = bf.OfflineDataset(val_sims, batch_size=64, adapter=adapter) approximator.fit(dataset=data, validation_data=val_data, epochs=50) As is customary in deep learning, the loss on the validation data is evaluated and shown in the history but not used to train the neural networks. In contrast to offline training, online training is not susceptible to overfitting because each simulation is seen only once during training (hence no need for validation data). 3.4.2 Inference with Approximators Once trained, we can ask the approximator to generate samples from the target distribution or evaluate the target’s log-density. For our example, the target is the posterior p(\mu,\sigma\mid x) of parameters \mu and \sigma given data x . Since, after training the neural networks, inference is amortized, we can get near instant posterior samples for multiple new (simulated or real-world) datasets at once: {Code} test_sims = simulator.sample(10) samples = approximator.sample(conditions=test_sims, num_samples=1000) The \code samples object is a dictionary with names according to the inference variables (here \code "mu" and \code "sigma"). Each element is a \pkg numpy array of shape \code (n_datasets, n_samples, len_parameter). Accordingly, for our example, the samples of both \code "mu" and \code "sigma" have shape \code (10, 1000, 1), since we evaluated 1000 samples for each of 10 simulated datasets, and our parameters are both scalar ( \code len_parameter = 1). In addition to sampling, we can also evaluate the log-density of the target distribution at given values of both inference variables and conditions. For example: {Code} log_densities = approximator.log_prob(data=test_sims) The \code data argument of \code approximator.log_prob needs to contain both the conditions of the target distribution (here x ) and the values of the inference variables ( \mu and \sigma ) at which the density should be evaluated. This stands in contrast to \code approximator.sample whose \code conditions argument only requires the conditions of the target but no values of the inference variables themselves, since those are to be sampled. 3.5 Diagnostics Just because we have trained an approximator does not mean its approximation to the target distribution is necessarily a good one. In order to validate whether training has been successful, \pkg BayesFlow comes with a wide range of diagnostics all hosted within the \code Diagnostics module. Within this module, we distinguish between graphical diagnostics (submodule \code diagnostics.plots) and numerical diagnostics (submodule \code diagnostics.metrics). Usually the first graphical diagnostic investigated after training is the loss history: {Code} bf.diagnostics.plots.loss(approximator.history) Ideally, it should show the training loss (and validation loss if present) to reduce over the epochs and eventually converge at a low level (see Figure 4 in Section 4 for an example). Most other graphical diagnostics perform a comparison between the obtained samples from the (approximated) target distribution (argument \code estimates) and a simulated ground truth ( \code targets). For example, to perform graphical simulation-based calibration checking ( Modrák et al. 2023 ; Talts et al. 2020 , SBC;) , we can run: {Code} test_sims = simulator.sample(100) test_samples = approximator.sample(conditions=test_sims, num_samples=1000) bf.diagnostics.plots.calibration_ecdf( estimates=test_samples, targets=test_sims, ) Most numerical diagnostics follow the same pattern. For example, to compute the test statistic corresponding to the above calibration ECDF plot ( Säilynoja et al. 2022 ) , we can use: {Code} bf.diagnostics.metrics.calibration_log_gamma( estimates=test_samples, targets=test_sims, ) More diagnostics and further arguments to tailor them are discussed in Section 4 . 3.6 Workflows As an alternative to the lower-level \code Approximator module, \pkg BayesFlow offers the high-level \code Workflow interface, from which all steps in the training and inference phase can conveniently be called from. First, we gather all main objects—the \code Simulator (optional), the \code Adapter (optional), and the \code Networks (optional summary network)—into one object: {Code} workflow = bf.BasicWorkflow( simulator=simulator, adapter=adapter, inference_network=inference_network, summary_network=summary_network, ) We can then train the workflow with a single line of code, for example, using online training: {Code} workflow.fit_online(epochs=50, batch_size=64, num_batches_per_epoch=100) Subsequently, we can run many diagnostics at once to obtain a good overview of the obtained approximations: {Code} test_data = simulator.sample(100) workflow.plot_default_diagnostics(test_data=test_data) workflow.compute_default_diagnostics(test_data=test_data) To perform actual inference on new data, \code workflow.sample and \code workflow.log_prob continue to work in the same way as for the \code approximator. More generally, the \code workflow object still allows to access all lower level elements and methods of its components, such that its increase in convenience does not come with a relevant loss in flexibility or expressiveness. In our own applications, we prefer using workflows over their lower level alternatives. 4 Case Study: Lotka-Volterra Dynamics To demonstrate \pkg BayesFlow’s end-to-end workflow with a more complex scenario than outlined in Section 3 , we present a case study in this section using the Lotka-Volterra system ( Lotka 2002 ; Wangersky 1978 ; Bunin 2017 ) as observation model. For clarity and ease of understanding, we only highlight the most important code segments and outputs. A fully reproducible tutorial for this case study can be found in the following repository: https://github.com/bayesflow-org/bf2-paper-case-study . 4.1 Preliminary: Choosing a Backend Before we start using \pkg BayesFlow, we need to commit to one of the deep learning backends that it supports. In general, the choice of backend depends on several factors, most importantly, the available hardware and speed requirements, as well as compatibility with already existing code, and external tools used in the workflow. Once installed, \pkg BayesFlow will attempt to automatically detect and use whichever backend the user has installed. This is indicated as follows: {Code} import bayesflow as bf > INFO:bayesflow:Using backend ’torch’ To use this automatic detection, \pkg BayesFlow must be imported before \pkg Keras. We can also override this behavior by setting the \code KERAS_BACKEND environment variable before importing \pkg BayesFlow, for instance by using the python built-in \code os module. This is particularly useful when multiple backends are installed in the same environment. {Code} import os os.environ["KERAS_BACKEND"] = "tensorflow" import bayesflow as bf > INFO:bayesflow:Using backend ’tensorflow’ In this case-study, we choose \pkg JAX for its fast performance, optimal reproducibility, and functional purity. We set this backend explicitly, and also ensure reproducibility via determinism with the \code TF_CUDNN_DETERMINISTIC environment variable: {Code} import os os.environ["KERAS_BACKEND"] = "jax" os.environ["TF_CUDNN_DETERMINISTIC"] = "1" 4.2 Observation Model Definition As we suggested in Figure 1 , defining an observation model involves 1) sampling the governing parameters \theta from their specified prior distributions, 2) sampling some measurement noise \xi , and 3) generating observables from an observation model \mathcal{D}=\operatorname{Sim}(\theta,\xi) . The Lotka-Volterra (LV) system ( Lotka 2002 ; Wangersky 1978 ; Bunin 2017 ) presents a classic example in computational ecology that models the predator-prey population dynamics between two species. Formally, the LV system is defined as a set of two nonlinear ODEs: \displaystyle{\frac{\mathrm{d}x}{\mathrm{d}t}} \displaystyle=\alpha x-\beta xy, (16) \displaystyle{\frac{\mathrm{d}y}{\mathrm{d}t}} \displaystyle=-\gamma y+\delta xy, (17) where x is the prey population, y is the predator population, \alpha,\beta are the rates of increase and decay for the prey population, while \gamma,\delta are the rates of increase and decay for the predator population, respectively. Together, \theta=(\alpha,\beta,\gamma,\delta) are the governing parameters of the LV system equations. While we can directly measure the population time series using these equations, we cannot directly measure the governing parameters. In its original form, we can implement the LV system equations as follows: {Code} def lotka_volterra_equations(state, t, alpha, beta, gamma, delta): x, y = state dxdt = alpha * x - beta * x * y dydt = - gamma * y + delta * x * y return [dxdt, dydt] However, we still need to make some assumptions about the LV system. First, we need to provide an initial state for the two species. Second, we need to specify a time horizon for the simulation as a whole. Combining these two assumptions, we can create a full implementation of the LV system by solving the ODE using \code scipy.integrate.odeint: {Code} def ecology_model( alpha, beta, gamma, delta, t_span=[0, 5], t_steps=100, initial_state=[1, 1] ): t = np.linspace(t_span[0], t_span[1], t_steps) state = odeint(lotka_volterra_equations, initial_state, t, args=(alpha, beta, gamma, delta)) x, y = state.T return dict(x=x, y=y, t=t) Furthermore, we would also assume that the observation model is imperfect. In practice, we can reflect this assumption by 1) introducing measurement noise to the ideal observation \code x, y, and \code t, and 2) including counting error by choosing a subset of time steps to observe. We introduce these changes after the ideal simulation to ensure realistic simulation outputs. {Code} def observation_model(x, y, t, subsample=10, obs_prob=1.0, noise_scale=0.1): t_steps = x.shape[0] # Add noise to observations and determine the observed time steps # … return "observed_x": noisy_x[observed_indices], "observed_y": noisy_y[observed_indices], "observed_t": t[observed_indices] To complete the definition of our observation model, we sample the priors from a scaled logit-normal distribution: {Code} def prior(): x = rng.normal(size=4) theta = 1 / (1 + np.exp(-x)) * 3.9 + 0.1 return dict(alpha=theta[0], beta=theta[1], gamma=theta[2], delta=theta[3]) We can easily inspect the sampled priors and the simulated observables by calling \code priors() and \code observation_model(**ecology_model(**prior()). 4.3 Simulator As mentioned in Section 3.1 , we can use the \code bf.make_simulator() function to connect all components of our implementation into a single simulator: {Code} simulator = bf.make_simulator([prior, ecology_model, observation_model]) We can then use \code simulator.sample() to run multiple simulations. This includes the sets of parameters \{\theta\} as well as the simulated ideal ( \{x,y,t\} ) and observed ( \{x_{\operatorname{obs}},y_{\operatorname{obs}},t_{\operatorname{obs}}\} ) time series. There are many steps we can take to check our model prior to training, and we recommend following principled prior predictive checks ( Gelman et al. 2020 ) . For brevity, we visualize a few the trajectories to observe if the specified priors lead to expected behavior in the observation model. For the LV system, we expect a general oscillating trend for each simulated trajectory pair, differing only by the sampled parameters from the priors (see Figure 3 ). Figure 3: Simulated trajectories aggregates from the LV model with sampled priors. 4.4 End-to-End Posterior Estimation With Raw Data The most common use of \pkg BayesFlow is neural posterior estimation. In this workflow, the dataset generated from the simulator is considered for training the neural approximator, where the parameters’ posterior is learned from the simulation outputs. We can easily set up this workflow via the \code BasicWorkflow object exactly as outlined in Figure 2 , where we define the \code simulator, the \code adapter, the \code summary_network, and the \code inference_network. 4.4.1 Creating the Adapter As discussed in Section 3.2 , simulation outputs can be “curated” into neural network-friendly format via a custom \code Adapter object that bundles together multiple stateless transforms: {Code} adapter = ( bf.adapters.Adapter() .convert_dtype("float64", "float32") .drop(["x", "y", "t"]) .as_time_series(["observed_x", "observed_y", "observed_t"]) .concatenate(["alpha", "beta", "gamma", "delta"], into="inference_variables") .concatenate(["observed_x", "observed_y", "observed_t"], into="summary_variables") ) We can inspect the order of transformations by printing the \code adapter. The list below explains the transforms we include here: • The \code convert_dtype transform unifies the data type of all simulated data to single-precision floats, the most commonly used precision for deep learning. • The \code drop transform removes the unused observables and parameters from the dataset, so that they are not included during training and inference. • The \code as_time_series transform ensures that variables are correctly identified as a time-series by corresponding summary networks. Internally, this just adds a trailing dimension of length 1, such that the now second-to-last dimension is the time-series dimension. • the \code concatenate transform gathers specific observables or parameters together into a single \pkg NumPy array along the last dimension (by default). Concatenation groups the time series as \code summary_variables to become the input to the \code summary_network; while grouping the model parameters as \code inference_variables to become the variables whose posterior distribution is learned by the \code inference_network. 4.4.2 Defining the Networks Next, we can assemble the neural approximator by defining a \code summary_network and an \code inference_network. Since the observable trajectories are all time series, \pkg BayesFlow’s off-the-shelf \code TimeSeriesNetwork can be used to compress them into fixed-length summary statistics. The choice of \code inference_network is rather free, yet similarly straightforward. Here, we choose the \code FlowMatching network for its cost-effectiveness. {Code} time_series_network = bf.networks.TimeSeriesNetwork(summary_dim=32) flow_matching = bf.networks.FlowMatching() 4.4.3 Offline Training With the Basic Workflow We gather all the ingredients required for training and inference in a \code BasicWorkflow object, as detailed in Section 3.6 : {Code} workflow = bf.BasicWorkflow( simulator=simulator, adapter=adapter, summary_network=time_series_network, inference_network=flow_matching ) Subsequently, we train the neural approximator with an offline dataset consisting of 5000 simulations: {Code} training_data = workflow.simulate(5000) history = workflow.fit_offline( data=training_data, epochs=50, batch_size=32, validation_data=validation_data ) In our case, given our simulation budget, training the neural approximator should be very fast (within the scale of minutes), regardless of whether a GPU is used. 4.4.4 Automatic Diagnostics After the neural approximator is trained, we can perform model validation by probing its computational faithfulness and model sensitivity. The \code plot_default_diagnostics() method combines several visualizations of model calibration and parameter recovery, along with the loss trajectory for convergence assurance (see Figure 4 ): {Code} figures = workflow.plot_default_diagnostics( test_data=validation_data, variable_names=[r" \alpha ", r" \beta ", r" \gamma ", r" \delta "], loss_kwargs=…, recovery_kwargs=…, calibration_ecdf_kwargs=… ) Figure 4: Automatically generated visual diagnostics from the \code BasicWorkflow object. From top to bottom : training and validation loss, calibration ECDF, parameter recovery, and posterior z -score contraction. The results indicate that the neural network training has converged, marginal posteriors are well calibrated, and parameter recovery as well as posterior contraction is high. Together, this implies that the our neural ABI procedure has been successful in accurately approximating the posteriors implied by the LV model, at least for the given validation data. The diagnostics shown in Figure 4 are summarizing the posteriors obtained for many datasets. We can also investigate the posteriors of individual datasets more closely, for example, via: {Code} estimates = workflow.sample(num_samples=300, conditions=validation_data) f = bf.diagnostics.plots.pairs_posterior( estimates=estimates, targets=validation_data, dataset_id=0 ) The \code dataset_id argument indicates which sampled dataset (and corresponding posterior) should be displayed in the plot (see Figure 5 ). Figure 5: Estimated posterior distribution of the LV model parameters for a single dataset, showing contraction towards the estimation targets compared to the priors. In addition to graphical diagnostics, \pkg BayesFlow also comes with several numerical diagnostics, such as normalized root mean square error (NRMSE), posterior contraction, and expected calibration error. These diagnostics are readily accessible via the \code compute_default_diagnostics() method (see Table 2 ): {Code} workflow.compute_default_diagnostics(test_data=validation_data) Table 2: Diagnostic metrics for posterior estimation. Metrics \alpha \beta \gamma \delta NRMSE 0.110 0.106 0.100 0.103 Log Gamma 1.334 2.036 0.634 0.986 Calibration Error 0.037 0.040 0.033 0.035 Posterior Contraction 0.944 0.926 0.943 0.941 4.4.5 Posterior Predictive Checks Figure 6: Posterior predictive check of prey-predator population trajectories implied by the LV model for a single validation dataset. As a final step, we can use the posterior samples from the neural approximator to resimulate data from the LV model and compare it to the observed data. The goal of such posterior predictive checks is usually to assess how appropriate the statistical model (as encoded by the simulator) is for the given data ( Gabry et al. 2019 ) . In our case, since the validation data has been generated from the model, we know the latter is appropriate by definition. Instead, when applied to validation data, we can use posterior predictive checks as another piece of evidence that the neural approximator can indeed approximate the analytic posteriors of the model with sufficient accuracy. As shown in Figure 6 , the posterior-implied trajectories match the observed data well and are much more concentrated than the prior-implied trajectories (compare with Figure 3 ). The model is also capable of predicting trajectories for time horizons going beyond the observed data, with increasing uncertainty as predictions are made further into the future; as would be expected for an appropriately calibrated model and corresponding posterior. 4.5 Point Estimation with Summary Statistics In some cases, full posterior estimation may not be feasible, for example, due to large-scale or slow simulator output that limits the simulation budget. In these cases, posterior point estimation becomes a viable alternative to estimating the full posterior. What is more, for the LV model, domain experts have developed summary statistics that can be used to our advantage and accelerate the workflow. Both point estimator and fixed summary statistics allow us to simplify the neural approximator and significantly increase the computational efficiency of our workflow. Here, we demonstrate \pkg BayesFlow’s ability to handle these cases. 4.5.1 Expert Summary Statistics As shown in Section 4.4.5 , the observed posterior predictive trajectory exhibits characteristics like periodicity, lag, and some degrees of synchronicity (correlations) when compared between predators and prey populations. Therefore, one can extract a collection of summary statistics around these observations with a simple function. {Code} def expert_stats(observed_x, observed_y, lags=[2,5]): means = np.array([observed_x.mean(), observed_y.mean()]) … return dict( means=means, log_vars=log_vars, auto_corrs=auto_corrs, cross_corr=cross_corr, period=T ) We can include these summary statistics in \pkg BayesFlow’s \code make_simulator() function. {Code} expert_simulator = bf.make_simulator( [prior, ecology_model, observation_model, expert_stats] ) Resimulation with \code expert_simulator allows us to include these summary statistics as data for the neural approximator. {Code} samples_with_expert_stats = simulator.sample(3) keras.tree.map_structure(keras.ops.shape, samples_with_expert_stats) ’alpha’: (3, 1), … ’means’: (3, 2), ’log_vars’: (3, 2), ’auto_corrs’: (3, 4), ’cross_corr’: (3, 1), ’period’: (3, 1) 4.5.2 Refining the Adapter Since we computed the summary statistics already within the simulator, we can directly label them in the \code Adapter as \code inference_conditions. Naturally, this eliminates the need to use the full trajectories as \code summary_variables. {Code} expert_adapter = ( bf.adapters.Adapter() … .concatenate(["means", "log_vars", "auto_corrs", "cross_corr", "period"], into="inference_conditions") ) 4.5.3 Simplifying the Networks Since our summary statistics are now precomputed, we no longer need a \code summary_network in our workflow. For posterior point estimation, we would need an inference network capable of drawing inference from some strictly proper scoring rules ( Gneiting and Raftery 2007 ) . \pkg BayesFlow’s \code PointInferenceNetwork allows us to draw inference from mean and quantiles, among others, with two such scoring rules: \code MeanScore() and \code QuantileScore(): {Code} quantile_levels = np.linspace(0.1, 0.9, 5) point_inference_network = bf.networks.PointInferenceNetwork( scores=dict( mean=bf.scores.MeanScore(), quantiles=bf.scores.QuantileScore(quantile_levels) ) ) We can then create a new \code BasicWorkflow that utilizes the \code point_inference_network: {Code} workflow = bf.BasicWorkflow( simulator=expert_simulator, adapter=expert_adapter, inference_network=point_inference_network ) 4.5.4 Online Training Instead of offline training, \pkg BayesFlow’s \code BasicWorkflow object also supports online training via the \code fit_online() method. Instead of computing the training steps using the simulation budget for the training dataset, online training requires the user to specify the number of batches per epoch as the number of training steps. {Code} history = workflow.fit_online( epochs=50, num_batches_per_epoch=200, batch_size=32, ) One of the benefits of online training is that, since training data is provided on-the-fly via simulation, it minimizes the possibility of overfitting to the pre-generated training dataset. However, the tradeoff is time. Compared to offline training, where simulation time is limited by the size of the training and validation datasets, online training requires training data to be generated per step, which adds to the total training time. 4.5.5 Manual Diagnostics Since this workflow involves point estimation with customized quantile statistics, automatic diagnostics do not apply. Nevertheless, \pkg BayesFlow has a diagnostic toolkit available for point estimates, which we can manually extend via customized metrics. As before, the loss function can be inspected via \code bf.diagnostics.loss(). We can obtain estimates of posterior mean and quantiles for all model parameters. {Code} validation_data = expert_simulator.sample(300) point_estimates = workflow.estimate(conditions=validation_data) keras.tree.map_structure(keras.ops.shape, point_estimates) ’alpha’: ’mean’: (300, 1), ’quantiles’: (300, 5, 1), ’beta’: … From here, we can apply visual diagnostics that indicate the neural approximator’s accuracy and calibration just using the posterior mean and quantile estimates: {Code} point_recovery = bf.diagnostics.plots.recovery_from_estimates( estimates=point_estimates, targets=validation_data, variable_names=variable_names, ) {Code} f = bf.diagnostics.plots.calibration_ecdf_from_quantiles( estimates=point_estimates, targets=validation_data, quantile_levels=quantile_levels, difference=True, variable_names=variable_names, ) Figure 7: Manual diagnostics for the point estimation, including parameter recovery ( top ) and calibration ECDF ( bottom ). At first glance, visual diagnostics are similar to those for the full posterior estimation (see Figure 7 ). However, the key difference here is that the diagnostics can only show what information is provided by the point estimates. The most evident example of this is the calibration diagnostic, which is only evaluated at the five estimated quantiles. Another difference, not visible in the above plots, is that the per-parameter point estimates do not contain any information on the dependency between parameters. Said dependencies would be required, for example, to obtain complete posterior pairs plots. 5 Comparison Between Packages Several software packages target amortized Bayesian inference. A comprehensive comparison of all existing approaches would be beyond the scope of this paper and of limited practical value. We therefore restrict attention to a small set of direct competitors that are actively maintained, widely used, and provide state-of-the-art inference methods. An overview of the comparisons can be found in Table 3 . In particular, we focus on \pkg sbi ( Boelts et al. 2024 ) and \pkg NeuralEstimators.jl ( Sainsbury-Dale et al. 2024 ) , which are conceptually closest to \pkg BayesFlow in that they support related classes of neural estimators, including posterior, likelihood, and ratio estimators. Other packages, such as \pkg sbijax ( Dirmeier et al. 2024 ) , \pkg lampe ( Rozet et al. 2021 ) , \pkg pydelfi ( Alsing et al. 2019 ) or \pkg carl ( Louppe et al. 2016 ) , either target a more limited subset of SBI methods, are at an early stage of development, or have not seen sustained development in recent years. Further tools, including \pkg madminer ( Brehmer et al. 2020 ) and \pkg swyft ( Miller et al. 2022a ) , are more specialized and emphasize specific estimator types or application domains rather than offering a general-purpose framework for SBI. Currently, \pkg BayesFlow is the only framework that natively supports multiple deep-learning backends. This capability arises from a backend-agnostic implementation on top of \pkg Keras 3, which allows users to run models using \pkg PyTorch, \pkg JAX, or \pkg TensorFlow backends. By contrast, \pkg sbi is based on \pkg PyTorch, which may be a friction point for users committed to \pkg TensorFlow or \pkg JAX ecosystems. Moreover, effective compilation of \pkg PyTorch models is still an ongoing effort and can incur overhead due to graph breaks or compatibility issues from lack of operation support. As such, in practice, large \pkg PyTorch models often remain uncompiled, rendering them much slower especially compared to models written in \pkg JAX. The \pkg NeuralEstimators.jl library targets the \proglang Julia ecosystem using \pkg Flux as deep-learning backend. Like \pkg BayesFlow, \pkg sbi and \pkg NeuralEstimators.jl support sequential (i.e., flat) statistical models. \pkg BayesFlow is the only package of the three to additionally support hierarchical models with arbitrarily nested and crossed probabilistic factorizations as well as varying number of parameters. Additionally, \pkg sbi and \pkg NeuralEstimators.jl do not provide support for unbatched or named-parameter simulators, both of which are core usability aspects of \pkg BayesFlow. In \pkg sbi, users are encouraged to run simulations directly in \pkg PyTorch in batched form, whereas \pkg BayesFlow encourages using \pkg numpy and accepts both batched and unbatched outputs, automatically batching the latter for efficiency. All three packages implement the core SBI estimators — neural posterior, neural likelihood, and neural ratio estimation. \pkg BayesFlow additionally implements a workflow for model comparison. \pkg BayesFlow and \pkg NeuralEstimators.jl also offer point estimators of posterior and likelihood, enabling rapid prototyping where a point estimate is sufficient before full generative uncertainty quantification is required. All three packages implement normalizing flows: \pkg sbi provides masked autoregressive flows ( Papamakarios et al. 2017 ) , neural spline flows (NSFs) ( Durkan et al. 2019 ) and allows using flows implemented in \pkg zuko ( Zuko Development Team 2025 ) and \pkg nflows ( Durkan et al. 2020a ) , BayesFlow ships with affine coupling flows ( Dinh et al. 2017 ) and NSFs, whereas \pkg NeuralEstimators.jl provides only ACFs. Additionally, \pkg BayesFlow allows for flexible latent spaces, for instance, Student-t for enhanced robustness or mixture distributions for enhanced expressivity ( Papamakarios et al. 2021 ) . Furthermore, \pkg BayesFlow implements all modern free-form generative networks, including various flavors of flow matching (e.g., optimal transport, conditional optimal transport), diffusion models (compatible with any schedule, guidance, and composition), and many other recent model families ( Arruda et al. 2025 ) , such as few-step (discrete and continuous) consistency models ( Schmitt et al. 2024b ) and free-form flows ( Draxler et al. 2024 ) . Notably, all of these architectures can be used for both posterior sampling and likelihood estimation. In contrast, \pkg sbi supports diffusion models and flow-matching only for posterior estimation and \pkg NeuralEstimators.jl does not offer generative free-form architectures. Although all three packages support bundling ensembles of trained networks, only \pkg BayesFlow allows bundling multiple untrained networks and training them jointly in parallel. Treating the ensemble as a single composite network not only enables convenient (de)serialization into and from a single file, but also helps maximizing hardware utilization during training, which can lead to substantial speedups over sequential training of ensemble members. \pkg BayesFlow further allows users to choose from an array of training schemes, most notably online training, where the simulator is called on-the-fly, generating new samples as the training progresses, as well as offline training, where all simulations are pre-computed and stored either in-memory or on-disk. Similarly, \pkg NeuralEstimators.jl conveniently allows users to pass a sampler as a dataset, enabling on-the-fly training, but leaves it to users to implement their own custom on-disk dataset. While \pkg sbi allows offline training using pre-computed simulations stored in CPU memory, it does not support storing them on-disk or creating them on-the-fly, which makes it necessary to write a custom training loop to scale to large datasets or exploit highly efficient simulators (e.g., when prototyping). Finally, all packages provide diagnostics to assess the performance of trained neural estimators. \pkg sbi and \pkg BayesFlow both provide a comprehensive suite of utilities, metrics, and plots to carry out recovery and simulation-based calibration (SBC) checks, including optional custom test quantities ( Modrák et al. 2023 ) , as well as model misspecification detection ( Schmitt et al. 2023 ) and sample-based comparison with reference distributions, like C2ST ( Lopez-Paz and Oquab 2018 ) and MMD ( Gretton et al. 2012 ) . \pkg NeuralEstimators.jl has so far focused on assessing the Bayes risk and frequentist coverage of estimates. Table 3: Feature Comparison of Simulation-Based Inference Software \pkg BayesFlow \pkg sbi \pkg NeuralEstimators.jl Supported Deep Learning Frameworks TensorFlow ✓ ✗ ✗ PyTorch ✓ ✓ ✗ JAX ✓ ✗ ✗ Flux ✗ ✗ ✓ Supported Model Types Sequential (flat) models ✓ ✓ ✓ Hierarchical models ✓ ✗ ✗ Mixture models ✓ ✓ ✗ Multimodal models ✓ (✓) (✓) Supported Neural Estimators Posterior estimation ✓ ✓ ✓ Likelihood estimation ✓ ✓ ✓ Ratio estimation ✓ ✓ ✓ Scoring rule estimation ✓ ✗ ✓ Implemented Density Estimators Mixture of Gaussians ✓ ✓ ✓ Normalizing flows ✓ ✓ ✓ Diffusion models ✓ (✓) ✗ Flow matching ✓ (✓) ✗ Free-form flows ✓ ✗ ✗ Consistency models ✓ ✗ ✗ Continuous consistency models ✓ ✗ ✗ Ensembles ✓ (✓) (✓) Supported Bayesian Tasks Parameter estimation ✓ ✓ ✓ Model comparison ✓ ✗ ✗ Model misspecification detection ✓ ✓ ✗ ✓ Supported (✓) Partially Supported ✗ Not Supported 6 Conclusion In this work, we presented \pkg BayesFlow Version 2.0, a backend-agnostic and feature-rich framework for scalable ABI workflows. We demonstrate \pkg BayesFlow’s intuitive and comprehensive API by outlining the software’s core components and applying the framework to a representative case study. By natively integrating recent state-of-the-art deep learning architectures such as various falvors of diffusion models and flow matching, \pkg BayesFlow enables rapid, flexible, end-to-end Bayesian inference across a broad range of tasks. We further showcased \pkg BayesFlow’s \code diagnostics module, which enables comprehensive inspection of trained estimators to ensure estimation quality through numerical and visual diagnostics. \pkg BayesFlow provides a streamlined interface that closely reflects established ABI terminology, carefully aligning naming conventions from statistics and ABI with those used in modern machine learning. The roles of the simulator, neural estimator, and diagnostic utilities are clearly delineated, providing users with a well-structured workflow for generative model specification, data pre-processing, training, and inference. By reducing conceptual overhead and lowering the entry barrier, we argue that \pkg BayesFlow’s new design extends its appeal to a broader community of scientists who rely on simulation in their research. Ultimately, the streamlined interface establishes a clear conceptual foundation that supports accessibility for new users and preserves flexibility for advanced practitioners. For the future, we have several plans on how to further improve the functionality of \pkg BayesFlow. The field of generative modeling is quickly changing, with new architectures and architecture improvements being developed constantly. We will continue to implement them in \pkg BayesFlow as they become available and reveal promising results for ABI. This will also include modular architecture combinations that mirror probabilistic symmetries of the approximated statistical model ( Habermann et al. 2024 ; Kucharskỳ and Bürkner 2025 ; Elsemüller et al. 2024b ) as well as loss functions specific to ABI that increase its accuracy and robustness ( Schmitt et al. 2024a ; Mishra et al. 2025 ; Elsemüller et al. 2025 ) . Further, we will implement methods for post hoc correcting ABI results during inference with minimal computational effort, such as advanced importance sampling approaches ( Vehtari et al. 2024 ; Li et al. 2024 ) . Moreover, we will work on building \pkg BayesFlow interfaces with common probabilistic programming languages such as \proglang Stan ( Stan Development Team 2025 ) or \pkg PyMC ( Abril-Pla et al. 2023 ) . This would strongly increase the potential user base of \pkg BayesFlow and overall simplify specification of simulators and corresponding log densities. Finally, we will continue to implement modern training methods that work out-of-the-box, such as automatic low-precision pre-training, to support efficient and flexible scaling to the largest Bayesian problems.