Created
December 11, 2020 00:55
-
-
Save seahrh/825304f2c5dbe3bf5a1b2b6feb6bd78e to your computer and use it in GitHub Desktop.
Keras clipping custom metric. Adapted from https://neptune.ai/blog/keras-metrics
This file contains hidden or 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
def recall(y_true, y_pred): | |
y_true = K.ones_like(y_true) | |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) | |
all_positives = K.sum(K.round(K.clip(y_true, 0, 1))) | |
recall = true_positives / (all_positives + K.epsilon()) | |
return recall | |
def precision(y_true, y_pred): | |
y_true = K.ones_like(y_true) | |
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(y_true, y_pred): | |
precision = precision_m(y_true, y_pred) | |
recall = recall_m(y_true, y_pred) | |
return 2*((precision*recall)/(precision+recall+K.epsilon())) | |
model.compile(...,metrics=['accuracy', f1_score, precision, recall]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment