CrossEntropyScore#

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

Bases: ScoringRule

Categorical cross-entropy scoring rule for classification tasks.

Scores a set of predicted logits against one-hot encoded target labels:

\(S(\hat y, y) = -\sum_c y_c \log \mathrm{softmax}(\hat y)_c\)

Designed for model comparison / classification, where the network outputs raw logits and targets are one-hot encoded class indicators.

get_head_shapes_from_target_shape(target_shape: 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 categorical cross-entropy from logits or probs.

Parameters:
estimatesdict[str, Tensor]

A dictionary containing tensors of estimated values. The "logits" key must be present, containing raw (unnormalized) class scores of shape (..., num_classes).

targetsTensor

One-hot encoded target labels of shape (..., num_classes).

weightsTensor, optional

Per-sample weights. If provided, computes a weighted mean score.

Returns:
Tensor

The (optionally weighted) mean categorical cross-entropy.

get_config()[source]#
NOT_TRANSFORMING_LIKE_VECTOR_WARNING: tuple[str] = ()#

Names of prediction heads for which to warn if the adapter is called on their estimates in inverse direction.

Prediction heads can output estimates in spaces other than the target distribution space. To such estimates the adapter cannot be straightforwardly applied in inverse direction, because the adapter is built to map vectors from the inference variable space. When subclassing ScoringRule, add the names of such heads to the following list to warn users about difficulties with a type of estimate whenever the adapter is applied to them in inverse direction.

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.