基于预测值的Keras自定义召回指标 [英] Keras custom recall metric based on predicted values

查看:196
本文介绍了基于预测值的Keras自定义召回指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在keras中实现一个自定义指标,假设最高k%的最可能y_pred_probs是真实的,从而计算召回率.

I would like to implement a custom metric in keras that calculates the recall assuming that the top k% most probable y_pred_probs's are true.

numpy中,我将按以下步骤进行操作.对y_preds_probs进行排序.然后取第c2个索引处的值.注意k=0.5将给出中间值.

In numpy I would do it as follows. Sort the y_preds_probs. Then take the value at the kth index. Note k=0.5 would give the median value.

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自定义决策阈值非常接近,但是假定用于确定哪个y_pred被假定为真的阈值是已知的.我想将这些方法结合起来,并在可能的情况下实现基于Keras后端中的ky_pred查找阈值.

The answer from: Keras custom decision threshold for precision and recall is quite close but assumes that the threshold for deciding which y_pred's are assumed true is already known. I would like to combine the approaches and implement finding the threshold_value based on k and y_pred's in Keras backend if possible.

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

推荐答案

感谢您引用我之前的回答.

Thanks for citing my previous answer.

在这种情况下,如果您正在使用tensorflow后端,我建议您使用此

In this case, if you are using tensorflow backend, I would suggest you to use this tensorflow function :

tf.nn.in_top_k(
    predictions,
    targets,
    k,
    name=None
)

它输出一个布尔张量,如果答案属于前k个,则输出张量;如果答案不属于前k,则输出一个张量.

It outputs a tensor of bools, 1 if the answer belongs to top k and 0 if it doesn't.

如果您需要更多信息,我已经链接了tensorflow文档.希望对您有所帮助. :-)

If you need more info, I have linked the tensorflow documentation. I hope it helps. :-)

这篇关于基于预测值的Keras自定义召回指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