张量的元素 0 不需要 grad 并且在使用自定义损失函数时没有 grad_fn

xiaoxingxing pytorch 498

原文标题element 0 of tensors does not require grad and does not have a grad_fn while using custom loss function

我正在尝试在多类分割任务中使用骰子分数

这是我使用的自定义损失函数(骰子分数):

def dice(output, target):
dice_tmp = 0
for index in range(3):
    dice_tmp += (2 * (output[:,index,:,:] * target[:,index,:,:]).sum()) / ((output[:,index,:,:] + target[:,index,:,:]).sum() + 1e-8)
dice = dice_tmp/3 # taking averag

return dice

这是训练脚本:

loop = tqdm(loader)

for data, targets in  loop:
    data = data.to(device=DEVICE)
    targets = targets.float().to(device=DEVICE)
    
    #dice = 0
        
    # forward
    with torch.cuda.amp.autocast():
        predictions = (torch.sigmoid(model(data)) > 0.5).float()
        loss = dice(predictions, targets)
        print(type(loss))

    # backward
    optimizer.zero_grad()
    scaler.scale(loss).backward()
    scaler.step(optimizer)
    scaler.update()

    # update tqdm loop
    loop.set_postfix(loss=loss.item())

我正面临这个错误

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

当我将损失替换为:

loss = torch.tensor(dice(predictions, targets), requires_grad=True)

我收到了这个错误:

No inf checks were recorded for this optimizer.

原文链接:https://stackoverflow.com//questions/71486715/element-0-of-tensors-does-not-require-grad-and-does-not-have-a-grad-fn-while-usi

回复

我来回复
  • Shai的头像
    Shai 评论

    这是什么??

    predictions = (torch.sigmoid(model(data)) > 0.5).float()
    

    你正在为你的预测设定阈值!根据定义,您的梯度现在几乎在所有地方都为零。在那之后就没有办法恢复你的学习过程了。


    你确定你知道你在做什么吗?

    1. 你需要你的骰子损失来使用“软标签”:也就是说,接受输出是
      不是
      二进制预测 {0,1} ,而是范围 [0, 1] 内的软预测。因此,您的预测将是: predictions = torch.sigmoid(model(data)) 。
    2. 计算分母时,您需要分别 .sum() 每个张量,而不是它们的总和。
    3. 骰子是一个
      分数
      ,而不是损失:即 Dice 分数高表示预测好,而 Dice 分数低表示预测差。您的代码尝试
      最小化
      骰子分数 – 这会使预测变得更糟。这就是你想要达到的目标吗?
    2年前 0条评论