1 问题描述

今天在写作DeepLabV3+的代码时,遇到了一个问题,

程序报错:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [2, 64, 253, 765]], which is output 0 of ReluBackward1, is at version 2; expected version 1 instead.

根据提示信息来说,感觉应该是跟inplace有关的操作出现的问题;

2 解决方案

出现这个问题,我第一时间想到的是就是nn.ReLU()函数出现的问题,因为ReLU()中有一个设置就是是否启用原位操作;

在一般情况下,为了节省内存,都会设置为True;

所以我把所有使用ReLU函数的地方全部排查了一遍,发现问题的原因是使用了两个连续的ReLU(inplace=True)的操作,

触发错误的代码如下:

conv1 = nn.Conv2d(3, 128, kernel_size=1, bias=False)
bn = nn.BatchNorm2d(128)
relu1 = nn.ReLU(inplace = True)
relu2 = nn.ReLU(inplace = True)input = torch.randn(10,3,8,8)
x = conv1(input)
x = bn(x)
x = relu1(x)# the second successive inplace ReLU operation
x = relu2(x)x = x.flatten()
x = torch.sum(x)
x.backward()

已经提交到PyTorch官方的GitHub上:

https://github.com/pytorch/pytorch/issues/34429

3 备忘

3.1 连续使用ReLU(inplace=True)和+=原位操作的bug已经修复

GitHub的上关于“连续使用ReLU(inplace=True)和+=原位操作的bug已经修复”,issue的链接如下:

https://github.com/pytorch/pytorch/issues/5687#issuecomment-412681482

3.2 不能使用原位操作修改叶子节点

PyTorch不允许直接使用原位操作修改叶子节点,如果需要修改,

可以参考下面的链接:https://zhuanlan.zhihu.com/p/38475183

3.3 可以使用Tensor.sum.backward()来辅助定位可能出现错误的代码位置

我们可以使用Tensor.sum.backward()来辅助定位可能出现错误的代码位置;

PyTorch报错“RuntimeError: one of the variables needed for gradient computation has been modified by……”相关推荐

  1. 解决一个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 ...

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

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

  3. 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 ...

  4. 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 ...

  5. 排坑日记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 ...

  6. 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 ...

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

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

  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. fastRPC的数据库服务
  2. C语言实验——求一个3*3矩阵对角线元素之和
  3. 百度云获取外链直接下载突破限速
  4. centos7安装mysql8.0简书_【Linux实战03】CentOS 7 下 MySQL 8.0 的安装
  5. for循环批量写文件 shell_shell脚本:for循环批量重命名带空格文件名的文件
  6. matlab pca可视化,利用Matlab实现PCA demo展示
  7. linux 喂狗时间,狗狗正确喂食时间表,喂狗最佳时间指南
  8. fun函数python_Python函数
  9. [剑指offer] 61. 序列化二叉树
  10. windows IDA 调试SO
  11. jQuery判断checked的三种方法
  12. UVA10673 Play with Floor and Ceil【暴力枚举】
  13. c语言mpi矩阵乘法,【MPI并行程序】矩阵乘法
  14. java连接oracle数据库jdbc
  15. 《统计学习方法》(李航)读书笔记
  16. Qt QWidget视频上叠加文字
  17. 终端代码重复率检测实践
  18. 新的开始,fighting
  19. android 如何从 3.55mm耳机接口 读取数据,USB Type-C接口详细定义,自制Type-C转3.5mm耳机转接线...
  20. 图案设计灵感怎么写_优秀Logo的设计灵感,从何而来?

热门文章

  1. python 解析json typeerror_TypeError:在使用Python解析JSON时,字符串索引必须是整数?...
  2. Redis基础知识详解(非原创)
  3. 谷歌 Provisional headers are shown 和360急速模式 网络连接错误
  4. 如何为Swift进行宏定义
  5. IIS和Asp.Net页面运行机制
  6. 【编译】StreamInsight简介
  7. 用Response.Filter生成静态页
  8. 软件_crontab任务配置失败原因总结和技巧
  9. 通向财务自由之路02_成功的决定因素:你
  10. 配置csrf_django 入门第一课 配置文件