Pytorch 如何得到预测概率?
此代码从模型中获取Pytorch 如何得到预测概率?,pytorch,Pytorch,此代码从模型中获取1或0值 如果我想得到预测的概率 我应该换哪一行 from torch.autograd import Variable results = [] #names = [] with torch.no_grad(): model.eval() print('===============================================start') for num, data in enumerate(test_loader):
1
或0
值如果我想得到预测的概率
我应该换哪一行
from torch.autograd import Variable
results = []
#names = []
with torch.no_grad():
model.eval()
print('===============================================start')
for num, data in enumerate(test_loader):
#print(num)
print("=====================================================")
imgs, label = data
imgs,labels = imgs.to(device), label.to(device)
test = Variable(imgs)
output = model(test)
#print(output)
ps = torch.exp(output)
print(ps)
top_p, top_class = ps.topk(1, dim = 1)
results += top_class.cpu().numpy().tolist()
model = models.resnet50(pretrained=True)
model.fc = nn.Linear(2048, num_classes)
model.cuda()
模型通常输出原始预测逻辑。若要将它们转换为概率,应使用函数
import torch.nn.功能与nnf相同
# ...
prob=nnf.softmax(输出,尺寸=1)
顶部,顶部等级=概率顶部(1,尺寸=1)
新变量
top\u p
应该给出前k类的概率。从top\u p,top\u class=ps.topk(1,dim=1)
printtop\u p
它必须包含概率top\p看起来像张量([15.0558],[225.5229],[204.3323],[124.6181],[212.8658],[239.8973],[188.1104], [ 13.3096], [146.6426], [ 12.6521], [232.5268], [ 73.8362], [209.5141], [307.2397], [219.1580], [130.2537]它需要更多信息。添加有问题的模型
网络架构。然后有人会提供帮助。你为什么torch.exp
你的输出
s?