Thanks for the clarification.
Your code seems to work. I’ve created a small dummy example where a very simple model fits some random noise.
Both approaches work, i.e. the plain code and using your functions.
As the code is quite long, I’ve created a gist 125.
Could you think about other issues, where you might have reset your gradients or the optimizer?