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 iterations.
You want to make sure that this is not the case and that you re-create the graph at each iteration.
In particular here, if you get gradients for coordinates from outputs while only inputs were used. I guess you have a graph that links inputs and coordinates and this one is shared across iterations.
You should move that computation inside the while loop.