Skip to content

Instantly share code, notes, and snippets.

@Hanrui-Wang
Created July 25, 2019 01:15
Show Gist options
  • Save Hanrui-Wang/124514c1b4babb7f12ddcfd740a6957b to your computer and use it in GitHub Desktop.
Save Hanrui-Wang/124514c1b4babb7f12ddcfd740a6957b to your computer and use it in GitHub Desktop.
how to model eval and torch no_grad in the test set
# Test the model
model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment