【python错误】Pytorch1.9 ImportError: cannot import name ‘zero_gradients‘

错误:Pytorch1.9 ImportError: cannot import name ‘zero_gradients’

错误提示

错误提示:

ImportError: cannot import name ‘zero_gradients’ from ‘torch.autograd.gradcheck’ (/root/miniconda3/envs/d2l/lib/python3.9/site-packages/torch/autograd/gradcheck.py)

原因:

pytorch版本更新后,没有对应的方法函数

解决:

将~/miniconda3/envs/d2l/lib/python3.9/site-packages/advertorch/attacks/fast_adaptive_boundary.py
中的
from torch.autograd.gradcheck import zero_gradients
删掉,加入

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

参考:https://zhuanlan.zhihu.com/p/420312739

相关推荐

  1. PythonPyTorch

    2024-02-04 09:32:03       33 阅读
  2. HJ19错误简单记录

    2024-02-04 09:32:03       15 阅读
  3. HJ19 简单错误记录

    2024-02-04 09:32:03       14 阅读
  4. Linux CUDA11.6 Python3.8 安装pytorch-geometric

    2024-02-04 09:32:03       45 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-04 09:32:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-04 09:32:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-04 09:32:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-04 09:32:03       20 阅读

热门阅读

  1. 【大厂AI课学习笔记】1.4 算法的进步(5)关于GPU

    2024-02-04 09:32:03       30 阅读
  2. 如何发布NPM包

    2024-02-04 09:32:03       31 阅读
  3. 如何在 Mac 上重置网络设置

    2024-02-04 09:32:03       29 阅读
  4. 【力扣经典面试题】274. H 指数

    2024-02-04 09:32:03       32 阅读
  5. [AIGC] Spring Gateway与 nacos 简介

    2024-02-04 09:32:03       27 阅读
  6. 20240203作业

    2024-02-04 09:32:03       29 阅读
  7. FollowYourPose 安装踩坑

    2024-02-04 09:32:03       29 阅读
  8. C语言——R/预处理详解

    2024-02-04 09:32:03       28 阅读
  9. 6-3 C. DS二叉树——二叉树之父子结点

    2024-02-04 09:32:03       34 阅读
  10. 如何让自己成为一个稀缺的人

    2024-02-04 09:32:03       29 阅读
  11. 【Springboot】单元测试Junit5应用

    2024-02-04 09:32:03       29 阅读