验证集需要torch.no_grad()方法:
with torch.no_grad():
for bt_idx, (imgs, labels) in enumerate(val_loader):
imgs = imgs.cuda()
labels = labels.cuda()
val_outputs = model(imgs)
val_loss += criterion(val_outputs, labels)
_,val_prediction = val_outputs.max(1)
val_correct += val_prediction.eq(labels.cuda()).sum().item()
val_acc = val_correct/(64*(bt_idx+1))
参考:
- https://blog.csdn.net/ncc1995/article/details/91126066
- https://blog.csdn.net/xijuezhu8128/article/details/86594478