4
我希望在keras中实现一个自定义指标,用于计算召回,并假设最有可能的前k%y_pred_probs
为真。Keras基于预测值的自定义召回指标
在numpy
我会这样做。对y_preds_probs进行排序。然后取值为k
th指数。注意k=0.5
会给出中值。
kth_pos = int(k * len(y_pred_probs))
threshold = np.sort(y_pred_probs)[::-1][kth_pos]
y_pred = np.asarray([1 if i >= threshold else 0 for i in y_pred_probs])
从答案:Keras custom decision threshold for precision and recall是相当接近,但假设这y_pred
的假设为真决定的阈值是已知的。如果可能的话,我想结合这些方法并实现在Keras后端基于k
和y_pred
查找threshold_value。
def recall_at_k(y_true, y_pred):
"""Recall metric.
Computes the recall over the whole batch using threshold_value from k-th percentile.
"""
###
threshold_value = # calculate value of k-th percentile of y_pred here
###
# 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