栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

TorchMetrics的安装以及使用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TorchMetrics的安装以及使用

文章目录
      • 安装
      • 使用

安装

官方网站:https://torchmetrics.readthedocs.io/en/stable/。

安装方法:

conda install -c conda-forge torchmetrics
使用

大家如果用过sklearn.metrics,那么就会很容易使用这个,其实我一开始都是一直使用前者的,但是前者只支持numpy以及list,所以每次使用sklearn的使用,我们需要将tensor从gpu搬到cpu,然后再由tensor转化为numpy,现在的话,TorchMetrics则不会需要啦。

import torchmetrics
import torch
preds = torch.randn(2, 2).softmax(dim=-1)#2个样本,每一个样本都是二分类问题
target = torch.randint(2, (2,))#真实标签。
print(preds)
print(target)


从上面我们可以看到,两个样本都是输出第0个类别的概率高,标签也是第0个类别,那么准确率为100%,下面验证一下:

acc = torchmetrics.functional.accuracy(preds, target)
print(acc)

tensor(1.)

我们发现,TorchMetrics直接支持tensor计算,其实其也支持gpu上直接计算,如下:

device=torch.device("cuda:1")
acc = torchmetrics.functional.accuracy(preds.to(device), target.to(device))
print(acc)

tensor(1., device=‘cuda:1’)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835593.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号