To reduce memory usage, during the .backward()
call, all the intermediary results are deleted when they are not needed anymore. Hence if you try to call .backward()
again, the intermediary results don’t exist and the backward pass cannot be performed (and you get the error you see).
You can call .backward(retain_graph=True)
to make a backward pass that will not delete intermediary results, and so you will be able to call .backward()
again. All but the last call to backward should have the retain_graph=True
option.
77 Likes