Ok, I’ve changed the things you suggested and the code seems to be running now. But for some reason sometimes when I run the code the return value from loss.item() is nan, while other time it returns a float…? It seems to be happening at random, do you have any idea why this might be happening?

i: 1, Loss: 0.6917
i: 2, Loss: 0.5840
i: 3, Loss: 0.4336
i: 4, Loss: nan
i: 5, Loss: nan
i: 6, Loss: nan
i: 7, Loss: nan
i: 8, Loss: nan
i: 9, Loss: nan
i: 10, Loss: nan
i: 11, Loss: nan
i: 12, Loss: nan