PoissonD2Score#

class ibl_bwb_eval.metrics.PoissonD2Score(num_outputs=None, degenerate_value=None, eps=1e-09, validate_finite=True, **kwargs)[source]#

Bases: torchmetrics.metric.Metric

Minimal implementation of Cohen pseudo-R^2 score for Poisson NLL loss.

Supports 1D tensors (single output) and 2D tensors of shape (T, N) (multiple outputs). Assumes log-rates for the predictions.

For 2D inputs, per-output R^2 scores are computed and the mean is returned. Outputs with zero total counts are excluded from the mean; if all outputs are degenerate the degenerate_value is returned.

The number of outputs is inferred automatically from the last dimension of the first call to update(), or can be provided explicitly via num_outputs.

Parameters:
  • num_outputs (Optional[int]) – Number of outputs. If provided, states are pre-allocated eagerly; otherwise the size is inferred from the last dim of the first update() call.

  • degenerate_value (Optional[Tensor]) – The value to return when the metric is degenerate.

  • eps (float) – The epsilon value to use for numerical stability.

  • validate_finite (bool) – Whether to check preds for NaN/inf on every update(). Disable only when the caller already trusts preds is finite (e.g. scoring predictions written by a validated pipeline).

The pseudo-R2 score is computed as:

\[1 - \frac{N_{pred} - N_{saturated}}{N_{null} - N_{saturated}}\]

where \(N_{pred}\) is the predicted Poisson NLL loss, \(N_{saturated}\) is the saturated loss, and \(N_{null}\) is the null (mean-rate) loss.

update(preds, target)[source]#

Accumulate predictions and targets into the metric state.

Parameters:
  • preds (Tensor) – Predicted log-rates of shape (T,) or (T, N).

  • target (Tensor) – Target counts of shape (T,) or (T, N).

compute()[source]#

Override this method to compute the final metric value.

This method will automatically synchronize state variables when running in distributed backend.

Return type:

Tensor