My gueses are:

You don’t evaluate under a with torch.no_grad() environment.
You are storing the tensor losses instead of their values (that is, you are missing calling the item() method of the tensor) keeping in memory the whole computation graph.

Check those two first and see if that’s the problem. At this moment I don’t have time to check out your code.