Hello,
In short, it won’t work using item. You need to keep tensors all along, else you’ll lose the computational graph (DAG) which is used by the autograd when calling backward (see autograd mechanism in the doc). item() method return a standard python number, which is indeed not compatible with backward.
You can safely use del on the loss after it is appended to the losses_list, but my guess is you won’t notice any significant change in memory footprint.
I don’t see any way to reduce the memory footprint on this part of your code.
Regards,
Thomas
[Edit] By the way, look to the documentation for the item method, it clearly states “This operation is not differentiable.”