如何在pytorch中找到分类模型结果的得分概率?

How to find score probability of classification model result in pytorch?

我是 pytorch 的新手,我已经训练了一个图像 classification 模型,当我用图像测试模型时,如果我想获得该图像的预测概率,我只会得到标签class 我怎样才能得到它?

 test_image = test_image_tensor.view(1,3,300,300)
 model.eval()
 out = model(test_image)
 ps = torch.exp(out)
 topk,topclass = ps.topk(1,dim=1)
 class_name = idx_to_class[topclass.cpu().numpy()[0][0]]

我正在使用上面的代码进行预测,它只给出 class 名称,如果我想要预测的标签分数,我该如何获得它?

如有任何帮助或建议,我们将不胜感激

概率是预测的 softmax:

class_prob = torch.softmax(out, dim=1)
# get most probable class and its probability:
class_prob, topclass = torch.max(class_prob, dim=1)
# get class names
class_name = idx_to_class[topclass.cpu().numpy()[0][0]]