0

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)

JAbr
  • 312
  • 2
  • 12
  • Calling `K.eval(y_pred)` is equivalent to running tensor `y_pred`, and performing this requires to specify a value for each input tensor for which `y_pred` depends on. Ideally you would use backend operations instead of numpy operations (you wouldn't call `K.eval`). – rvinas Nov 05 '18 at 15:33
  • Yeah you are right, i actually found a solution and added [here](https://datascience.stackexchange.com/questions/33587/keras-custom-loss-function-as-true-negatives-by-true-negatives-plus-false-posit/40746#40746) – JAbr Nov 06 '18 at 06:56

0 Answers0