Skip to content

Instantly share code, notes, and snippets.

@victorouttes
Created July 1, 2019 19:43
Show Gist options
  • Save victorouttes/6c638f15669908cc5502db79e95e7f33 to your computer and use it in GitHub Desktop.
Save victorouttes/6c638f15669908cc5502db79e95e7f33 to your computer and use it in GitHub Desktop.
Keras Recall, Precision and F1 score for binary classification
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(y_true, y_pred):
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment