2017-08-16 77 views
4

我希望在keras中实现一个自定义指标,用于计算召回,并假设最有可能的前k%y_pred_probs为真。Keras基于预测值的自定义召回指标

numpy我会这样做。对y_preds_probs进行排序。然后取值为kth指数。注意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后端基于ky_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 

回答

2

感谢您引用我以前的答案。

在这种情况下,如果你正在使用tensorflow后台,我会建议你使用这个tensorflow function

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

它输出的bool的张量,1如果答案属于如果顶部K和0没有。

如果您需要更多信息,我已链接tensorflow文档。我希望它有帮助。 :-)