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