Last active
September 13, 2017 18:38
-
-
Save cfperez/bb6c56989b56c3dbac51743105b4135f to your computer and use it in GitHub Desktop.
Some metrics for Keras training
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
# From https://github.com/fchollet/keras/issues/5400#issuecomment-314747992 | |
from keras import backend as K | |
def mcor(y_true, y_pred): | |
#matthews_correlation | |
y_pred_pos = K.round(K.clip(y_pred, 0, 1)) | |
y_pred_neg = 1 - y_pred_pos | |
y_pos = K.round(K.clip(y_true, 0, 1)) | |
y_neg = 1 - y_pos | |
tp = K.sum(y_pos * y_pred_pos) | |
tn = K.sum(y_neg * y_pred_neg) | |
fp = K.sum(y_neg * y_pred_pos) | |
fn = K.sum(y_pos * y_pred_neg) | |
numerator = (tp * tn - fp * fn) | |
denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)) | |
return numerator / (denominator + K.epsilon()) | |
def precision(y_true, y_pred): | |
"""Precision metric. | |
Only computes a batch-wise average of precision. | |
Computes the precision, a metric for multi-label classification of | |
how many selected items are relevant. | |
""" | |
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 recall(y_true, y_pred): | |
"""Recall metric. | |
Only computes a batch-wise average of recall. | |
Computes the recall, a metric for multi-label classification of | |
how many relevant items are selected. | |
""" | |
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 f1(y_true, y_pred): | |
def recall(y_true, y_pred): | |
"""Recall metric. | |
Only computes a batch-wise average of recall. | |
Computes the recall, a metric for multi-label classification of | |
how many relevant items are selected. | |
""" | |
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): | |
"""Precision metric. | |
Only computes a batch-wise average of precision. | |
Computes the precision, a metric for multi-label classification of | |
how many selected items are relevant. | |
""" | |
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 | |
precision = precision(y_true, y_pred) | |
recall = recall(y_true, y_pred) | |
return 2*((precision*recall)/(precision+recall)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment