Are `model.eval` and `with torch.no_grad():` equivalent?
Apr 3, 11:30am
In PyTorch, model.eval() and with torch.no_grad(): are not equivalent, but they are often used together for model evaluation or inference. Here’s how they differ and how they interact:
1. model.eval()
Purpose: Sets the model to evaluation mode.
What it does:
Disables layers like dropout and batch normalization, which behave differently during training vs. evaluation.
Affects the model’s internal state (e.g., running statistics for batch norm).
When to use: Before running inference or evaluation, to ensure the model behaves correctly.