Pytorch Bug解决:RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

  • 编程环境
  • Bug描述
  • bug分析
  • 解决方法

编程环境

Python:3.9
Pytorch:1.11.0

Bug描述

Traceback (most recent call last):File "E:\Anaconda\envs\torch_c_13\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_codeexec(code_obj, self.user_global_ns, self.user_ns)File "<ipython-input-2-18bbaf295f9c>", line 1, in <module>runfile('E:/Code/AEs by PyTorch/AEsingle_train_test_temp.py', wdir='E:/Code/AEs by PyTorch')File "E:\SoftWare\PyCharm\PyCharm 2021.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfilepydev_imports.execfile(filename, global_vars, local_vars)  # execute the scriptFile "E:\SoftWare\PyCharm\PyCharm 2021.2.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfileexec(compile(contents+"\n", file, 'exec'), glob, loc)File "E:/Code/AEs by PyTorch/AEsingle_train_test_temp.py", line 205, in <module>train_ae_x_h2 = AEtrain(AEmodel2, train_ae_x_h1, 10, "AEmodel2")File "E:/Code/AEs by PyTorch/AEsingle_train_test_temp.py", line 95, in AEtrainloss.backward()File "E:\Anaconda\envs\torch_c_13\lib\site-packages\torch\tensor.py", line 195, in backwardtorch.autograd.backward(self, gradient, retain_graph, create_graph)File "E:\Anaconda\envs\torch_c_13\lib\site-packages\torch\autograd\__init__.py", line 99, in backwardallow_unreachable=True)  # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [784, 512]], which is output 0 of TBackward, is at version 3; expected version 2 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

梯度计算所需的变量之一已被 inplace 操作修改。

bug分析

查看报错信息发现是loss.backward()部分报错,即在根据损失计算某个变量反向传播的梯度时,该变量被inplace操作修改了,即被原地修改,而不是将变化后的值赋给一个新的变量(类似于 a += 1 的操作)。因为反向传播遵循的是链式求导法则,这种原地修改的操作有时会使得模型计算梯度时找不到依赖的变量,从而导致梯度信息丢失或恶意篡改。

解决方法

  • 找到网络模型中的 inplace 操作,将inplace=True改成 inplace=False,例如torch.nn.ReLU(inplace=False)
  • 将代码中的“a+=b”之类的操作改为“c = a + b”
  • 将loss.backward()函数内的参数retain_graph值设置为True, loss.backward(retain_graph=True),如果retain_graph设置为False,计算过程中的中间变量使用完即被释放掉。

注意:所以大家在平常写代码的时候一定要养成良好的习惯,不乱用变量,注意代码的简洁性和高效性。

Pytorch Bug解决:RuntimeError:one of the variables needed for gradient computation has been modified相关推荐

  1. PyTorch报错“RuntimeError: one of the variables needed for gradient computation has been modified by……”

    1 问题描述 今天在写作DeepLabV3+的代码时,遇到了一个问题, 程序报错: RuntimeError: one of the variables needed for gradient com ...

  2. 【Pytorch】RuntimeError: one of the variables needed for gradient computation has been modified by

    0.前言 最近在用Pytorch训练网络的时候出现了一个奇怪的错误,找了很久在偶然间试出了解决方法,因此记录一下,以作备忘.但具体原理还不是很清楚,也请各位大佬指教. 1.错误 RuntimeErro ...

  3. RuntimeError: one of the variables needed for gradient computation has been modified by an inplace

    报错: RuntimeError: one of the variables needed for gradient computation has been modified by an inpla ...

  4. 排坑日记1:RuntimeError: one of the variables needed for gradient computation has been modified

    问题描述 在使用Pytorch复现DeepFill V1时,报如下错: RuntimeError: one of the variables needed for gradient computati ...

  5. RuntimeError: one of the variables needed for gradient computation has been modified by an inplace o

    RuntimeError: one of the variables needed for gradient computation has been modified by an inplace o ...

  6. 【完美解决】RuntimeError: one of the variables needed for gradient computation has been modified by an inp

    正文在后面,往下拉即可~~~~~~~~~~~~ 欢迎各位深度学习的小伙伴订阅的我的专栏 Pytorch深度学习·理论篇+实战篇(2023版)专栏地址:

  7. 解决一个GAN训练过程中的报错:one of the variables needed for gradient computation has been modified by an inplace

    跑一个GAN DEMO , 运行时出错. 出错代码: RuntimeError: one of the variables needed for gradient computation has be ...

  8. one of the variables needed for gradient computation has been modified by an inplace operation

    参考:https://www.cnblogs.com/liangzp/p/9207979.html 使用一个pytorch写的模型时,报错:RuntimeError:one of the variab ...

  9. 错误处理:one of the variables needed for gradient computation has been modified by inplace operation

    RuntimeError: one of the variables needed for gradient computation has been modified by an inplace o ...

最新文章

  1. 二维平面内无人机的路径规划——势场法-改进
  2. iOS 导航栏实现总结
  3. android c博客园,android上进行c/C 开发测试(转) - 奋进 - 博客园
  4. 不同的核函数对支持向量机分类性能的影响
  5. 推荐8个实用精美的在线网站,珍藏多年!
  6. ADB各种操作指令详解大汇总
  7. Windows10常用Windows自检方法
  8. Vue中的自定义指令
  9. TU-768与IT180A对比分析报告
  10. python递归排列组合_Python 排列组合
  11. Java调用ApacheOpenOffice将Word转PDF
  12. Sawin软件研发之窗:《南郭先生新论》 - 使用 Google 工具栏进行发送
  13. 2022制冷与空调设备安装修理特种作业证考试题库及答案
  14. 数据结构与算法(一) - 常用容器的了解
  15. 智联招聘中申请职位之后你根本不知道你申请的职位是啥,而且不想说...
  16. 笔记本分享热点wifi给手机上网
  17. java ieee754_IEEE754浮点数
  18. 7.25 10figting!
  19. Python爬虫之四:使用BeautifulSoup爬取微博热搜
  20. oracle数据库system01,system01坏块的问题

热门文章

  1. Altium Designer如何打印清晰的底层丝印
  2. 牛客P19836 裴蜀定理+莫比乌斯反演+杜教筛
  3. 天堂2单机启动不了mysql_单机天堂2运行不了 求助
  4. 浅谈TC8数据链路层测试
  5. 一分钟了解EPON光收发模块
  6. HMS Core助力《天谕》,打造高性能手游解决方案
  7. php的CI框架的配置文件的方法
  8. Edge 插件开发 简单示例
  9. 程序员市场饱和了吗?未来5年前景如何?工资会下降么?
  10. 使用pip命令安装库时,一直出现让你更新pip的情况,怎么处理?