Keras custom recall metric based on predicted valu

2020-07-17 06:17发布

问题:

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.

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])

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

回答1:

Thanks for citing my previous answer.

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
)

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

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