Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
688f22c
[no ci] notebook tests: increase timeout, fix platform/backend depend…
vpratz Apr 23, 2025
de30009
Enable use of summary networks with functional API again (#434)
vpratz Apr 23, 2025
0eefa69
[no ci] docs: add GitHub and Discourse links, reorder navbar
vpratz Apr 25, 2025
a97b5a2
[no ci] docs: acknowledge scikit-learn website
vpratz Apr 25, 2025
8ac8aa3
[no ci] docs: capitalize navigation headings
vpratz Apr 25, 2025
7ea287f
More tests (#437)
LarsKue Apr 25, 2025
42fa035
Automatically run slow tests when main is involved. (#438)
vpratz Apr 25, 2025
206d706
Update dispatch
stefanradev93 Apr 25, 2025
25f5c64
Update dispatching distributions
stefanradev93 Apr 25, 2025
f6a70b5
Improve workflow tests with multiple summary nets / approximators
stefanradev93 Apr 25, 2025
28652ac
Merge branch 'dev' of https://github.com/stefanradev93/BayesFlow into…
stefanradev93 Apr 25, 2025
7ce37cf
Fix zombie find_distribution import
stefanradev93 Apr 25, 2025
ea5a78d
Add readme entry [no ci]
stefanradev93 Apr 25, 2025
dc3cf81
Update README: NumFOCUS affiliation, awesome-abi list (#445)
marvinschmitt Apr 25, 2025
af38458
Merge branch 'dev' of https://github.com/stefanradev93/BayesFlow into…
stefanradev93 Apr 25, 2025
3b1c053
fix is_symbolic_tensor
LarsKue Apr 25, 2025
c638124
remove multiple batch sizes, remove multiple python version tests, re…
LarsKue Apr 25, 2025
de8e1cb
implement compile_from_config and get_compile_config (#442)
LarsKue Apr 25, 2025
16491be
Fix Optimal Transport for Compiled Contexts (#446)
LarsKue Apr 25, 2025
ec0ee2f
update dispatch tests for more coverage
LarsKue Apr 25, 2025
acf1c72
Update issue templates (#448)
LarsKue Apr 26, 2025
d24f5a3
Robustify kwargs passing inference networks, add class variables
stefanradev93 Apr 26, 2025
a43fd07
Merge branch 'dev' of https://github.com/stefanradev93/BayesFlow into…
stefanradev93 Apr 26, 2025
f67e443
Merge branch 'main' into dev
LarsKue Apr 26, 2025
463c0c7
fix convergence method to debug for non-log sinkhorn
LarsKue Apr 26, 2025
8f3739c
Bump optimal transport default to False
stefanradev93 Apr 26, 2025
40eccd4
use logging.info for backend selection instead of logging.debug
LarsKue Apr 26, 2025
8903089
fix model comparison approximator
LarsKue Apr 26, 2025
cbc86b8
improve docs and type hints
LarsKue Apr 26, 2025
77ddc5a
improve One-Sample T-Test Notebook:
LarsKue Apr 26, 2025
ad01171
remove backend print
LarsKue Apr 26, 2025
a742d9c
[skip ci] turn all single-quoted strings into double-quoted strings
LarsKue Apr 26, 2025
b450961
turn all single-quoted strings into double-quoted strings
LarsKue Apr 26, 2025
898a869
Merge remote-tracking branch 'origin/dev' into dev
LarsKue Apr 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: Bug report
about: Create a bug report to help us improve BayesFlow
title: "[BUG]"
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Minimal steps to reproduce the behavior:
1. Import '...'
2. Create network '....'
3. Call '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Traceback**
If you encounter an error, please provide a complete traceback to help explain your problem.

**Environment**
- OS: [e.g. Ubuntu]
- Python Version: [e.g. 3.11]
- Backend: [e.g. jax, tensorflow, pytorch]
- BayesFlow Version: [e.g. 2.0.2]

**Additional context**
Add any other context about the problem here.

**Minimality**
- [ ] I verify that my example is minimal, does not rely on third-party packages, and is most likely an issue in BayesFlow.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest a new feature to be implemented in BayesFlow
title: "[FEATURE]"
labels: feature
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
2 changes: 0 additions & 2 deletions .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ on:
branches:
- main
- dev
- update-workflows
push:
branches:
- main
- dev
- update-workflows

jobs:
check-code-style:
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.10", "3.11"]
python-version: ["3.10"] # we usually only need to test the oldest python version
backend: ["jax", "tensorflow", "torch"]

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -73,8 +73,11 @@ jobs:
pytest -x -m "not slow"

- name: Run Slow Tests
# run all slow tests only on manual trigger
if: github.event_name == 'workflow_dispatch'
# Run slow tests on manual trigger and pushes/PRs to main.
# Limit to one OS and Python version to save compute.
# Multiline if statements are weird, https://github.com/orgs/community/discussions/25641,
# but feel free to convert it.
if: ${{ ((github.event_name == 'workflow_dispatch') || (github.event_name == 'push' && github.ref_name == 'main') || (github.event_name == 'pull_request' && github.base_ref == 'main')) && ((matrix.os == 'windows-latest') && (matrix.python-version == '3.10')) }}
run: |
pytest -m "slow"

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
![Codecov](https://img.shields.io/codecov/c/github/bayesflow-org/bayesflow?style=for-the-badge&link=https%3A%2F%2Fapp.codecov.io%2Fgh%2Fbayesflow-org%2Fbayesflow%2Ftree%2Fmain)
[![DOI](https://img.shields.io/badge/DOI-10.21105%2Fjoss.05702-blue?style=for-the-badge)](https://doi.org/10.21105/joss.05702)
![PyPI - License](https://img.shields.io/pypi/l/bayesflow?style=for-the-badge)
![NumFOCUS Affiliated Project](https://img.shields.io/badge/NumFOCUS-Affiliated%20Project-orange?style=for-the-badge)

BayesFlow is a Python library for simulation-based **Amortized Bayesian Inference** with neural networks.
It provides users and researchers with:
Expand Down Expand Up @@ -225,8 +226,10 @@ You can find and install the old Bayesflow version via the `stable-legacy` branc

## Awesome Amortized Inference

If you are interested in a curated list of resources, including reviews, software, papers, and other resources related to amortized inference, feel free to explore our [community-driven list](https://github.com/bayesflow-org/awesome-amortized-inference).
If you are interested in a curated list of resources, including reviews, software, papers, and other resources related to amortized inference, feel free to explore our [community-driven list](https://github.com/bayesflow-org/awesome-amortized-inference). 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 Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) Projects 528702768 and 508399956. The project is further supported by Germany's Excellence Strategy -- EXC-2075 - 390740016 (Stuttgart Cluster of Excellence SimTech) and EXC-2181 - 390900948 (Heidelberg Cluster of Excellence STRUCTURES), the collaborative research cluster TRR 391 – 520388526, as well as the Informatics for Life initiative funded by the Klaus Tschira Foundation.

BayesFlow is a [NumFOCUS Affiliated Project](https://numfocus.org/sponsored-projects/affiliated-projects).
16 changes: 16 additions & 0 deletions bayesflow/approximators/continuous_approximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ def compile(

return super().compile(*args, **kwargs)

def compile_from_config(self, config):
self.compile(**deserialize(config))
if hasattr(self, "optimizer") and self.built:
# Create optimizer variables.
self.optimizer.build(self.trainable_variables)

def compute_metrics(
self,
inference_variables: Tensor,
Expand Down Expand Up @@ -213,6 +219,16 @@ def get_config(self):

return base_config | serialize(config)

def get_compile_config(self):
base_config = super().get_compile_config() or {}

config = {
"inference_metrics": self.inference_network._metrics,
"summary_metrics": self.summary_network._metrics if self.summary_network is not None else None,
}

return base_config | serialize(config)

def estimate(
self,
conditions: Mapping[str, np.ndarray],
Expand Down
16 changes: 16 additions & 0 deletions bayesflow/approximators/model_comparison_approximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def compile(

return super().compile(*args, **kwargs)

def compile_from_config(self, config):
self.compile(**deserialize(config))
if hasattr(self, "optimizer") and self.built:
# Create optimizer variables.
self.optimizer.build(self.trainable_variables)

def compute_metrics(
self,
*,
Expand Down Expand Up @@ -262,6 +268,16 @@ def get_config(self):

return base_config | serialize(config)

def get_compile_config(self):
base_config = super().get_compile_config() or {}

config = {
"classifier_metrics": self.classifier_network._metrics,
"summary_metrics": self.summary_network._metrics if self.summary_network is not None else None,
}

return base_config | serialize(config)

def predict(
self,
*,
Expand Down
2 changes: 0 additions & 2 deletions bayesflow/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from .diagonal_student_t import DiagonalStudentT
from .mixture import Mixture

from .find_distribution import find_distribution

from ..utils._docs import _add_imports_to_all

_add_imports_to_all(include_modules=[])
47 changes: 21 additions & 26 deletions bayesflow/distributions/diagonal_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

import keras
from keras import ops

from bayesflow.types import Shape, Tensor
from bayesflow.utils.decorators import allow_batch_size
Expand All @@ -19,7 +20,7 @@ def __init__(
self,
mean: int | float | np.ndarray | Tensor = 0.0,
std: int | float | np.ndarray | Tensor = 1.0,
use_learnable_parameters: bool = False,
trainable_parameters: bool = False,
seed_generator: keras.random.SeedGenerator = None,
**kwargs,
):
Expand All @@ -39,7 +40,7 @@ def __init__(
std : int, float, np.ndarray, or Tensor, optional
The standard deviation of the Gaussian distribution. Can be a scalar or a tensor.
Default is 1.0.
use_learnable_parameters : bool, optional
trainable_parameters : bool, optional
Whether to treat the mean and standard deviation as learnable parameters. Default is False.
seed_generator : keras.random.SeedGenerator, optional
A Keras seed generator for reproducible random sampling. If None, a new seed
Expand All @@ -53,47 +54,41 @@ def __init__(
self.mean = mean
self.std = std

self.trainable_parameters = trainable_parameters
self.seed_generator = seed_generator or keras.random.SeedGenerator()

self.dim = None
self.log_normalization_constant = None

self.use_learnable_parameters = use_learnable_parameters

if seed_generator is None:
seed_generator = keras.random.SeedGenerator()

self.seed_generator = seed_generator
self._mean = None
self._std = None

def build(self, input_shape: Shape) -> None:
if self.built:
return

self.dim = int(input_shape[-1])

self.mean = keras.ops.broadcast_to(self.mean, (self.dim,))
self.mean = keras.ops.cast(self.mean, "float32")
self.std = keras.ops.broadcast_to(self.std, (self.dim,))
self.std = keras.ops.cast(self.std, "float32")
self.mean = ops.cast(ops.broadcast_to(self.mean, (self.dim,)), "float32")
self.std = ops.cast(ops.broadcast_to(self.std, (self.dim,)), "float32")

self.log_normalization_constant = -0.5 * self.dim * math.log(2.0 * math.pi) - keras.ops.sum(
keras.ops.log(self.std)
)
self.log_normalization_constant = -0.5 * self.dim * math.log(2.0 * math.pi) - ops.sum(ops.log(self.std))

if self.use_learnable_parameters:
if self.trainable_parameters:
self._mean = self.add_weight(
shape=keras.ops.shape(self.mean),
# Initializing with const tensor https://github.com/keras-team/keras/pull/20457#discussion_r1832081248
initializer=keras.initializers.get(value=self.mean),
shape=ops.shape(self.mean),
initializer=keras.initializers.get(self.mean),
dtype="float32",
trainable=True,
)
self._std = self.add_weight(
shape=keras.ops.shape(self.std),
# Initializing with const tensor https://github.com/keras-team/keras/pull/20457#discussion_r1832081248
initializer=keras.initializers.get(self.std),
dtype="float32",
shape=ops.shape(self.std), initializer=keras.initializers.get(self.std), dtype="float32", trainable=True
)
else:
self._mean = self.mean
self._std = self.std

def log_prob(self, samples: Tensor, *, normalize: bool = True) -> Tensor:
result = -0.5 * keras.ops.sum((samples - self._mean) ** 2 / self.std**2, axis=-1)
result = -0.5 * ops.sum((samples - self._mean) ** 2 / self._std**2, axis=-1)

if normalize:
result += self.log_normalization_constant
Expand All @@ -110,7 +105,7 @@ def get_config(self):
config = {
"mean": self.mean,
"std": self.std,
"use_learnable_parameters": self.use_learnable_parameters,
"trainable_parameters": self.trainable_parameters,
"seed_generator": self.seed_generator,
}

Expand Down
Loading