排坑日记1:RuntimeError: one of the variables needed for gradient computation has been modified
问题描述
在使用Pytorch复现DeepFill V1时,报如下错:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [512, 256, 5, 5]] is at version 6; expected version 5 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
报错提示行为生成器的反向传播部分
环境配置
python 3.6.13
pytorch 1.10.2
cuda 11.3
cudnn 8.0
解决过程
参考网上:
找到网络模型中的 inplace 操作,将inplace=True改成 inplace=False;
发现模型中根本就没有inplace操作或者 += 操作,不得已只能自己排坑。
首先根据报错提示内容,在生成器反向传播的上方添加:
torch.autograd.set_detect_anomaly(True)
但仍会报出如下错误:
RuntimeError: one of the variables needed for gradient computation has
been modified by an inplace operation: [torch.cuda.FloatTensor [512,
256, 5, 5]] is at version 6; expected version 5 instead. Hint: the
backtrace further above shows the operation that failed to compute its
gradient. The variable in question was changed in there or anywhere
later. Good luck!
多次debug无果后去找g_loss的定义
self.loss['g_loss'] = self.gan_loss_alpha * self.loss['g_loss']self.loss['g_loss'] = self.loss['g_loss'] + self.l1_loss_alpha * self.loss['recon']+ self.ae_loss_alpha * self.loss['ae_loss']
由上可得,g_loss由两部分组成:重构损失和自编码器损失。
将重构损失去掉,程序仍报相同错误,但将自编码器损失去掉后,程序顺利运行,说明问题的根源在于自编码器损失,定义如下:
但是将其去掉显然是不合理的,并且在进行反向传播的过程中,并没有改变ae_loss的值,只有计算,所以返回刚刚的代码中:
可以看到,在生成器反向传播前进行了网络参数更新,将之调整到生成器反向传播的下方,即:
困扰好久得问题得以解决,程序顺利跑通。
解决方案
原因: 在进行生成器网络反向传播梯度计算之前,先对判别器参数进行了更新,修改了某些值,导致生成器网络反向传播时梯度计算失败。
解决方案: 将辨别器网络优化器的参数更新放到生成器网络反向传播的下方(后面)。
排坑日记1:RuntimeError: one of the variables needed for gradient computation has been modified相关推荐
- 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 ...
- 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 ...
- 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】RuntimeError: one of the variables needed for gradient computation has been modified by
0.前言 最近在用Pytorch训练网络的时候出现了一个奇怪的错误,找了很久在偶然间试出了解决方法,因此记录一下,以作备忘.但具体原理还不是很清楚,也请各位大佬指教. 1.错误 RuntimeErro ...
- 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 inp
正文在后面,往下拉即可~~~~~~~~~~~~ 欢迎各位深度学习的小伙伴订阅的我的专栏 Pytorch深度学习·理论篇+实战篇(2023版)专栏地址:
- Python 排坑日记 --SSLContext错误
目录 简述 排雷一 排雷二 排雷三 最近一直在学习Vue,碰巧遇到了需要和服务端交互的任务,于是打算用Flask写一个简单的API,没想到的是迎接我的居然是一个大坑. 简述 在创建Pipenv时一直失 ...
- node升级命令_Laravel Mix 4升级说明与“排坑儿”指南
本篇是我们系列课程<Laravel5.7优雅实战入门:第二版>和<Laravel 5.7&Vue 2.x深度整合实战:第二版>的扩展阅读. 想要更好的阅读体验,请观看原 ...
- Go的闭包看你犯错,Rust却默默帮你排坑
作者 | 马超 责编 | 晋兆雨 出品 | CSDN博客 初识闭包 闭包(Closure)在某些编程语言中也被称为 Lambda 表达式,是能够读取其他函数内部变量的函数.一般只有函数内部的子函数才能 ...
最新文章
- Linux下Sed命令的用法(之替换)
- 玩转java(Android)注解
- puts(char *) gets(char *)
- java 生产者消费者_基于JAVA的生产者消费者问题
- Java实现图片文件的上传和获取
- 面试:第十四章:大厂中高级java程序员面试题
- 电力-开闭所/配电房/变电所/变电站
- 电脑开机蓝屏时要怎么解决修复?哪种方便比较好?
- ThoughtWorks待遇
- 分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)java
- 怎么打造云计算的碎片计算方式
- 新手入门:如何用Laya开发微信小游戏?
- HCIP课堂笔记总结
- 如何用python搜索要用的素材_一篇文章教会你利用Python网络爬虫获取素材图片
- 音频频谱 via FFT
- 华中科技大学赛尔宽带登录六维空间
- 微信小程序模仿微信主页及页面跳转详解
- Qt数据库应用23-个人信息报表
- C++利用SIO_GET_INTERFACE_LIST查询网关
- 北京、成都、大连、广州、杭州、南京、青岛、上海、深圳、苏州、天津、武汉、西安、郑州、重庆 15个城市地铁线及站点SHP文件