这篇文章主要介绍Pytorch如何实现计算分类器准确率,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
分类器平均准确率计算:
correct = torch.zeros(1).squeeze().cuda()
total = torch.zeros(1).squeeze().cuda()
for i, (images, labels) in enumerate(train_loader):
images = Variable(images.cuda())
labels = Variable(labels.cuda())
output = model(images)
prediction = torch.argmax(output, 1)
correct += (prediction == labels).sum().float()
total += len(labels)
acc_str = 'Accuracy: %f'%((correct/total).cpu().detach().data.numpy())
分类器各个子类准确率计算:
correct = list(0. for i in range(args.class_num))
total = list(0. for i in range(args.class_num))
for i, (images, labels) in enumerate(train_loader):
images = Variable(images.cuda())
labels = Variable(labels.cuda())
output = model(images)
prediction = torch.argmax(output, 1)
res = prediction == labels
for label_idx in range(len(labels)):
label_single = label[label_idx]
correct[label_single] += res[label_idx].item()
total[label_single] += 1
acc_str = 'Accuracy: %f'%(sum(correct)/sum(total))
for acc_idx in range(len(train_class_correct)):
try:
acc = correct[acc_idx]/total[acc_idx]
except:
acc = 0
finally:
acc_str += '\tclassID:%d\tacc:%f\t'%(acc_idx+1, acc)
pytorch的优点
1.PyTorch是相当简洁且高效快速的框架;2.设计追求最少的封装;3.设计符合人类思维,它让用户尽可能地专注于实现自己的想法;4.与google的Tensorflow类似,FAIR的支持足以确保PyTorch获得持续的开发更新;5.PyTorch作者亲自维护的论坛 供用户交流和求教问题6.入门简单
以上是“Pytorch如何实现计算分类器准确率”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注天达云行业资讯频道!