6
我正在使用Keras
(與Tensorflow
後端)的二元分類,我已經得到了約76%的精度和70%的召回。現在我想嘗試玩決定門檻。據我所知Keras
使用決策閾值0.5。 Keras
有沒有辦法使用自定義閾值進行決策精確度和召回?精密Keras定製判決門限和召回
謝謝你的時間!
我正在使用Keras
(與Tensorflow
後端)的二元分類,我已經得到了約76%的精度和70%的召回。現在我想嘗試玩決定門檻。據我所知Keras
使用決策閾值0.5。 Keras
有沒有辦法使用自定義閾值進行決策精確度和召回?精密Keras定製判決門限和召回
謝謝你的時間!
創建自定義指標是這樣的:
編輯感謝@Marcin:創建功能與threshold_value
爲參數
def precision_threshold(threshold=0.5):
def precision(y_true, y_pred):
"""Precision metric.
Computes the precision over the whole batch using threshold_value.
"""
threshold_value = threshold
# Adaptation of the "round()" used before to get the predictions. Clipping to make sure that the predicted raw values are between 0 and 1.
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx())
# Compute the number of true positives. Rounding in prevention to make sure we have an integer.
true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1)))
# count the predicted positives
predicted_positives = K.sum(y_pred)
# Get the precision ratio
precision_ratio = true_positives/(predicted_positives + K.epsilon())
return precision_ratio
return precision
def recall_threshold(threshold = 0.5):
def recall(y_true, y_pred):
"""Recall metric.
Computes the recall over the whole batch using threshold_value.
"""
threshold_value = threshold
# Adaptation of the "round()" used before to get the predictions. Clipping to make sure that the predicted raw values are between 0 and 1.
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx())
# Compute the number of true positives. Rounding in prevention to make sure we have an integer.
true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1)))
# Compute the number of positive targets.
possible_positives = K.sum(K.clip(y_true, 0, 1))
recall_ratio = true_positives/(possible_positives + K.epsilon())
return recall_ratio
return recall
現在你可以在
model.compile(..., metrics = [precision_threshold(0.1), precision_threshold(0.2),precision_threshold(0.8), recall_threshold(0.2,...)])
使用這些返回所需的指標
我希望這有助於:)
@NassimBen不錯的解決方案。我想做一些非常相似的事情,但是根據'y_pred'中的'kth'最大值來判斷'閾值_value':我在這裏提出了這個問題:https://stackoverflow.com/questions/45720458/keras-自定義召回 - 基於度量的預測值 – notconfusing
如果我給它不同的閾值,並保存模型的精度或召回值,模型將保存在模型中? – Mohsin