bayesflow.losses module#

bayesflow.losses.kl_latent_space_gaussian(z, log_det_J)[source]#

Computes the Kullback-Leibler divergence between true and approximate posterior assuming a Gaussian latent space as a source distribution.

Parameters:
ztf.Tensor of shape (batch_size, …)

The (latent transformed) target variables

log_det_Jtf.Tensor of shape (batch_size, …)

The logartihm of the Jacobian determinant of the transformation.

Returns:
losstf.Tensor

A single scalar value representing the KL loss, shape (,)

Examples

Parameter estimation

>>> kl_latent_space_gaussian(z, log_det_J)
bayesflow.losses.kl_latent_space_student(v, z, log_det_J)[source]#

Computes the Kullback-Leibler divergence between true and approximate posterior assuming latent student t-distribution as a source distribution.

Parameters:
vtf Tensor of shape (batch_size, …)

The degrees of freedom of the latent student t-distribution

ztf.Tensor of shape (batch_size, …)

The (latent transformed) target variables

log_det_Jtf.Tensor of shape (batch_size, …)

The logarithm of the Jacobian determinant of the transformation.

Returns:
losstf.Tensor

A single scalar value representing the KL loss, shape (,)

bayesflow.losses.kl_dirichlet(model_indices, alpha)[source]#

Computes the KL divergence between a Dirichlet distribution with parameter vector alpha and a uniform Dirichlet.

Parameters:
model_indicestf.Tensor of shape (batch_size, n_models)

one-hot-encoded true model indices

alphatf.Tensor of shape (batch_size, n_models)

positive network outputs in [1, +inf]

Returns:
kltf.Tensor

A single scalar representing \(D_{KL}(\mathrm{Dir}(\alpha) | \mathrm{Dir}(1,1,\ldots,1) )\), shape (,)

bayesflow.losses.mmd_summary_space(summary_outputs, z_dist=<function random_normal>, kernel='gaussian')[source]#

Computes the MMD(p(summary_otuputs) | z_dist) to re-shape the summary network outputs in an information-preserving manner.

Parameters:
summary_outputstf Tensor of shape (batch_size, …)

The outputs of the summary network.

z_distcallable, default tf.random.normal

The latent data distribution towards which the summary outputs are optimized.

kernelstr in (‘gaussian’, ‘inverse_multiquadratic’), default ‘gaussian’

The kernel function to use for MMD computation.

bayesflow.losses.log_loss(model_indices, preds, evidential=False, label_smoothing=0.01)[source]#

Computes the logarithmic loss given true model_indices and approximate model probabilities either according to [1] if evidential is True or according to [2] if evidential is False.

[1] Radev, S. T., D’Alessandro, M., Mertens, U. K., Voss, A., Köthe, U., & Bürkner, P. C. (2021). Amortized bayesian model comparison with evidential deep learning. IEEE Transactions on Neural Networks and Learning Systems.

[2] Elsemüller, L., Schnuerch, M., Bürkner, P. C., & Radev, S. T. (2023). A Deep Learning Method for Comparing Bayesian Hierarchical Models. arXiv preprint arXiv:2301.11873.

Parameters:
model_indicestf.Tensor of shape (batch_size, num_models)

one-hot-encoded true model indices

predstf.Tensor of shape (batch_size, num_models)

If evidential is True these should be the concentration parameters of a Dirichlet density bounded between [1, +inf]. Else, these should be normalized probability values.

evidentialboolean, optional, default: False

Whether to first normalize preds (True) or assume normalized (False, default)

label_smoothingfloat or None, optional, default: 0.01

Optional label smoothing factor.

Returns:
losstf.Tensor

A single scalar Monte-Carlo approximation of the log-loss, shape (,)