Pytorch disable gradients globally

Is there a way to disable gradients globally ?

I typically evaluate my models in a jupyter notebook.

Instead of having to repeatedly use with torch.no_grad blocks, can I disable gradients for the entire notebook ?

As long as the evaluation in jupyter happens on the same thread you probably can use torch.set_grad_enabled(False).

1 Like