简体   繁体   English

Keras:骰子系数损失函数为负且随时间增加

[英]Keras: Dice coefficient loss function is negative and increasing with epochs

According to this Keras implementation of Dice Co-eff loss function, the loss is minus of calculated value of dice coefficient.根据 Dice Co-eff 损失函数的这个 Keras 实现,损失减去骰子系数的计算值。 Loss should decrease with epochs but with this implementation I am , naturally, getting always negative loss and the loss getting decreased with epochs, ie shifting away from 0 toward the negative infinity side, instead of getting closer to 0. If I use (1- dice co-eff) instead of (-dice co-eff) as loss, will it be wrong?损失应该随着 epochs 减少,但是通过这个实现,我自然而然地总是负损失,并且损失随着 epochs 减少,即从 0 向负无穷大侧移动,而不是接近 0。如果我使用 (1- dice co-eff) 而不是 (-dice co-eff) 作为损失,会不会错? Here's the full Keras implementation (which I was talking about): https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py这是完整的 Keras 实现(我正在谈论): https : //github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py

smooth = 1.

def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)


def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)

I have shared a log of my experiment with you, although only for 2 epochs:我已经和你分享了我的实验日志,虽然只有 2 个时期:

Train on 2001 samples, validate on 501 samples
Epoch 1/2
Epoch 00001: loss improved from inf to -0.73789, saving model to unet.hdf5
 - 3229s - loss: -7.3789e-01 - dice_coef: 0.7379 - val_loss: -7.9304e-01 - val_dice_coef: 0.7930
Epoch 2/2
Epoch 00002: loss improved from -0.73789 to -0.81037, saving model to unet.hdf5
 - 3077s - loss: -8.1037e-01 - dice_coef: 0.8104 - val_loss: -8.2842e-01 - val_dice_coef: 0.8284
predict test data
9/9 [==============================] - 4s 429ms/step
dict_keys(['val_dice_coef', 'loss', 'val_loss', 'dice_coef'])

1-dice_coef-dice_coef对收敛没有任何影响,但1-dice_coef提供了一种更熟悉的监控方式,因为值在 [0, 1] 范围内,而不是 [-1, 0]。

我认为正确的损失是 1 - dice_coef(y_true, y_pred)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM