Trouble avoiding "Trying to backward through the graph a second time" error on simple model

Hello,
I faced this error once. Here is the solution: RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time

In my case, since I called .backward() twice, It happend to me. I fixed this by adding retain_graph = True as an input of the first backward call