TorchMetrics is a comprehensive collection of PyTorch native metrics for evaluating machine learning models, offering over 100 common and specialized metrics implemented directly in PyTorch. Developed and maintained by Lightning AI, it provides a standardized, rigorously tested, and distributed-training compatible API for metric computation, reducing boilerplate and ensuring reproducibility. It automatically accumulates over batches and synchronizes between multiple devices. The library is currently at version 1.9.0 and maintains a regular release cadence with several patch and minor releases per year.
pip install torchmetricsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core ways to use TorchMetrics: the functional API for stateless, single-batch computation, the class-based API for accumulating states over multiple batches, and MetricCollection for grouping several metrics. Remember to reset class-based metrics after each epoch or evaluation phase to avoid mixing states.
Upgrade your Python environment to 3.10 or newer, or pin `torchmetrics<1.9.0`.
Explicitly set the `average` argument in `DiceScore` to `None` or your desired reduction method if you relied on the previous default behavior.
Always initialize separate metric instances for different phases (training, validation, test) or call `metric.reset()` after each complete evaluation epoch/phase to clear its internal state.
Call `metric.to(device)` after initialization, or ensure the metric is registered as a child module within a `torch.nn.Module` or `LightningModule`, which handles device transfers automatically.
Use `torch.nn.ModuleList` or `torch.nn.ModuleDict` instead of native Python collections when nesting metrics within a `torch.nn.Module`.
Ensure all metrics within the `MetricCollection` receive `update` calls for the relevant data. If issues persist, consider isolating metrics or upgrading to the latest `torchmetrics` version as device management and state handling are continually improved.
Profile your code. For `MeanMetric`, consider `metric.update(value, weight=my_tensor)`. For advanced optimization, explore `Aggregator` configurations and carefully manage device placement and cross-device synchronization.
Ensure your environment has necessary build tools installed. For Alpine Linux, this typically means running `apk add build-base` or explicitly installing `gcc` and `g++`. For Debian/Ubuntu-based systems, `apt-get install build-essential` is usually sufficient.
Import the metric from its correct submodule: `from torchmetrics.classification import Accuracy`
Initialize the metric with `task='binary'` for binary classification problems: `metric = Accuracy(task='binary')`
Import specific metrics from their respective submodules: `from torchmetrics.classification import Accuracy`
Ensure that the metric and all input tensors are moved to the same device using `.to(device)` before computation: `device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')` `metric = Accuracy(task='binary').to(device)` `preds = preds.to(device)` `target = target.to(device)`