首页 > 编程知识 正文

pytorch参数和梯度可视化,混淆矩阵怎么画

时间:2023-05-04 10:10:43 阅读:12312 作者:4367

pytorch分类模型模糊矩阵的绘制与可视化Step 1.模糊矩阵的获取#首先定义一个分类数*分类数的模糊矩阵conf _ matrix=torch.zeros (emotion _ kinds, 使用Emotion_kinds(#torch.no_grad ),可以显著降低测试用例的GPU占有率with torch.no_grad ) : for step,(imgs )。 targets (in enumerate (test _ loader ) : # imgs : torch.size ([ 50,3,200, 200] ) torch.float tensor # target ts torch.long tensor一维较多,因此targets=targets.squeeze (# [ 50,1 ]---- [ 50 ] # ) imgs.type (,targets.shape,targets.type ) ) out=model ) imgs ) conf_matrix=confusion记录混淆矩阵参数

defconfusion_matrix(preds,labels,conf_matrix ) :preds=torch.argmax(preds,1 ) for p,tinzip ) preds, linzip t] =1 return conf_matrix我们的程序运行结束后,我们可以得到这次数据的模糊矩阵,然后计算其识别正确的个数和模糊矩阵的可视化。

从conf _ matrix=NP.array (conf _ matrix.CPU ) ) gpu到CPU,再到NP corrects=conf _ matrix.diagonal ) offset=0 测试集总数:) )、格式) int(NP.sum ) conf_matrix、test_num((print(conf_matrix ) ) Emotion的识别精度

Step 2.混淆矩阵的可视化在上边求出的混淆矩阵的可视化

#混淆矩阵Emotion=8#是具体的分类数,labels=['neutral '、' calm '、' happy '、' sad '、' angry '、' fearful ', 可以自己修改“disgust”的“surprised']#每种标签#显示数据PLT.imshow(conf_matrix, 在cmap=plt.cm.Blues )图中填写数量/概率信息thresh=conf _ matrix.max forxinrange (emotion _ kinds ) 3360 foryinrange emotion y ]请注意,y]info=info,垂直对齐=' center ',水平对齐=' center ', 避免color=' white ' ifinfothreshelse ' black ' (PLT.tight _ layout ) #图与PLT.yticks(range ) emotion_kinds )重叠

其他分类指标的获取,如F1得分、TP、TN、FP、FN、准确率、召回率等指标,尚未使用,我们将予以补充~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。