【Pytorch】RuntimeError: one of the variables needed for gradient computation has been modified by
0.前言
最近在用Pytorch训练网络的时候出现了一个奇怪的错误,找了很久在偶然间试出了解决方法,因此记录一下,以作备忘。但具体原理还不是很清楚,也请各位大佬指教。
1.错误
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [19, 24, 30]], which is output 0 of SoftmaxBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
2.查找错误
根据报错的提示显示出错的是模型反向传播的时候,即:
(policy_loss - dist_entropy * self.args.entropy_coef).backward()
但dist_entropy
和self.args.entropy_coef
都没有梯度,所以问题应该是在policy_loss
的梯度计算上,但检查了一下前向过程也并没有发现什么问题,于是按照错误提示加上了with torch.autograd.set_detect_anomaly(True)
这句,对于简单模型这句很容易加在模型开头,但我的模型稍微复杂一些,而且似乎到底放在哪也没有比较确定的说法,于是我将此句放在了模型更新的一开始,之后重新运行,在报错中出现了一个写在模型中的语句,也就是这句:
output = torch.cat(s_actions, dim=-2)
action_prob = torch.nn.functional.softmax(output, dim=-1).cpu()
看来错误应该就是出于这个地方了,这也和报错的SoftmaxBackward一致。
3.解决方法
找到了问题出处,下面就该解决了。查了一下似乎也没有我这种出现在SoftmaxBackward上的,查到的解决办法大致如下:
1.将relu
的inplace=True
改成False,但我的所有网络模块中的relu
函数都没有将inplace置为True,故此方法行不通;
2.还有是说由于模型中出现了类似+=
这样的操作,但在我仔细检查代码后,确定没有写这种,所以也只能放弃;
3.将Pytorch版本降级,其实这个确实是可以,我在服务器上的Pytorch1.4.0环境下,就没有出现这个错误,但是在本地Window环境下的Pytorch1.5.0版本环境就会出现这个错误,所以可能是和Pytorch版本有关?
4.但总是用低版本的也不是办法,于是在一通瞎试之后偶然发现,在softmax后面加上.clone()
后这个错误就神奇的消失了。最终将代码改为;
output = torch.cat(s_actions, dim=-2)
action_prob = torch.nn.functional.softmax(output, dim=-1).cpu().clone()
改完之后代码就可以运行了,具体原理还不是很清楚,希望有大神指教。
【Pytorch】RuntimeError: one of the variables needed for gradient computation has been modified by相关推荐
- 【完美解决】RuntimeError: one of the variables needed for gradient computation has been modified by an inp
正文在后面,往下拉即可~~~~~~~~~~~~ 欢迎各位深度学习的小伙伴订阅的我的专栏 Pytorch深度学习·理论篇+实战篇(2023版)专栏地址:
- 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 ...
- Pytorch Bug解决:RuntimeError:one of the variables needed for gradient computation has been modified
Pytorch Bug解决:RuntimeError: one of the variables needed for gradient computation has been modified b ...
- 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 ...
- 排坑日记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 ...
- 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 ...
- 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 ...
- 错误处理: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 ...
- 解决一个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 ...
最新文章
- 2021-10-20 Speaking Class
- 内联元素(display:inline-block)注意事项
- python生成器用法_理解python中生成器用法
- 用智能墨水打造超灵敏传感器 “写”到哪里感知就到哪里...
- python源文件的执行方式_python源程序执行的方式有哪几种
- Jenkins + Ansible + Gitlab之ansible篇
- 煤岩分析仪测定煤的镜质体反射率和煤显微组分
- 缥缈峰--JVM系列之内存区域
- CentOS 7 systemd添加自定义系统服务
- 元宇宙虚拟数字人系统
- 社交+小游戏方案来袭,全方位提升社交APP的活跃留存
- java达内项目_达内IT学院举办Java互联网架构师项目峰会
- 建模新手使用Maya的xGen功能后,角色毛发顺滑堪比使用海飞丝!
- Python 乱码原理及其解决办法
- Android获取NFC标签和NFC十进制16进制ID
- python100day day22 -Requests
- Docker多容器搭建LNMP
- Tensorflow object detection API 的环境配置
- 获取《使命召唤》模型及动画
- 修家谱花钱在哪里?从几年变成几个月,为什么现在修谱效率这么高?
热门文章
- Windows server 2012 安装 .NET Core 2.2.8 和 vc2015 杂谈
- linux挂载盘 程序执行没权限,mount挂载权限问题
- sql server2016 安装成功后无法连接服务器localhost的一种解决方法
- maven安装教程(超详细图解)
- 抖音推荐算法工程师提前批
- 单片机原理及应用 实验七 直流数字电压表设计
- 360浏览器的html在哪里,360浏览器在哪个文件夹里_360浏览器文件夹位置怎么打开-系统城...
- 插件推荐 | 小白必须懂得几款edge浏览器插件,用了可以提高效率!
- 何时何地看黄山落日晚霞
- maven命令指定打包模块