Retain_graph error

I’m trying to backward over loss inside a loop.
The loss variable is computed afresh each time and so are other variables. The 1st iteration works fine and backward() call works.

But it throws the retain_graph error in the consequent iteration. I can’t find where the problem is.

The variables inside model didn’t get updated with parameters, causing a dependency on prev iteration parameters.