one of the variables needed for gradient computation has been modified by an inplace operation

created at 11-22-2021 views: 5

PyTorch ERROR:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1024, 512, 3, 3]], which is output 26 of BroadcastBackward, 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).

I have encountered it a few times, but the problem lies in the forward function. Just change some variable names to different ones.

It is also possible that no_grad was accidentally added:

with torch.no_grad():
    pre = model(img)
created at:11-22-2021
edited at: 11-22-2021: