ExponentialScore#

class bayesflow.networks.inference.scoring.scoring_rules.ExponentialScore(**kwargs)[source]#

Bases: CategoricalScoringRule

\(S(\{f_k\}, m) = \sum_{k \neq m} \exp\!\left(\tfrac{1}{2}(f_k - f_m)\right)\)

The network outputs logits \(f\) whose softmax is the posterior over models; pairwise differences \(f_k - f_j\) estimate log Bayes factors \(\log K_{k,j}\).

A Leaky head link can be passed via links to improve numerical recovery of extreme log Bayes factors without affecting properness.

Examples

>>> ExponentialScore()

With a leaky l-POP head link (recommended for extreme Bayes factors):

>>> import bayesflow as bf
>>> ExponentialScore(links={"logits": bf.links.Leaky(power=2.0)})
NOT_TRANSFORMING_LIKE_VECTOR_WARNING: tuple[str] = ('logits',)#
get_head_shapes_from_target_shape(target_shape: tuple[int, ...]) dict[str, tuple[int, ...]][source]#

Request a dictionary of names and output shapes of required heads from the score.

score(estimates: dict[str, Tensor], targets: Tensor, weights: Tensor = None) Tensor[source]#

Computes the exponential Bayes factor score.

Parameters:
estimatesdict[str, Tensor]

Must contain "logits" of shape (..., M).

targetsTensor

One-hot encoded true model labels of shape (..., M).

weightsTensor, optional

Per-sample weights for a weighted mean.

Returns:
Tensor

(Optionally weighted) mean exponential score over the batch.

get_config()[source]#
TRANSFORMATION_TYPE: dict[str, str] = {}#

Defines nonstandard transformation behaviour for de-standardization.

The standard transformation

x_i = x_i’ * sigma_i + mu_i

is referred to as “location_scale”. Keys not specified here will fallback to that default.

classmethod from_config(config)#
get_head(key: str, output_shape: tuple[int, ...]) Sequential#

For a specified head key and output shape, request corresponding head network.

A head network has the following components that are called sequentially:

  1. subnet: A keras.Layer.

  2. dense: A trainable linear projection with as many units as are required by the next component.

  3. reshape: Changes shape of output of projection to match requirements of next component.

  4. link: Transforms unconstrained values into a constrained space for the final estimator. See links for examples.

This method initializes the components in reverse order to meet all requirements and returns them.

Parameters:
keystr

Name of head for which to request a link.

output_shape: Shape

The necessary shape of estimated values for the given key as returned by get_head_shapes_from_target_shape().

Returns:
headkeras.Sequential

Head network consisting of a learnable projection, a reshape and a link operation to parameterize estimates.

For a specified key, request a link from network output to estimation target.

If no link was specified for the key (e.g. upon initialization), return a linear activation.

Parameters:
keystr

Name of head for which to request a link.

Returns:
linkkeras.Layer

Activation function linking network output to estimation target.

get_subnet(key: str) Layer#

For a specified key, request a subnet to be used for projecting the shared condition embedding before further projection and reshaping to the heads output shape.

If no subnet was specified for the key (e.g. upon initialization), return just an instance of keras.layers.Identity.

Parameters:
keystr

Name of head for which to request a subnet.

Returns:
linkkeras.Layer

Subnet projecting the shared condition embedding.

to_log_odds(rule_output: dict[str, Tensor]) Tensor#

Map head output to M log posterior odds.