解决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 inplace operation

解决RuntimeError: one of the variables needed for gradient computation has been modified by an inplace_第1张图片

这一般是出现了不可导问题,即loss.backward()可能存在情况:
1.in-place问题:如x= x+k是inplace操作,会导致变量不可导

其它类似inplace操作


2.还有一种可能:optimizer.step()这句代码放在了loss.backward之前,导致梯度未成功更新

In conclusion:问题都是出在loss.backward(),梯度未成功更新。希望帮到大家

报错代码:

解决RuntimeError: one of the variables needed for gradient computation has been modified by an inplace_第2张图片

 本bug是由fser大佬帮忙解决,非常感谢!

fser的blog:The forest in the fog - Ruochen's Blog

我的知乎主页:德国Viviane - 知乎

你可能感兴趣的:(python,开发语言)