Evaluation mode switches a PyTorch model from training behavior to inference or inspection behavior. Use it when measuring held-out performance, generating samples, detecting objects, or examining predictions and attention. section 2.4.4 section 7.5.2 section 8.5
Call model.eval() before evaluation. In a training loop, pair it with torch.no_grad(): eval mode selects evaluation behavior, while no_grad prevents gradient tracking and model alteration during inspection. section 2.4.4 section 8.5
Common uses include:
- Evaluate a trained network on a test or validation DataLoader, recording metrics such as accuracy and F1. section 2.4.4
- Generate autoregressive sequences after processing a seed and feeding each sampled prediction into the next time step. section 7.5.2
- Run a pretrained Faster R-CNN detector on normalized images and inspect scored object predictions. section 8.5
- Inspect sequence-to-sequence translations and visualize attention maps for individual test examples. section 11.4.2
Evaluation inside a training loop
model = model.eval()\nwith torch.no_grad():\n run_epoch(model, optimizer, test_loader, loss_func,\n device, results, score_funcs, prefix="test")
autoReg_model = autoReg_model.eval() sampling = torch.zeros((1, 500), dtype=torch.int64, device=device)
Shows evaluation mode used before allocating a sampling tensor for autoregressive generation. section 7.5.2
seq2seq = seq2seq.eval().cpu()
def results(indx):
eng_x, french_y = test_dataset[indx]
eng_str = " ".join([indx2word[i] for i in eng_x.cpu().numpy()])
french_str = " ".join([indx2word[i] for i in french_y.cpu().numpy()])
print("Input: ", eng_str)
print("Target: ", french_str)
with torch.no_grad():
preds, attention = seq2seq(eng_x.unsqueeze(0))
p = torch.argmax(preds, dim=2)
pred_str = " ".join([indx2word[i] for i in p[0,:].cpu().numpy()])
print("Predicted: ", pred_str)
plot_heatmap(eng_str.split(" "), pred_str.split(" "),
attention.T.cpu().numpy())
Shows evaluation mode, CPU placement, no_grad inference, prediction extraction, and attention visualization for sequence-to-sequence results. section 11.4.2


