Hello,
I am just getting started with BayesFlow, so apologies if I am missing something obvious from the examples. For any reasonably complex model, I am not sure how to keep track of the parameter indices when defining a simulator function. In the case below, there are only 5 parameters so it wouldn’t be so bad. But it seems error prone/not scalable to access each parameter its flattened index, i.e., theta[0], theta[1], etc.
Naively, I would like to write the functions like this:
import numpy as np
import bayesflow as bf
from scipy.stats import halfnorm
RNG = np.random.default_rng(1)
def prior_fun():
parameters = {
'b': RNG.standard_normal(size=3),
'sigma': halfnorm.rvs(scale=1, size=2, random_state=RNG)
}
return parameters
def simulator_fun(params, n_obs = 100):
x = RNG.normal(loc = params["b"][0], scale = params["sigma"][0], size = n_obs)
y = RNG.normal(loc = params["b"][1] + params["b"][2]*x, scale = params["sigma"][1], size = n_obs)
return (x, y)
But of course this does not work with BayesFlow as the output of the prior_fun cannot be a dict. Is there any way other way that I can access the params by name that is compatible with BayesFlow modules?
Thank you,
Erik
Hi Erik,
Welcome to the BayesFlow Forums, thanks for posting your question. I totally agree that named parameters are often handy and help write readable+maintainable code.
We’re currently working on a complete refactor of BayesFlow and the development version is already available here: GitHub - bayesflow-org/bayesflow at dev
In the new BayesFlow version, you can in fact name all your parameters! Here’s an example notebook: bayesflow/examples/TwoMoons_FlowMatching.ipynb at dev · bayesflow-org/bayesflow · GitHub
In addition to named variables throughout the pipeline, Bayesflow 2.0 has some other nice features that might pique your interest:
- You can choose your backend: PyToch, TensorFlow, or JAX
- Numerous performance and stability improvements
- First-class support for flow matching, which is very nice if super-fast inference speed isn’t critical for you (30% faster training but 50-100x slower sampling than normalizing flows, but very stable and expressive)
So if you’re just getting started with BayesFlow, you might consider using the new version right away
Best,
Marvin
1 Like
Hi Marvin,
Thanks so much for your reply! The dev version indeed makes things a lot nicer for setting up forward simulations. It appears that the diagnostic module is not yet implemented for Bayesflow 2.0. For my purposes the most important are the SBC diagnostics, and perhaps mmd hypothesis test. Do you have any suggestions while waiting for the full release?
Thanks again,
Erik