multiclasssensitivityatspecificity
Metric
MulticlassSensitivityAtSpecificityfromtorchmetrics(torchmetrics.classification.MulticlassSensitivityAtSpecificity)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with MulticlassSensitivityAtSpecificity, or
mentions torchmetrics.classification.MulticlassSensitivityAtSpecificity directly, or wants the standard torchmetrics implementation.
Reference signature
from torchmetrics.classification import MulticlassSensitivityAtSpecificity
# MulticlassSensitivityAtSpecificity(num_classes: int, min_specificity: float, thresholds: Union[int, list[float], torch.Tensor, NoneType] = None, ignore_index: Optional[int] = None, validate_args: bool = True, **kwargs: Any) -> None
Library docstring
Compute the highest possible sensitivity value given the minimum specificity thresholds provided.
This is done by first calculating the Receiver Operating Characteristic (ROC) curve for different thresholds and the
find the sensitivity for a given specificity level.
For multiclass the metric is calculated by iteratively treating each class as the positive class and all other
classes as the negative, which is referred to as the one-vs-rest approach. One-vs-one is currently not supported by
this metric.
Accepts the following input tensors:
- ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
softmax per sample.
- ``target`` (int tensor): ``(N, ...)``. Target should be a tensor containing ground truth labels, and therefore
only contain values in the [0, n_classes-1] range (except if `ignore_index` is specified).
Additional dimension ``...`` will be flattened into the batch dimension.
The implementation both supports calculating the metric in a non-binned but accurate version and a binned version
that is less accurate but more memory efficient. Setting the `thresholds` argument to `None` will activate the
non-binned version that uses memory of size :math:`\mathcal{O}(n_{samples})` whereas setting the `thresholds`
argument to either an integer, list or a 1d tensor will use a binned version that uses memory of
size :math:`\mathcal{O}(n_{thresholds} \times n_{classes})` (constant memory).
Args:
num_classes: Integer specifying the number of classes
min_specificity: float value specifying minimum specificity threshold.
thresholds:
Can be one of:
- ``None``, will use a non-binned approach where thresholds are dynamically calculated from
all the data. It is the most accurate but also the most memory-consuming approach.
- ``int`` (larger than 1), will use that number of thresholds linearly spaced from
0 to 1 as bins for the calculation.
- ``list`` of floats, will use the indicated thresholds in the list as bins for the calculation
Quick recipe
import torchmetrics.classification as _m
score = _m.MulticlassSensitivityAtSpecificity(y_true, y_pred)
Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is
(y_true, y_pred)while torchmetrics is(preds, target).