如何在 Python 中使用带有 Keras 的 scikit-learn 评估指标函数?

Posted

技术标签:

【中文标题】如何在 Python 中使用带有 Keras 的 scikit-learn 评估指标函数?【英文标题】:How to employ the scikit-learn evaluation metrics functions with Keras in Python? 【发布时间】:2019-06-01 14:34:06 【问题描述】:

Keras 提供了定义自定义评估指标的可能性——我对 F 指标的变体感兴趣,例如scikit learn 提供的 F1、F2 等,但通过调用在这方面受到限制的 Keras 后端函数来指示我们这样做。

我的目标是将这些指标与 Keras 的 Early-Stopping 方法结合使用。所以我应该找到一种方法将度量与 Keras 模型的学习过程相结合。 (当然,在学习/拟合过程之外,我可以简单地使用结果调用 Scikit-Learn)。

我有什么选择?

更新

使用来自 Kaggle 的 titanic_all_numeric 数据集实施 Aaron 的解决方案后,我得到以下信息:

# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy', f1])

# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)

Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 642us/step - loss: 0.8037 - acc: 0.6132 - f1: 0.6132 - val_loss: 0.5815 - val_acc: 0.7537 - val_f1: 0.7537

# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)

Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 658us/step - loss: 0.8148 - acc: 0.6404 - val_loss: 0.7056 - val_acc: 0.7313

# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = [f1])

# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)

Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 690us/step - loss: 0.6554 - f1: 0.6709 - val_loss: 0.5107 - val_f1: 0.7612

我想知道这些结果是否正常。一次,准确率和 f1 分数是一样的。

【问题讨论】:

【参考方案1】:

您可以将 keras 模型中的预测和标签传递给任何 scikit-learn 函数以进行评估。例如,如果您正在处理分类问题,您可以使用 scikit-learn 中的classification_report,它提供了诸如精度、召回率、f1 分数等指标。 (示例代码直接取自他们的文档):

from sklearn.metrics import classification_report
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))

          precision    recall  f1-score   support

 class 0       0.50      1.00      0.67         1
 class 1       0.00      0.00      0.00         1
 class 2       1.00      0.67      0.80         3

 micro avg     0.60      0.60      0.60         5
 macro avg     0.50      0.56      0.49         5
 weighted avg  0.70      0.60      0.61         5

更新: 如果您想将指标纳入 keras 训练使用:

from keras import backend as K

def f1(y_true, y_pred):
    def recall(y_true, y_pred):
        """Recall metric.

        Only computes a batch-wise average of recall.

        Computes the recall, a metric for multi-label classification of
        how many relevant items are selected.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
        recall = true_positives / (possible_positives + K.epsilon())
        return recall

    def precision(y_true, y_pred):
        """Precision metric.

        Only computes a batch-wise average of precision.

        Computes the precision, a metric for multi-label classification of
        how many selected items are relevant.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
        precision = true_positives / (predicted_positives + K.epsilon())
        return precision
    precision = precision(y_true, y_pred)
    recall = recall(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))


model.compile(loss='binary_crossentropy',
          optimizer= "adam",
          metrics=[f1])

【讨论】:

感谢您的回复,我应该更清楚地说明我的问题。请查看更新。 这太棒了亚伦!非常感谢! Aaron 我刚刚试用了代码。当我将 'accuracy' 和 f1 作为指标传递时,我会得到相同的结果。你能解释一下吗?查看我更新的帖子。

以上是关于如何在 Python 中使用带有 Keras 的 scikit-learn 评估指标函数?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用带有词嵌入的 Keras LSTM 来预测词 id

如何在 GPU 上使用 Keras?

在谷歌 colab 中使用带有 TPU 的 Keras 调谐器搜索方法时如何解决“从空列表中弹出”错误?

将 Keras 模型转换为 C++ [关闭]

如何更新 keras 中的权重以进行强化学习?

你如何在 Python 中使用 Keras LeakyReLU?