minmaxmetric
Metric
MinMaxMetricfromtorchmetrics(torchmetrics.MinMaxMetric)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with MinMaxMetric, or
mentions torchmetrics.MinMaxMetric directly, or wants the standard torchmetrics implementation.
Reference signature
from torchmetrics import MinMaxMetric
# MinMaxMetric(base_metric: torchmetrics.metric.Metric, **kwargs: Any) -> None
Library docstring
Wrapper metric that tracks both the minimum and maximum of a scalar/tensor across an experiment.
The min/max value will be updated each time ``.compute`` is called.
Args:
base_metric:
The metric of which you want to keep track of its maximum and minimum values.
kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
Raises:
ValueError
If ``base_metric` argument is not a subclasses instance of ``torchmetrics.Metric``
Example::
>>> import torch
>>> from torchmetrics.wrappers import MinMaxMetric
>>> from torchmetrics.classification import BinaryAccuracy
>>> from pprint import pprint
>>> base_metric = BinaryAccuracy()
>>> minmax_metric = MinMaxMetric(base_metric)
>>> preds_1 = torch.Tensor([[0.1, 0.9], [0.2, 0.8]])
>>> preds_2 = torch.Tensor([[0.9, 0.1], [0.2, 0.8]])
>>> labels = torch.Tensor([[0, 1], [0, 1]]).long()
>>> pprint(minmax_metric(preds_1, labels))
{'max': tensor(1.), 'min': tensor(1.), 'raw': tensor(1.)}
>>> pprint(minmax_metric.compute())
{'max': tensor(1.), 'min': tensor(1.), 'raw': tensor(1.)}
>>> minmax_metric.update(preds_2, labels)
>>> pprint(minmax_metric.compute())
{'max': tensor(1.), 'min': tensor(0.7500), 'raw': tensor(0.7500)}
Quick recipe
import torchmetrics as _m
score = _m.MinMaxMetric(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).