stefanradev93 / BayesFlow

A Python library for amortized Bayesian workflows using generative neural networks.
https://bayesflow.org/
MIT License
286 stars 45 forks source link

Keras 3: Streamlined Backend #159

Open LarsKue opened 3 months ago

LarsKue commented 3 months ago

Work In Progress

This PR is still in progress. Please discuss open issues and raise possible concerns below.

Summary

batch_of_samples = prior.sample((32,)) assert batch_of_samples["theta"].shape == (32, 2)

- `Amortizer`s are now Keras 3 Models, which allows backend-agnostic training
```py3
# set backend to "tensorflow", "jax", or "torch" before importing bayesflow
import os
os.environ["KERAS_BACKEND"] = "torch"

import bayesflow as bf

amortizer = bf.AmortizedPosterior(...)
amortizer.fit()

class MyDataset(keras.utils.PyDataset): ... # user-implemented data loading from disk

dataset = MyDataset(workers=12, use_multiprocessing=True) amortizer.fit(dataset)


# In Progress

- Allow the user to specify what variables are observed vs. inferred vs. conditioned on by name
```py3
# two moons
posterior = Amortizer(
    inference_network,
    observed_variables=["x1", "x2"],
    inferred_variables=["theta1", "theta2"],
    inference_conditions=["r", "alpha"]
)

Postponed

We should probably do these things after merging with dev and before merging with main:

In Discussion

Dropped

Reason: Too restrictive for data structure.

paul-buerkner commented 3 months ago

@LarsKue Thank you so much! This already looks amazing!

Could you perhaps add a simple fully runnable example here for people to get started playing around with it? It is kind of there above, but I think it would make things easier to have one chunk of example code to copy and edit from there.

Everyone, please try out the new interface and tell us what you think!

LarsKue commented 3 months ago

@paul-buerkner Yes, I am working on it. I hope I have one ready today.

review-notebook-app[bot] commented 3 months ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

marvinschmitt commented 3 months ago

Great work!! 👏

I'll write down some thoughts on the installation process. Those don't need any changes in the streamlined codebase but are just reminders for our future selves shortly before the release.

codecov-commenter commented 1 month ago

Welcome to Codecov :tada:

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered :open_umbrella: