RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation [torch.cuda.FloatTensor [4, 64, 128, 128]], which is output 0 of ReluBackward0, is at version 2 ; 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).
-
解决办法
- 网上找到的
1)找到网络模型中的 inplace 操作,将inplace=True改成 inplace=False,例如torch.nn.ReLU(inplace=False)
2)将代码中的“a+=b”之类的操作改为“c = a + b”
3)将loss.backward()函数内的参数retain_graph值设置为True, loss.backward(retain_graph=True),如果retain_graph设置为False,计算过程中的中间变量使用完即被释放掉。 - 但是不起作用
- 将原来的loss.backward()这行代码更改为
loss1 = loss.detach_().requires_grad_(True)
loss1.backward()
成功解决!
参考博文:https://blog.csdn.net/MilanKunderaer/article/details/121425885