Thanks for the info!
I tried to debug your code and stumbled upon this line of code 2. You are detaching  image_energy  by calling  .item() . Later in your  get_loss  method, you are using  outputs  to calculate the gradients.
However, since  image_energy  was detached, the computation graph shouldn’t compute any valid gradients before this point.

I think I understand what you mean about the problem I create when detaching the image_energy with .item(). I proceeded to change 2 the line of code you referred above removing the .item() and changed L-202 to outputs = torch.tensor(outputs, requires_grad=True). I still get the same problem. Then it means that the tensors I am using to compute the loss function are broken. Is that correct?

I’m not completely understanding the code, so let me know, if I’m on the wrong path. 

Your analysis is right. Let me just give a brief idea about what is going on.  These targets are two energies (scalars) of two molecules with 4 atoms each. The inputs, in this case, are 4 vectors for each each molecule because there are 4 atoms (see here). Applying forward to those features will return atomic energies. That is why I sum them, add their sum (image_energy) to the outputs array, and then pass them to the loss. I probably am failing to see how to avoid summing those 4 atomic energies to recover the total energy and avoid detaching and breaking the graph calculation.