Thanks for your reply. I think I forgot to add the optimizer in the code snippet. Please see the last block where I add the optimizer.step() during each iteration. Therefore, I can’t call loss.backward() after the loop.