Solved by albanD in post #2 
              
              
                Hi, 
This is most likely not linked to your get_gradients() function. 
But more to the fact that something that you using in the loop (inputs or coordinates) already has a history (you made some operations with it while it was requiring gradients) and so this part of the graph is shared across itera…