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
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx())
true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(y_pred)
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
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx())
true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.clip(y_true, 0, 1))
recall_ratio = true_positives / (possible_positives + K.epsilon())
return recall_ratio
return recall
Пользовательский порог принятия решения Keras для точности и отзыва