From torch.autograd.gradcheck import zero_gradients


This error comes after the upgrade of my PyTorch version from 1.8 to 1.9.0.

When using this line:

from torch.autograd.gradcheck import zero_gradients, I get this error message: ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck'

The command:


is used.

What is the new command equivalent in PyTorch 1.9.0?

Unfortunately it was removed as part of a refactor to the code. I’m not sure there’s an equivalent you can just import, but since its relatively simple, you might want to just replicate it in your own code.
This was the original function:

def zero_gradients(x):
    if isinstance(x, torch.Tensor):
        if x.grad is not None:
    elif isinstance(x,
        for elem in x: