machine-learning - Keras 中的自定义损失函数用于惩罚漏报

标签 machine-learning keras deep-learning gradient-descent loss-function

我正在研究一个医疗数据集,试图尽可能减少误报。 “实际上没有疾病时有疾病”的预测对我来说可以,但“实际上有疾病时没有疾病”的预测则不然。也就是说,我同意 FP但不是FN .

经过一些研究,我发现了类似 Keeping higher learning rate for one class 的方法, using class weights , ensemble learning with specificity/sensitivity等等

我使用像 class_weight = {0 : 0.3,1: 0.7} 这样的类权重实现了接近预期的结果然后调用model.fit(class_weights=class_weight) 。这给了我非常低的 FN 但相当高的 FP。我正在尝试尽可能减少 FP,保持 FN 非常低。

我正在努力使用 Keras 编写自定义损失函数这将帮助我惩罚假阴性。感谢您的帮助。

最佳答案

我将简要介绍我们正在尝试解决的概念。

记忆

所有阳性中,我们的模型预测有多少为阳性?

所有积极的= positive

我们的模型所说的是积极的 = said positive

recall

由于召回率与 FN 成反比,因此提高召回率会降低 FN。

特异性

所有阴性中,我们的模型预测有多少为阴性?

所有负面的= negative

我们的模型所说的是负数 = said negative

specificity

由于特异性与 FP 成反比,因此提高特异性会降低 FP。

在您的下一次搜索或您执行的任何与分类相关的事件中,了解这些将为您在沟通和理解方面提供额外的优势。

<小时/>

解决方案

所以。正如您已经了解的那样,这两个概念是相反的。这意味着增加其中一项可能会减少另一项

由于您希望记忆优先,但又不想在特异性上失去太多,因此您可以将这些权重和属性权重结合起来。遵循 this answer 中明确解释的内容:

import numpy as np
import keras.backend as K

def binary_recall_specificity(y_true, y_pred, recall_weight, spec_weight):

    TN = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 0)
    TP = np.logical_and(K.eval(y_true) == 1, K.eval(y_pred) == 1)

    FP = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 1)
    FN = np.logical_and(K.eval(y_true) == 1, K.eval(y_pred) == 0)

    # Converted as Keras Tensors
    TN = K.sum(K.variable(TN))
    FP = K.sum(K.variable(FP))

    specificity = TN / (TN + FP + K.epsilon())
    recall = TP / (TP + FN + K.epsilon())

    return 1.0 - (recall_weight*recall + spec_weight*specificity)

注意到recall_weightspec_weight了吗?它们是我们赋予每个指标的权重。对于分发约定,它们应始终添加到 1.01,例如recall_weight=0.9speciality_weight=0.1。这里的目的是让您了解什么比例最适合您的需求。

但是 Keras 的损失函数必须只接收 (y_true, y_pred) 作为参数,所以让我们定义一个包装器:

# Our custom loss' wrapper
def custom_loss(recall_weight, spec_weight):

    def recall_spec_loss(y_true, y_pred):
        return binary_recall_specificity(y_true, y_pred, recall_weight, spec_weight)

    # Returns the (y_true, y_pred) loss function
    return recall_spec_loss

然后我们就可以使用它了

# Build model, add layers, etc
model = my_model
# Getting our loss function for specific weights
loss = custom_loss(recall_weight=0.9, spec_weight=0.1)
# Compiling the model with such loss
model.compile(loss=loss)

¹ 添加后的权重总计必须为 1.0,因为如果 recall=1.0specificity=1.0(满分) ,公式

loss1

例如,应给我们,

loss2

显然,如果我们获得满分,我们希望损失等于 0。

关于machine-learning - Keras 中的自定义损失函数用于惩罚漏报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52695913/

相关文章:

tensorflow - 反向传播和反向模式自动微分有什么区别?

machine-learning - 引用url地址学习数据挖掘算法C5.0

python - python中逻辑回归GD的实现

python - 如何安装keras-bert? (包未找到错误: The following packages are not available from current channels)

python - 如何使用 LSTM 生成序列?

deep-learning - YOLO中的 anchor 框: How are they decided

python - 密集合成器的实现

validation - 处理数据集中的缺失值

python - 使用 scikit learn .9 或更低版本的随机森林

python - 在 Tensorflow 中——是否可以在一个层中锁定特定的卷积过滤器,或者完全删除它们?