一、问题
问题:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 64, 256, 256]], which is output 0 of ReluBackward0, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
原因: PyTorch版本的问题
二、解决方法
2.1 nn.ReLU 问题(试过,不行)
把对应模块的self.relu = nn.ReLU(inplace=True)改成self.relu = nn.ReLU(inplace=False)
2.2 loss.backward() 问题(试过,不行)
loss.backward(retain_graph=True)
2.3 PyTorch版本的问题(试过,可以解决)
- pytorch==2.0.0,试过不好使;
- pytorch==1.13.0,试过不好使;
- pytorch==1.7.0,试过有用。
Linux ,用的下面代码安装(用conda安装不成功,网慢,用pip安装可以
):
pip install torch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0
- 可以去官网下载自己服务器对应的版本:PyTorch官网
参考
pytorch坑货–RuntimeError: one of the variables needed for gradient computation has been modified