Yun_Lee:

when I searched the solution to that runtime error, the answer was to specify retain_graph = True
but I already specified retain_graph=True in that code.


You need to specify it the first time you called autograd.grad or .backward. Not the one that raises the error.
Also if you have your training loop, you should avoid doing differentiable computations outside of the loop. As it will also create part of the graph that will be re-used from one iteration to the next.