Save and restore autograd enabled state

2019-08-18 04:39发布

问题:

Is there a way of saving and restoring the current state of autograd?

eg:

saved_state = torch.autograd.get_grad_enabled()  # How do I do this?
torch.autograd.set_grad_enabled(temporary_state)
# do things for great justice
torch.autograd.set_grad_enabled(saved_state)

回答1:

The Source code for torch.autograd.grad_mode hints at a solution:

torch.is_grad_enabled()

It seems that this function is currently undocumented.



标签: pytorch