This error might be raised, since you are keeping the graph via retain_graph = True, which would then in the second iteration try to backpropagate through all parameters twice.
Is this your use case or why are you using retain_graph = True?