Hi,
Your implementation is problematic here:
loss_ret = torch.autograd.Variable(loss, requires_grad=True)
Beyond the fact that Variable are not used anymore and you should not use them, what this line means is: “take this Tensor, discard its past, and start tracking gradients for it”.
The autograd can only work of you perform all operations with pytorch methods and you don’t break the graph between where you want gradients and where you backward.
Also I would advise against saving stuff in self. for nn.Modules. As they should be stateless as much as possible.
If you want to be able to inspect these values later, you can simply return them in most cases.