torchmetrics
PyTorch native Metrics
What it is and what it does
Torchmetrics is a PyTorch library that standardizes metric computation for machine learning models, particularly in distributed and multi-device training scenarios. It provides a collection of pre-built metrics (accuracy, F1, precision, recall, and many others) alongside an API for defining custom metrics. The library handles the boilerplate of accumulating metric state across batches and synchronizing results across multiple GPUs or nodes automatically, so you can focus on model logic rather than metric plumbing.
The package works as a PyTorch module—you instantiate a metric, move it to your device, call it with predictions and targets in a training loop, and it tracks state internally. It supports single-GPU, multi-GPU with DDP, and multi-node setups without code changes. It also integrates tightly with PyTorch Lightning, where metrics are automatically placed on the correct device and logged with minimal extra code.
Use it for:
- Evaluate classification models on multi-GPU training runs without manually synchronizing accuracy or F1 scores across devices.
- Track cumulative metrics over an entire epoch or dataset without storing all predictions in memory.
- Implement custom metrics by subclassing Metric and defining update() and compute() methods for domain-specific evaluation.
- Log metrics to PyTorch Lightning training runs with automatic device placement and distributed synchronization.
- Benchmark model performance reproducibly using a standardized metric interface across different training frameworks.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Torchmetrics provides a collection of PyTorch metrics implementations with automatic batch accumulation and multi-device synchronization, designed for distributed training workflows.
Yes. Torchmetrics is a mature, actively maintained library (2458 stars, recent releases) with low install friction and no known vulnerabilities. It solves a real problem—metric computation in distributed PyTorch training—with a clean API and tight Lightning integration. Use it if you train models on multiple GPUs or need reproducible, standardized metric evaluation; skip it only if you compute metrics outside PyTorch or have highly specialized evaluation logic.
Install
torchmetrics on PyPI
pip
pip install torchmetricsuv
uv add torchmetricspoetry
poetry add torchmetricsInstalling torchmetrics
Before you install
Low install friction with a pure-Python wheel distribution. Active maintenance with recent releases and 2458 repository stars. Requires torch and three lightweight dependencies (numpy, packaging, lightning-utilities).
License in practice
Apache-2.0 permissive license allows commercial and private use with minimal restrictions—standard for ML tooling.
Quickstart
pip install torchmetrics
import torch
import torchmetrics
metric = torchmetrics.classification.Accuracy(task="multiclass", num_classes=5)
device = "cuda" if torch.cuda.is_available() else "cpu"
metric.to(device)
preds = torch.randn(10, 5).softmax(dim=-1).to(device)
target = torch.randint(5, (10,)).to(device)
acc = metric(preds, target)
Requires PyTorch (torch) to be installed; Python 3.10 or later.
Verify before relying
- Whether the 100+ built-in metrics cover your specific evaluation needs
- Performance overhead of automatic synchronization in very large distributed setups
- Compatibility with custom PyTorch training loops vs. PyTorch Lightning integration benefits
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — numpy, packaging, torch, lightning-utilities |
| Maintenance | actively maintained — 158 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 12,257,228/month — #1,331 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: torchmetrics-1.9.0-py3-none-any.whl
Keywords: deep learning, machine learning, pytorch, metrics, AI
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
torchevalTorchEval provides a collection of PyTorch…
permissive · top 15,000 on PyPI
entmaxEntmax provides PyTorch implementations of…
permissive · top 15,000 on PyPI
torchrunxtorchrunx distributes PyTorch training…
copyleft · top 15,000 on PyPI
ema-pytorchMaintains an exponential moving average (EMA)…
permissive · top 15,000 on PyPI
nv-one-logger-pytorch-lightning-integrationAdds training telemetry collection to Lightning…
permissive · top 15,000 on PyPI
lightningLightning is a framework that organizes PyTorch…
permissive · top 5,000 on PyPI
motmetricsComputes multiple object tracker (MOT)…
permissive · top 15,000 on PyPI
spmd-typesProvides type annotations and runtime checking…
permissive · top 15,000 on PyPI
litdataLitData optimizes and streams large datasets…
permissive · top 15,000 on PyPI
torchPyTorch provides GPU-accelerated tensor…
permissive · top 1,000 on PyPI