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’)

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

音程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值