LogisticScore#
- class bayesflow.scoring_rules.LogisticScore(alpha: float = 0.0, **kwargs)[source]#
Bases:
CategoricalScoringRule\(S(\{f_k\}, m; \alpha) = \sum_{k \neq m} \left(1 + e^{(f_k - f_m)/(\alpha+1)}\right)^\alpha\) for \(\alpha > 0\), and its \(\alpha \to 0\) limit
\[S(\{f_k\}, m) = \sum_{k \neq m} \log\!\left(1 + e^{f_k - f_m}\right)\]for \(\alpha = 0\) (default, the plain logistic rule).
The network outputs logits \(f\) whose softmax is the posterior over models.
- Parameters:
- alphafloat, optional
Power exponent (default: 0.0, the plain logistic rule). Must be non-negative.
Examples
Plain logistic rule:
>>> LogisticScore()
Power logistic rule:
>>> LogisticScore(alpha=1.0)
- 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 (power) logistic 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 (power) logistic score over the batch.
- 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:
subnet: A keras.Layer.
dense: A trainable linear projection with as many units as are required by the next component.
reshape: Changes shape of output of projection to match requirements of next component.
link: Transforms unconstrained values into a constrained space for the final estimator. See
linksfor 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.
- get_link(key: str) Layer#
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.