Skip to content

Commit 44af8e7

Browse files
committed
Documentation and cleanup
1 parent f791a81 commit 44af8e7

File tree

20 files changed

+414
-190
lines changed

20 files changed

+414
-190
lines changed

assets/__init__.py

Whitespace-only changes.

assets/benchmark_network_architectures.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

bayesflow/diagnostics/metrics/calibration_error.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def calibration_error(
1515
min_quantile: float = 0.005,
1616
max_quantile: float = 0.995,
1717
) -> Mapping[str, Any]:
18-
"""Computes an aggregate score for the marginal calibration error over an ensemble of approximate
18+
"""
19+
Computes an aggregate score for the marginal calibration error over an ensemble of approximate
1920
posteriors. The calibration error is given as the aggregate (e.g., median) of the absolute deviation
2021
between an alpha-CI and the relative number of inliers from ``estimates`` over multiple alphas in
2122
(0, 1).

bayesflow/diagnostics/metrics/expected_calibration_error.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ def expected_calibration_error(
1313
n_bins: int = 10,
1414
return_probs: bool = False,
1515
) -> Mapping[str, Any]:
16-
"""Estimates the expected calibration error (ECE) of a model comparison network according to [1].
16+
"""
17+
Estimates the expected calibration error (ECE) of a model comparison network according to [1].
1718
18-
[1] Naeini, M. P., Cooper, G., & Hauskrecht, M. (2015).
19-
Obtaining well calibrated probabilities using bayesian binning.
20-
In Proceedings of the AAAI conference on artificial intelligence (Vol. 29, No. 1).
19+
[1] Naeini, M. P., Cooper, G., & Hauskrecht, M. (2015). Obtaining well calibrated probabilities using
20+
Bayesian binning. In Proceedings of the AAAI conference on artificial intelligence (Vol. 29, No. 1).
2121
2222
Notes
2323
-----
24-
Make sure that ``targets`` are **one-hot encoded** classes!
24+
Make sure that ``targets`` are **one-hot encoded** classes (i.e., model indices)!
2525
2626
Parameters
2727
----------

bayesflow/diagnostics/metrics/posterior_contraction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def posterior_contraction(
1212
variable_names: Sequence[str] = None,
1313
aggregation: Callable = np.median,
1414
) -> Mapping[str, Any]:
15-
"""Computes the posterior contraction (PC) from prior to posterior for the given samples.
15+
"""
16+
Computes the posterior contraction (PC) from prior to posterior for the given samples.
1617
1718
Parameters
1819
----------

bayesflow/diagnostics/metrics/root_mean_squared_error.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def root_mean_squared_error(
1313
normalize: bool = True,
1414
aggregation: Callable = np.median,
1515
) -> Mapping[str, Any]:
16-
"""Computes the (Normalized) Root Mean Squared Error (RMSE/NRMSE) for the given posterior and prior samples.
16+
"""
17+
Computes the (Normalized) Root Mean Squared Error (RMSE/NRMSE) for the given posterior and prior samples.
1718
1819
Parameters
1920
----------

bayesflow/distributions/diagonal_normal.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ def __init__(
2222
seed_generator: keras.random.SeedGenerator = None,
2323
**kwargs,
2424
):
25+
"""
26+
Initializes a backend-agnostic diagonal Gaussian distribution with optional learnable parameters.
27+
28+
This class represents a Gaussian distribution with a diagonal covariance matrix, allowing for efficient
29+
sampling and density evaluation.
30+
31+
The mean and standard deviation can be specified as fixed values or learned during training. The class also
32+
supports random number generation with an optional seed for reproducibility.
33+
34+
Parameters
35+
----------
36+
mean : int, float, np.ndarray, or Tensor, optional
37+
The mean of the Gaussian distribution. Can be a scalar or a tensor. Default is 0.0.
38+
std : int, float, np.ndarray, or Tensor, optional
39+
The standard deviation of the Gaussian distribution. Can be a scalar or a tensor.
40+
Default is 1.0.
41+
use_learnable_parameters : bool, optional
42+
Whether to treat the mean and standard deviation as learnable parameters. Default is False.
43+
seed_generator : keras.random.SeedGenerator, optional
44+
A Keras seed generator for reproducible random sampling. If None, a new seed
45+
generator is created. Default is None.
46+
**kwargs
47+
Additional keyword arguments passed to the base `Distribution` class.
48+
49+
"""
50+
2551
super().__init__(**kwargs)
2652
self.mean = mean
2753
self.std = std

