train_loss.backward(retain_graph=True) AttributeError: 'float' object has no attribute 'backward'

Your train_loss seems to be a plain float (i.e. not a tensor) so make sure you are calling backward on the actual loss tensor not the Python float scalar.

2 Likes