PolynomialScore#
- class bayesflow.scoring_rules.PolynomialScore(alpha: float = 2.0, links=None, **kwargs)[source]#
Bases:
CategoricalScoringRule\(S(\hat p_{1\ldots C}, y; \alpha) = \tfrac{\alpha-1}{\alpha}\sum_c \hat p_c^\alpha - \hat p_y^{\alpha-1}\)
Implements the Tsallis proper scoring rule on the probability simplex, derived from the Savage representation with \(G(p) = \frac{1}{\alpha}\sum_k p_k^\alpha\):
\[S(p, m; \alpha) = \frac{\alpha - 1}{\alpha}\sum_k p_k^\alpha - p_m^{\alpha - 1}\]where \(p = \mathrm{softmax}(\hat y)\) and \(m\) is the true model index. The unique minimizer of the expected score is the true posterior \(p_k^* = P(\mathcal{M}_k \mid x)\) for any \(\alpha > 1\).
For \(\alpha = 2\) this is proportional to the
BrierScore(same gradient direction, same minimizer). Larger \(\alpha\) sharpens the penalty for wrong predictions.- Parameters:
- alphafloat, optional
Exponent (default: 2.0). Must satisfy \(\alpha > 1\).
- 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.
- to_log_odds(rule_output: dict[str, Tensor]) Tensor[source]#
Map head output to M log posterior odds.
- score(estimates: dict[str, Tensor], targets: Tensor, weights: Tensor = None) Tensor[source]#
Computes the Tsallis polynomial score from probabilities.
- Parameters:
- estimatesdict[str, Tensor]
Must contain
"probs"with shape(..., num_models).- targetsTensor
One-hot encoded target labels of shape
(..., num_models).- weightsTensor, optional
Per-sample weights for a weighted mean.
- Returns:
- Tensor
(Optionally weighted) mean Tsallis polynomial 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.