bayesflow/distributions/diagonal_student_t.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ def __init__(
2525
seed_generator: keras.random.SeedGenerator = None,
2626
**kwargs,
2727
):
28+
"""
29+
Initializes a backend-agnostic Student's t-distribution with optional learnable parameters.
30+
31+
This class represents a Student's t-distribution, which is useful for modeling heavy-tailed data.
32+
The distribution is parameterized by degrees of freedom (`df`), location (`loc`), and scale (`scale`).
33+
These parameters can either be fixed or learned during training.
34+
35+
The class also supports random number generation with an optional seed for reproducibility.
36+
37+
Parameters
38+
----------
39+
df : int or float
40+
Degrees of freedom for the Student's t-distribution. Lower values result in
41+
heavier tails, making it more robust to outliers.
42+
loc : int, float, np.ndarray, or Tensor, optional
43+
The location parameter (mean) of the distribution. Default is 0.0.
44+
scale : int, float, np.ndarray, or Tensor, optional
45+
The scale parameter (standard deviation) of the distribution. Default is 1.0.
46+
use_learnable_parameters : bool, optional
47+
Whether to treat `loc` and `scale` as learnable parameters. Default is False.
48+
seed_generator : keras.random.SeedGenerator, optional
49+
A Keras seed generator for reproducible random sampling. If None, a new seed
50+
generator is created. Default is None.
51+
**kwargs
52+
Additional keyword arguments passed to the base `Distribution` class.
53+
"""
54+
2855
super().__init__(**kwargs)
2956

3057
self.df = df

bayesflow/networks/consistency_models/consistency_model.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515

1616
@register_keras_serializable(package="bayesflow.networks")
1717
class ConsistencyModel(InferenceNetwork):
18-
"""Implements a Consistency Model with Consistency Training (CT) as
19-
described in [1-2]. The adaptations to CT described in [2] were taken
20-
into account in this implementation.
21-
22-
[1] Song, Y., Dhariwal, P., Chen, M. & Sutskever, I. (2023).
23-
Consistency Models.
24-
arXiv preprint arXiv:2303.01469
25-
26-
[2] Song, Y., & Dhariwal, P. (2023).
27-
Improved Techniques for Training Consistency Models:
28-
arXiv preprint arXiv:2310.14189
29-
Discussion: https://openreview.net/forum?id=WNzy9bRDvG
18+
"""Implements a Consistency Model with Consistency Training (CT) a described in [1-2]. The adaptations to CT
19+
described in [2] were taken into account in our implementation for ABI [3].
20+
21+
[1] Song, Y., Dhariwal, P., Chen, M. & Sutskever, I. (2023). Consistency Models. arXiv preprint arXiv:2303.01469
22+
23+
[2] Song, Y., & Dhariwal, P. (2023). Improved Techniques for Training Consistency Models.
24+
arXiv preprint arXiv:2310.14189. Discussion: https://openreview.net/forum?id=WNzy9bRDvG
25+
26+
[3] Schmitt, M., Pratz, V., Köthe, U., Bürkner, P. C., & Radev, S. T. (2023). Consistency models for scalable and
27+
fast simulation-based inference. arXiv preprint arXiv:2312.05440.
3028
"""
3129

3230
MLP_DEFAULT_CONFIG = {
@@ -49,8 +47,7 @@ def __init__(
4947
s1: int | float = 50,
5048
**kwargs,
5149
):
52-
"""Creates an instance of a consistency model (CM) to be used
53-
for standalone consistency training (CT).
50+
"""Creates an instance of a consistency model (CM) to be used for standalone consistency training (CT).
5451
5552
Parameters:
5653
-----------

bayesflow/networks/coupling_flow/actnorm.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@
1010

1111
@serializable(package="networks.coupling_flow")
1212
class ActNorm(InvertibleLayer):
13-
"""Implements an Activation Normalization (ActNorm) Layer.
14-
Activation Normalization is learned invertible normalization, using
15-
a Scale (s) and Bias (b) vector::
13+
"""Implements an Activation Normalization (ActNorm) Layer. Activation Normalization is learned invertible
14+
normalization, using a scale (s) and a bias (b) vector::
1615
1716
y = s * x + b(forward)
1817
x = (y - b) / s(inverse)
1918
2019
References
2120
----------
2221
23-
.. [1] Kingma, D. P., & Dhariwal, P. (2018).
24-
Glow: Generative flow with invertible 1x1 convolutions.
22+
[1] Kingma, D. P., & Dhariwal, P. (2018). Glow: Generative flow with invertible 1x1 convolutions.
2523
Advances in Neural Information Processing Systems, 31.
2624
27-
.. [2] Salimans, Tim, and Durk P. Kingma. (2016).
28-
Weight normalization: A simple reparameterization to accelerate
29-
training of deep neural networks.
30-
Advances in Neural Information Processing Systems, 29.
25+
[2] Salimans, Tim, and Durk P. Kingma. (2016). Weight normalization: A simple reparameterization to accelerate
26+
training of deep neural networks. Advances in Neural Information Processing Systems, 29.
3127
"""
3228

3329
def __init__(self, **kwargs):

0 commit comments

Comments
 (0)