Created
May 25, 2018 09:55
-
-
Save nokados/57a281904c50a4731ce09c3a0eb71d0d to your computer and use it in GitHub Desktop.
Precision, recall, f1_score for Keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras.backend as K | |
def recall(y_true, y_pred): | |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) | |
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) | |
recall = true_positives / (possible_positives + K.epsilon()) | |
return recall | |
def precision(y_true, y_pred): | |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) | |
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) | |
precision = true_positives / (predicted_positives + K.epsilon()) | |
return precision | |
def f1_score(true, pred): | |
p = precision(true, pred) | |
r = recall(true, pred) | |
return 2 * (p * r) / (p + r + 1e-6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment