Check if grad is enabled

I’m looking for a way to check if gradient calculation is enabled or not from within my net. Is there a way to do this from within PyTorch? Something like: torch.check_grad_enabled → bool…


You can check it by accessing torch.Tensor's requires_grad attribute, which returns True if gradient should be calculated on that Tensor. Note that it has a contagious behavior, that is, if A.requires_grad=True for some Tensor A, all Tensors computed from A have the requires_grad attribute True.

1 Like