错误处理: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 operation: [torch.cuda.FloatTensor [1, 100]], which is output 0 of UnsqueezeBackward0, is at version 5040; expected version 5039 instead.

错误分析:

由于pytorch升级到pytorch0.4之后,与之前pytorch0.3的用法发生来了一些变化,比如最重要的在pytorch0.4中将Tensor与Variance都组合成了同一个东西,pytorch0.4不再支持inplace操作。

解决方法:


1.把所有的inplace=True改成inplace=False
2.将loss+=new_loss这样所有的+=操作,改成loss=loss+new_loss
3.将pytorch版本回退到0.3,或者添加一个pytorch0.3的环境。
 

Guess you like

Origin blog.csdn.net/qq_40206371/article/details/121259361