Solved by tom in post #2 
              
              
                The error you had before the present one, namely 

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling .backward() or autograd.grad() the first time. 

is a bit incomplete. Between the tw…