1、计算正确识别数量(识别率)的技巧(以numpy为例)
import numpy as np
acc = 0 # 初始化
label = np.array([1,0,1,0,1]) # 目标标签
preds = np.array([1,1,1,1,1]) # 预测结果
acc = (label == preds).sum() # 可以直接输出,写在循环中可以使用 '+=' 代替 '='
print(acc)
实际应用在pytorch中时:
with torch.no_grad():
for x,y in test_loader:
x = x.to(device)
y = y.to(device)
preds = model(x)
predictions = preds.max(1).indices # dim = 1
num_correct += (predictions == y).sum()