

Github GitHub - PyTorchLightning/metrics: Machine learning metrics for distribut...
source link: https://github.com/PyTorchLightning/metrics
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Installation
Simple installation from PyPI
pip install torchmetrics
Other installions
What is Torchmetrics
TorchMetrics is a collection of 25+ 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:
import torch # import our library import torchmetrics # initialize metric metric = torchmetrics.Accuracy() n_batches = 10 for i in range(n_batches): # simulate a classification problem preds = torch.randn(10, 5).softmax(dim=-1) target = torch.randint(5, (10,)) # metric on current batch acc = metric(preds, target) print(f"Accuracy on batch {i}: {acc}") # metric on all batches using custom accumulation acc = metric.compute() print(f"Accuracy on all data: {acc}")
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 implement the following methods:
class MyAccuracy(Metric): def __init__(self, dist_sync_on_step=False): # call `self.add_state`for every internal state that is needed for the metrics computations # dist_reduce_fx indicates the function that should be used to reduce # state from multiple processes super().__init__(dist_sync_on_step=dist_sync_on_step) self.add_state("correct", default=torch.tensor(0), dist_reduce_fx="sum") self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum") def update(self, preds: torch.Tensor, target: torch.Tensor): # update metric states preds, target = self._input_format(preds, target) assert preds.shape == target.shape self.correct += torch.sum(preds == target) self.total += target.numel() def compute(self): # compute final result return self.correct.float() / self.total
Functional metrics
Similar to torch.nn
, most metrics have both a module-based and a functional version.
The functional versions are simple python functions that as input take torch.tensors and return the corresponding metric as a torch.tensor.
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.accuracy(preds, target)
Implemented metrics
And many more!
Contribute!
The lightning + torchmetric 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 Slack to get help becoming a contributor!
Community
For help or questions, join our huge community on Slack!
Citations
We’re excited to continue the strong legacy of opensource software and have been inspired over the years by Caffee, Theano, Keras, PyTorch, torchbearer, ignite, sklearn and fast.ai. When/if a paper is written about this, we’ll be happy to cite these frameworks and the corresponding authors.
License
Please observe the Apache 2.0 license that is listed in this repository. In addition the Lightning framework is Patent Pending.
Recommend
-
76
README.md Node exporter
-
28
README.md
-
11
Java Monitoring Tools & Metrics Dive into Java monitoring and learn how to keep peak application performance To keep your users happy – and business going – your Java app needs to be up and running...
-
4
Machine Learning With ML.NET – Evaluation Metrics Apr 12, 2021 | .NET, AI,
-
10
Collection of tasks for fast prototyping, baselining, finetuning and solving problems with deep learning Installation •
-
7
How universal API management can drive digital success for the hybrid, distributed enterprise
-
9
Model Evaluation Metrics for Machine Learning Algorithms Reading Time: 6 minutes When you build any Machine Learning model...
-
5
SAP HANA Cloud Machine Learning Challenge “I quit!” – understanding metrics 2 12 421 I partici...
-
9
Essential Metrics to Boost Support for YourCybersecurity Learning Program – Part 1 Aug 21, 2023
-
13
Essential Types of Metrics to Boost Support forYour Cybersecurity Learning Program – Part 2 Aug 21, 2023
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK