Trying to pass custom loss but it will not allow me to. AttributeError: 'float' object has no attribute 'backward'

Trying to pass custom loss but it will not allow me to. AttributeError: 'float' object has no attribute 'backward'

我有一个自定义损失函数,我想在我的模型上使用它,但是当我在 Pytorch 中使用 loss.backward() 时它不起作用。

这是我的损失函数:

class Neg_Pearson(nn.Module):    # Pearson range [-1, 1] so if < 0, abs|loss| ; if >0, 1- loss
    def __init__(self):
        super(Neg_Pearson,self).__init__()
        return
    def forward(self, preds, labels):       # tensor [Batch, Temporal]
        loss = 0
        for i in range(preds.shape[0]):
            sum_x = torch.sum(preds[i])                # x
            sum_y = torch.sum(labels[i])               # y
            sum_xy = torch.sum(preds[i]*labels[i])        # xy
            sum_x2 = torch.sum(torch.pow(preds[i],2))  # x^2
            sum_y2 = torch.sum(torch.pow(labels[i],2)) # y^2
            N = preds.shape[1]
            pearson = (N*sum_xy - sum_x*sum_y)/(torch.sqrt((N*sum_x2 - torch.pow(sum_x,2))*(N*sum_y2 - torch.pow(sum_y,2))))
                        
            loss += 1 - pearson
        
        
        loss = loss.tolist()
        loss = loss/preds.shape[0]
        #print(loss) 
        return loss

当我尝试将它与模型一起使用时:

        yp = (yp-torch.mean(yp)) /torch.std(yp)     # normalize
        yt = (yt-torch.mean(yt)) /torch.std(yt)     # normalize
        
        
        
        loss = neg_pears_loss(yp, yt)
        print(loss)
        
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

我收到以下错误:AttributeError: 'float' object has no attribute 'backward'关于如何解决这个问题有什么建议吗?

backward 是 PyTorch Tensor 的一个函数。当你调用 loss.tolist() 时,你破坏了计算图,你不能从那里向后退。我不确定你想用那行代码完成什么,但注释掉它应该会有帮助。