【问题标题】:Imorting zero_gradients from torch.autograd.gradcheck从 torch.autograd.gradcheck 导入零梯度
【发布时间】: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)

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    这似乎使用的是非常旧的 PyTorch 版本,该功能本身不再可用。但是,如果您查看this commit,您将看到zero_gradients 的实现。它所做的只是将输入的梯度归零:

    def zero_gradients(i):
        for t in iter_gradients(i):
            t.zero_()
    

    那么zero_gradients(x)应该和x.zero_grad()一样,也就是当前的API,假设xnn.Module

    或者只是:

    if x.grad is not None:
        x.grad.zero_()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 2020-06-12
      • 2020-10-30
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多