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

I am doing binary classification with a batch size of 8 and the output of my model is:

out shape is:  torch.Size([8, 2])
out is:  tensor([[-1.2086,  0.8501],
        [-0.8705, -0.2427],
        [-1.0448,  0.5410],
        [-1.2318,  0.2670],
        [-1.0920,  0.0126],
        [-0.9867,  0.1056],
        [-1.2848,  0.9758],
        [-1.2415, -0.2882]], device='cuda:0', grad_fn=<AddmmBackward0>)

and I am getting this error:

train loss:  0.0
Traceback (most recent call last):
  File "main_classifier.py", line 253, in <module>
    train_loss.backward(retain_graph=True) 
AttributeError: 'float' object has no attribute 'backward'

How can I solve this error?

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