btguijiyang 2019-05-05 14:51 采纳率: 100%
浏览 4368
已采纳

tensorflow自定义的损失函数 focal_loss出现inf,在训练过程中出现inf

图片说明

def focal_loss(alpha=0.25, gamma=2.):
    """ focal loss used for train positive/negative samples rate out 
    of balance, improve train performance
    """
    def focal_loss_calc(y_true, y_pred):
        positive = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
        negative = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
        return -(alpha*K.pow(1.-positive, gamma)*K.log(positive) +
                       (1-alpha)*K.pow(negative, gamma)*K.log(1.-negative))
    return focal_loss_calc
self.keras_model.compile(optimizer=optimizer, loss=dice_focal_loss, metrics=[ mean_iou, dice_loss, focal_loss()])

上面的focal loss 开始还是挺正常的,随着训练过程逐渐减小大0.025左右,然后就突然变成inf。何解

  • 写回答

1条回答

  • btguijiyang 2019-06-13 23:47
    关注

    解决方法

    def focal_loss_calc(alpha=0.25, gamma=2., epsilon=1e-6):
        """ focal loss used for train positive/negative samples rate out 
        of balance, improve train performance
        """
        def focal_loss(y_true, y_pred):
            positive = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
            negative = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
            return -alpha*K.pow(1.-positive, gamma)*K.log(positive+epsilon) - \
                (1-alpha)*K.pow(negative, gamma)*K.log(1.-negative+epsilon)
        return focal_loss
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn