Reputation: 1109
I want to replicate the code here, and I get the following error while running in Google Colab?
ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck' (/usr/local/lib/python3.7/dist-packages/torch/autograd/gradcheck.py)
Can someone help me with how to solve this?
Upvotes: 6
Views: 7220
Reputation: 4367
My error was:
Traceback (most recent call last): File "make_figures/make_recon_figure_multires.py", line 23, in from dlutils.pytorch import count_parameters File "/home/ubuntu/.local/lib/python3.8/site-packages/dlutils/init.py", line 32, in from dlutils.pytorch.jacobian import jacobian File "/home/ubuntu/.local/lib/python3.8/site-packages/dlutils/pytorch/init.py", line 16, in from dlutils.pytorch.jacobian import jacobian File "/home/ubuntu/.local/lib/python3.8/site-packages/dlutils/pytorch/jacobian.py", line 2, in from torch.autograd.gradcheck import zero_gradients ImportError: cannot import name 'zero_gradients' from 'torch.autograd.gradcheck' (/home/ubuntu/.local/lib/python3.8/site-packages/torch/autograd/gradcheck.py)
I basically went to the location:
cd /home/ubuntu/.local/lib/python3.8/site-packages/dlutils/pytorch/
and removed the line:
from torch.autograd.gradcheck import zero_gradients
and added the function to the top:
def zero_gradients(i):
for t in iter_gradients(i):
t.zero_()
Upvotes: 0
Reputation: 40708
This seems like it's using a very old version of PyTorch, the function itself is not available anymore. However, if you look at this commit, you will see the implementation of zero_gradients
. What it does is simply zero out the gradient of the input:
def zero_gradients(i):
for t in iter_gradients(i):
t.zero_()
Then zero_gradients(x)
should be the same as x.zero_grad()
, which is the current API, assuming x
is a nn.Module
!
Or it's just:
if x.grad is not None:
x.grad.zero_()
Upvotes: 6