Solved by tom in post #2 
              
              
                The usual way is to use torch.autograd.grad instead of backward for the derivative you want to include in your loss. 
Best regards 
Thomas