【发布时间】:2021-09-25 21:13:49
【问题描述】:
我想复制代码here,在 Google Colab 中运行时出现以下错误?
ImportError: 无法从其中导入名称“zero_gradients” 'torch.autograd.gradcheck' (/usr/local/lib/python3.7/dist-packages/torch/autograd/gradcheck.py)
有人可以帮我解决这个问题吗?
【问题讨论】:
我想复制代码here,在 Google Colab 中运行时出现以下错误?
ImportError: 无法从其中导入名称“zero_gradients” 'torch.autograd.gradcheck' (/usr/local/lib/python3.7/dist-packages/torch/autograd/gradcheck.py)
有人可以帮我解决这个问题吗?
【问题讨论】:
这似乎使用的是非常旧的 PyTorch 版本,该功能本身不再可用。但是,如果您查看this commit,您将看到zero_gradients 的实现。它所做的只是将输入的梯度归零:
def zero_gradients(i):
for t in iter_gradients(i):
t.zero_()
那么zero_gradients(x)应该和x.zero_grad()一样,也就是当前的API,假设x是nn.Module!
或者只是:
if x.grad is not None:
x.grad.zero_()
【讨论】: