Actually, this is something strange, because I haven’t seen any other examples with this retain_graph=True, but if I remove it, it prompts an error:
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.
I will try your advice. Thank you Francisco!