classwisewrapper
Metric
ClasswiseWrapperfromtorchmetrics(torchmetrics.ClasswiseWrapper)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with ClasswiseWrapper, or
mentions torchmetrics.ClasswiseWrapper directly, or wants the standard torchmetrics implementation.
Reference signature
from torchmetrics import ClasswiseWrapper
# ClasswiseWrapper(metric: torchmetrics.metric.Metric, labels: Optional[list[str]] = None, prefix: Optional[str] = None, postfix: Optional[str] = None) -> None
Library docstring
Wrapper metric for altering the output of classification metrics.
This metric works together with classification metrics that returns multiple values (one value per class) such that
label information can be automatically included in the output.
Args:
metric: base metric that should be wrapped. It is assumed that the metric outputs a single
tensor that is split along the first dimension.
labels: list of strings indicating the different classes.
prefix: string that is prepended to the metric names.
postfix: string that is appended to the metric names.
Example::
Basic example where the output of a metric is unwrapped into a dictionary with the class index as keys:
>>> from torch import randint, randn
>>> from torchmetrics.wrappers import ClasswiseWrapper
>>> from torchmetrics.classification import MulticlassAccuracy
>>> metric = ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None))
>>> preds = randn(10, 3).softmax(dim=-1)
>>> target = randint(3, (10,))
>>> metric(preds, target) # doctest: +NORMALIZE_WHITESPACE
{'multiclassaccuracy_0': tensor(0.5000),
'multiclassaccuracy_1': tensor(0.7500),
'multiclassaccuracy_2': tensor(0.)}
Example::
Using custom name via prefix and postfix:
>>> from torch import randint, randn
>>> from torchmetrics.wrappers import ClasswiseWrapper
>>> from torchmetrics.classification import MulticlassAccuracy
>>> metric_pre = ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None), prefix="acc-")
>>> metric_post = ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None), postfix="-acc")
>>> preds = randn(10, 3).softmax(dim=-1)
>>> target = randint(3, (10,))
>>> metric_pre(preds, target) # doctest: +NORMALIZE_WHITESPACE
{'acc-0': tensor(0.3333), 'acc-1': tensor(0.6667), 'acc-2': tensor(0.)}
>>> metric_post(preds, target) # doctest: +NORMALIZE_WHITESPACE
{'0-acc': tensor(0.3333), '1-acc': tensor(0.6667), '2-acc': tensor(0.)}
Example::
Providing labels as a list of strings:
>>> from torch import randint, randn
>>> from torchmetrics.wrappers import ClasswiseWrapper
>>> from torch
Quick recipe
import torchmetrics as _m
score = _m.ClasswiseWrapper(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).