[torch.FloatTensor [*, *]] is at version 2; expected version 1 instead

训练网络时遇到的RuntimeError,
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 32,3,3]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

[torch.FloatTensor [*, *]] is at version 2; expected version 1 instead_第1张图片
定位后发现是在loss.backward()出现了问题,之后看到了文章
https://blog.csdn.net/qq_33866063/article/details/109775468
里面提到了三种解决方法,
我将原来的loss.backward()这行代码更改为
loss1 = loss.detach_().requires_grad_(True)
loss1.backward()
错误解决。

你可能感兴趣的:(python,pytorch)