Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

torchmetrics

> DevOps
Open source

Machine learning metrics for distributed, scalable PyTorch applications.

2.5K stars0 likes2 views
WebsiteGitHub

About

Machine learning metrics for distributed, scalable PyTorch applications.

Looking for GPUs?

Over 340,000 developers use Lightning Cloud - purpose-built for PyTorch and PyTorch Lightning.

  • GPUs from $0.19.
  • Clusters: frontier-grade training/inference clusters.
  • AI Studio (vibe train): workspaces where AI helps you debug, tune and vibe train.
  • AI Studio (vibe deploy): workspaces where AI helps you optimize, and deploy models.
  • Notebooks: Persistent GPU workspaces where AI helps you code and analyze.
  • Inference: Deploy models as inference APIs.

Installation

Simple installation from PyPI

bash
pip install torchmetrics
Other installations

Install using conda

bash
conda install -c conda-forge torchmetrics

Install using uv

bash
uv add torchmetrics

Pip from source

bash
# with git
pip install git+https://github.com/Lightning-AI/torchmetrics.git@release/stable

Pip from archive

bash
pip install https://github.com/Lightning-AI/torchmetrics/archive/refs/heads/release/stable.zip

Extra dependencies for specialized metrics:

bash
pip install torchmetrics[audio]
pip install torchmetrics[image]
pip install torchmetrics[text]
pip install torchmetrics[all]  # install all of the above

Install latest developer version

bash
pip install https://github.com/Lightning-AI/torchmetrics/archive/master.zip

What is TorchMetrics

TorchMetrics is a collection of 100+ PyTorch metrics implementations and an easy-to-use API to create custom metrics. It offers:

  • A standardized interface to increase reproducibility
  • Reduces boilerplate
  • Automatic accumulation over batches
  • Metrics optimized for distributed-training
  • Automatic synchronization between multiple devices

You can use TorchMetrics with any PyTorch model or with PyTorch Lightning to enjoy additional features such as:

  • Module metrics are automatically placed on the correct device.
  • Native support for logging metrics in Lightning to reduce even more boilerplate.

Using TorchMetrics

Module metrics

The module-based metrics contain internal metric states (similar to the parameters of the PyTorch module) that automate accumulation and synchronization across devices!

  • Automatic accumulation over multiple batches
  • Automatic synchronization between multiple devices
  • Metric arithmetic

This can be run on CPU, single GPU or multi-GPUs!

For the single GPU/CPU case:

…

Module metric usage remains the same when using multiple GPUs or multiple nodes.

Example using DDP
…

Implementing your own Module metric

Implementing your own metric is as easy as subclassing an torch.nn.Module. Simply, subclass torchmetrics.Metric and just implement the update and compute methods:

…

Functional metrics

Similar to torch.nn, most metrics have both a module-based and functional version. The functional versions are simple python functions that as input take torch.tensors and return the corresponding metric as a torch.tensor.

python
import torch

# import our library
import torchmetrics

# simulate a classification problem
preds = torch.randn(10, 5).softmax(dim=-1)
target = torch.randint(5, (10,))

acc = torchmetrics.functional.classification.multiclass_accuracy(
    preds, target, num_classes=5
)

Covered domains and example metrics

In total TorchMetrics contains 100+ metrics, which covers the following domains:

  • Audio
  • Classification
  • Detection
  • Information Retrieval
  • Image
  • Multimodal (Image-Text-3D Talking Heads)
  • Nominal
  • Regression
  • Segmentation
  • Text

Each domain may require some additional dependencies which can be installed with pip install torchmetrics[audio], pip install torchmetrics['image'] etc.

Additional features

Plotting

Visualization of metrics can be important to help understand what is going on with your machine learning algorithms. Torchmetrics have built-in plotting support (install dependencies with pip install torchmetrics[visual]) for nearly all modular metrics through the .plot method. Simply call the method to get a simple visualization of any metric!

…

For examples of plotting different metrics try running this example file.

Contribute!

The lightning + TorchMetrics team is hard at work adding even more metrics. But we're looking for incredible contributors like you to submit new metrics and improve existing ones!

Join our Discord to get help with becoming a contributor!

Community

For help or questions, join our huge community on Discord!

Citation

We’re excited to continue the strong legacy of open source software and have been inspired over the years by Caffe, Theano, Keras, PyTorch, torchbearer, ignite, sklearn and fast.ai.

If you want to cite this framework feel free to use GitHub's built-in citation option to generate a bibtex or APA-Style citation based on this file (but only if you loved it ).

License

Please observe the Apache 2.0 license that is listed in this repository. In addition, the Lightning framework is Patent Pending.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Pythonanalysesdata-sciencedeep-learningmachine-learning

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理