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

Encountered an error:

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

This is generally a problem of non-guidance, that is, loss.backward() may exist:
1. In-place problem: if x= x+k is an inplace operation, the variable will not be guided

Other similar inplace operations


2. There is another possibility: the code of optimizer.step() is placed before loss.backward, resulting in the unsuccessful update of the gradient

In conclusion: The problem is all in loss.backward(), the gradient is not updated successfully. hope to help everyone

Error code:

 This bug was solved by the help of fser, thank you very much!

fser的blog:The forest in the fog - Ruochen's Blog

My Zhihu homepage: German Viviane - Zhihu

Guess you like

Origin blog.csdn.net/Viviane_2022/article/details/126357845