Skip to content

Commit b38f23e

Browse files
Merge pull request #150 from stefanradev93/Development
Development
2 parents 01e6588 + 3d90e91 commit b38f23e

File tree

6 files changed

+680
-13
lines changed

6 files changed

+680
-13
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ the model-amortizer combination on unseen simulations:
105105
# Generate 500 new simulated data sets
106106
new_sims = trainer.configurator(generative_model(500))
107107

108-
# Obtain 100 posteriors draws per data set instantly
108+
# Obtain 100 posterior draws per data set instantly
109109
posterior_draws = amortized_posterior.sample(new_sims, n_samples=100)
110110

111-
# Diagnoze calibration
111+
# Diagnose calibration
112112
fig = bf.diagnostics.plot_sbc_histograms(posterior_draws, new_sims['parameters'])
113113
```
114114

@@ -302,7 +302,7 @@ This project is currently managed by researchers from Rensselaer Polytechnic Ins
302302
You can cite BayesFlow along the lines of:
303303

304304
- We approximated the posterior with neural posterior estimation and learned summary statistics (NPE; Radev et al., 2020), as implemented in the BayesFlow software for amortized Bayesian workflows (Radev et al., 2023a).
305-
- We approximated the likelihood with neural likelihood estimation (NLE; Papamakarios et al., 2019) without hand-cafted summary statistics, as implemented in the BayesFlow software for amortized Bayesian workflows (Radev et al., 2023b).
305+
- We approximated the likelihood with neural likelihood estimation (NLE; Papamakarios et al., 2019) without hand-crafted summary statistics, as implemented in the BayesFlow software for amortized Bayesian workflows (Radev et al., 2023b).
306306
- We performed simultaneous posterior and likelihood estimation with jointly amortized neural approximation (JANA; Radev et al., 2023a), as implemented in the BayesFlow software for amortized Bayesian workflows (Radev et al., 2023b).
307307

308308
1. Radev, S. T., Schmitt, M., Schumacher, L., Elsemüller, L., Pratz, V., Schälte, Y., Köthe, U., & Bürkner, P.-C. (2023a). BayesFlow: Amortized Bayesian workflows with neural networks. *The Journal of Open Source Software, 8(89)*, 5702.([arXiv](https://arxiv.org/abs/2306.16015))([JOSS](https://joss.theoj.org/papers/10.21105/joss.05702))

bayesflow/amortizers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from bayesflow.default_settings import DEFAULT_KEYS
3232
from bayesflow.exceptions import ConfigurationError, SummaryStatsError
3333
from bayesflow.helper_functions import check_tensor_sanity
34-
from bayesflow.losses import log_loss, mmd_summary_space
34+
from bayesflow.losses import log_loss, mmd_summary_space, norm_diff
3535
from bayesflow.networks import EvidentialNetwork
3636

3737

@@ -412,7 +412,7 @@ def _compute_summary_condition(self, summary_conditions, direct_conditions, **kw
412412
elif direct_conditions is not None:
413413
full_cond = direct_conditions
414414
else:
415-
raise SummaryStatsError("Could not concatenarte or determine conditioning inputs...")
415+
raise SummaryStatsError("Could not concatenate or determine conditioning inputs...")
416416
return sum_condition, full_cond
417417

418418
def _determine_latent_dist(self, latent_dist):
@@ -1337,7 +1337,7 @@ def compute_loss(self, input_dict, **kwargs):
13371337
"""
13381338

13391339
net_out = self(input_dict, **kwargs)
1340-
loss = tf.reduce_mean(self.loss_fn(net_out - input_dict[DEFAULT_KEYS["parameters"]]))
1340+
loss = tf.reduce_mean(self.loss_fn(net_out, input_dict[DEFAULT_KEYS["parameters"]]))
13411341
return loss
13421342

13431343
def _compute_summary_condition(self, summary_conditions, direct_conditions, **kwargs):
@@ -1366,4 +1366,4 @@ def _determine_loss(self, loss_fun, norm_ord):
13661366
# In case of user-provided loss, override norm order
13671367
if loss_fun is not None:
13681368
return loss_fun
1369-
return partial(tf.norm, ord=norm_ord, axis=-1)
1369+
return partial(norm_diff, ord=norm_ord, axis=-1)

bayesflow/helper_networks.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def __init__(self, latent_dim, act_norm_init, **kwargs):
332332

333333
super().__init__(**kwargs)
334334

335-
# Initialize scale and bias with zeros and ones if no batch for initalization was provided.
335+
# Initialize scale and bias with zeros and ones if no batch for initialization was provided.
336336
if act_norm_init is None:
337337
self.scale = tf.Variable(tf.ones((latent_dim,)), trainable=True, name="act_norm_scale")
338338

@@ -594,7 +594,15 @@ class ConfigurableMLP(tf.keras.Model):
594594
"""Implements a simple configurable MLP with optional residual connections and dropout."""
595595

596596
def __init__(
597-
self, input_dim, hidden_dim=512, num_hidden=2, activation="relu", residual=True, dropout_rate=0.05, **kwargs
597+
self,
598+
input_dim,
599+
hidden_dim=512,
600+
output_dim=None,
601+
num_hidden=2,
602+
activation="relu",
603+
residual=True,
604+
dropout_rate=0.05,
605+
**kwargs,
598606
):
599607
"""
600608
Creates an instance of a flexible and simple MLP with optional residual connections and dropout.
@@ -605,6 +613,8 @@ def __init__(
605613
The input dimensionality
606614
hidden_dim : int, optional, default: 512
607615
The dimensionality of the hidden layers
616+
output_dim : int, optional, default: None
617+
The output dimensionality. If None is passed, `output_dim` is set to `input_dim`
608618
num_hidden : int, optional, default: 2
609619
The number of hidden layers (minimum: 1)
610620
activation : string, optional, default: 'relu'
@@ -618,6 +628,7 @@ def __init__(
618628
super().__init__(**kwargs)
619629

620630
self.input_dim = input_dim
631+
self.output_dim = input_dim if output_dim is None else output_dim
621632
self.model = tf.keras.Sequential(
622633
[tf.keras.layers.Dense(hidden_dim, activation=activation), tf.keras.layers.Dropout(dropout_rate)]
623634
)
@@ -630,7 +641,7 @@ def __init__(
630641
dropout_rate=dropout_rate,
631642
)
632643
)
633-
self.model.add(tf.keras.layers.Dense(input_dim))
644+
self.model.add(tf.keras.layers.Dense(self.output_dim))
634645

635646
def call(self, inputs, **kwargs):
636647
return self.model(inputs, **kwargs)

bayesflow/losses.py

+17
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,20 @@ def log_loss(model_indices, preds, evidential=False, label_smoothing=0.01):
184184
# Actual loss + regularization (if given)
185185
loss = -tf.reduce_mean(tf.reduce_sum(model_indices * tf.math.log(preds), axis=1))
186186
return loss
187+
188+
189+
def norm_diff(tensor_a, tensor_b, axis=None, ord='euclidean'):
190+
"""
191+
Wrapper around tf.norm that computes the norm of the difference between two tensors along the specified axis.
192+
193+
Parameters
194+
----------
195+
tensor_a : A Tensor.
196+
tensor_b : A Tensor. Must be the same shape as tensor_a.
197+
axis : Any or None
198+
Axis along which to compute the norm of the difference. Default is None.
199+
ord : int or str
200+
Order of the norm. Supports 'euclidean' and other norms supported by tf.norm. Default is 'euclidean'.
201+
"""
202+
difference = tensor_a - tensor_b
203+
return tf.norm(difference, ord=ord, axis=axis)

examples/Amortized_Point_Estimation.ipynb

+639
Large diffs are not rendered by default.

setup.cfg

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ install_requires =
4242
seaborn >= 0.11
4343
tqdm >= 4.65
4444
matplotlib >= 3.5
45-
tensorflow-macos >= 2.10; sys_platform == 'darwin' and platform_machine == 'arm64'
46-
tensorflow >= 2.10.1; sys_platform != 'darwin' or platform_machine != 'arm64'
47-
tensorflow_probability >= 0.17
45+
tensorflow-macos >= 2.10, < 2.16; sys_platform == 'darwin' and platform_machine == 'arm64'
46+
tensorflow >= 2.10.1, < 2.16; sys_platform != 'darwin' or platform_machine != 'arm64'
47+
tensorflow_probability >= 0.17, < 0.24
4848

4949
[options.extras_require]
5050
testing =

0 commit comments

Comments
 (0)