No. They aren’t the same. I’ve copied a reply from another user that explains the difference.


These two have different goals:


model.eval() will notify all your layers that you are in eval mode, that way, batchnorm or dropout layers will work in eval mode instead of training mode.


torch.no_grad() impacts the autograd engine and deactivates it. It will reduce memory usage and speed up computations but you won’t be able to backprop (which you don’t want in an eval script).




In essence, simply using torch.no_grad() will not impact layers such as dropout or batchnorm, they will still be used during inference.