【发布时间】:2018-02-08 05:28:11
【问题描述】:
您好,我一直在尝试在 keras 中为 dice_error_coefficient 制作自定义损失函数。它在 tensorboard 中有实现,我尝试在带有 tensorflow 的 keras 中使用相同的函数,但是当我使用 model.train_on_batch 时它一直返回 NoneType或 model.fit,因为它在模型中的指标中使用时会给出正确的值。有人可以帮我解决我该怎么做吗?我曾尝试关注 ahunt 的 Keras-FCN 等库,他在其中使用了自定义损失函数,但似乎都不起作用。代码中的目标和输出分别是 keras 中的 loss.py 文件中使用的 y_true 和 y_pred。
def dice_hard_coe(target, output, threshold=0.5, axis=[1,2], smooth=1e-5):
"""References
-----------
- `Wiki-Dice <https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`_
"""
output = tf.cast(output > threshold, dtype=tf.float32)
target = tf.cast(target > threshold, dtype=tf.float32)
inse = tf.reduce_sum(tf.multiply(output, target), axis=axis)
l = tf.reduce_sum(output, axis=axis)
r = tf.reduce_sum(target, axis=axis)
hard_dice = (2. * inse + smooth) / (l + r + smooth)
hard_dice = tf.reduce_mean(hard_dice)
return hard_dice
【问题讨论】:
标签: python machine-learning tensorflow keras