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

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

Error Messages:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [544, 768]], which is output 0 of ViewBackward, 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).

Solution:
The reason for the error is that when I was building the network model, the code for summing residuals was written like this: x += y
But in pytorch, it is wrong to write it like this, just change it to: x = x+y