torchmetrics
Machine learning metrics for distributed, scalable PyTorch applications.
What is torchmetrics?
Machine learning metrics for distributed, scalable PyTorch applications.
Overview of torchmetrics
What it is
TorchMetrics is a collection of over 100 PyTorch metric implementations and an API for creating custom metrics. It is designed for distributed, scalable PyTorch applications. The library provides a standardized interface to increase reproducibility, reduces boilerplate code, and offers automatic accumulation over batches. Metrics are optimized for distributed training and include automatic synchronization between multiple devices. TorchMetrics can be used with any PyTorch model or with PyTorch Lightning for additional features such as automatic device placement and native logging support.
Key features
- Standardized interface for metrics to improve reproducibility.
- Automatic accumulation of metric states over multiple batches.
- Automatic synchronization of metric states across multiple devices (CPU, single GPU, multi-GPU).
- Module-based metrics that behave like PyTorch modules, with internal states for accumulation.
- Functional versions of most metrics for simple, stateless computations.
- Support for custom metrics by subclassing the
torchmetrics.Metricclass and implementingupdateandcomputemethods.
Use cases
- Evaluating classification, regression, segmentation, detection, and other machine learning models.
- Tracking metrics during distributed training across multiple GPUs or nodes.
- Implementing custom evaluation metrics for research or production pipelines.
- Integrating with PyTorch Lightning for streamlined logging and device management.
- Computing metrics for audio, image, text, and multimodal domains.
Getting started
Install from PyPI: bash pip install torchmetrics Install using conda: bash conda install -c conda-forge torchmetrics Install using uv: bash uv add torchmetrics Install from source: bash pip install git+https://github.com/Lightning-AI/torchmetrics.git@release/stable Install extra dependencies for specialized metrics: bash pip install torchmetrics[audio] pip install torchmetrics[image] pip install torchmetrics[text] pip install torchmetrics[all]
License and pricing
The license is Apache-2.0. The project is open source and free to use.
Based on the project's public README and repository metadata.