栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在进行网络训练的时候,显示loss.backward()处出现RuntimeError:one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [5, 16, 316, 316]], which is output 0 of LeakyReluBackward1, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True) 的错误。

在网上找了一圈,这类问题主要是梯度求导的问题,在计算梯度前此变量被修改。下面是我在网上找到的方法,及对我有用的方法,亲则有效!

方法一:

可以看下代码里有没有a += xxx这类的代码,把它改成:

a = a + xxx

或者a = a.clone() + xxx

方法二:

可以看看是不是网络共用某个参数。当两个地方共用了同一个参数,先进行求导的那个地方更新了这个参数导致后面共用这个参数的地方的梯度计算失败。其解决方法是在第二个网络计算梯度之前对这个参数进行detach()操作。

方法三:

将网络中ReLU相关的激活函数中的(inplace=True)改为False。

此方法亲测有效!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号