PyTorch 2 · Training and saving

Evaluate a model

Switch to evaluation mode and turn off gradients.

model.eval()
correct = 0
with torch.no_grad():
    for x, y in test_loader:
        preds = model(x.to(device)).argmax(dim=1)
        correct += (preds == y.to(device)).sum().item()

Official PyTorch 2 documentation

More training and saving commands

All training and saving commands · PyTorch 2 cheatsheet

Was this page helpful?