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

问题

pytorch代码,0.3.0版本还可以运行,0.4.0就报了这个错。

原因

由于0.4.0把Varible和Tensor融合为一个Tensor,inplace操作,之前对Varible能用,但现在对Tensor,就会出错了。

办法

找到网络模型中的inplace操作。

x = x + 1 # not inplace
x += 1 # inplace

把第二种写法,替换成第一种。

至于如果网络很大,找起来很麻烦,可以在网络的中间变量加一句 x.backward(),看会不会报错,如果不会,说明至少这之前是OK的。

你可能感兴趣的:(pytorch)