I found this function here How to calculate F1 Macro in Keras? but i am not sure how i can write the same way for specificity? I am using tensorflow backend for keras.
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
I tried this solution but it gives error,
def compute_binary_specificity(y_pred, y_true):
"""Compute the confusion matrix for a set of predictions.
Returns
-------
out : the specificity
"""
TN = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 0)
FP = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 1)
# as Keras Tensors
TN = K.sum(K.variable(TN))
FP = K.sum(K.variable(FP))
specificity = TN / (TN + FP + K.epsilon())
return specificity
Error: InvalidArgumentError: You must feed a value for placeholder tensor 'dense_95_input' with dtype float and shape [?,140] [[Node: dense_95_input = Placeholderdtype=DT_FLOAT, shape=[?,140], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
and points here
---> TN = np.logical_and(K.eval(y_true) == 0, K.eval(y_pred) == 0)