These errors are often raised by using retain_graph = True
as a workaround for another issue.
Could you explain why you are using it?
If you are not sure and added it to avoid the “trying to backpropagate a second time…” error, check if you have forgotten to detach the computation graph to avoid trying to recompute gradients from previous iterations.