Skip to content

Commit 78393cb

Browse files
Merge pull request #147 from han-ol/Development
Bayes Estimators: Loss functions with flexible signature
2 parents 65961be + 597aa3b commit 78393cb

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

@@ -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/losses.py

Lines changed: 17 additions & 0 deletions
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)

0 commit comments

Comments
 (0)