Q:

How do I get the true negatives from y_true and y_pred?

Is it possible to get true negative from y_true and y_pred tensors in Keras?
I know we can get true positives from the following code:
true_positive = K.sum(y_true*y_pred)

How to do the same for the true negative?

A:

Here's an example of how you can extract the True Positive and the True Negative values of your confusion matrix:
def confusion(y_true, y_pred):
    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) / K.sum(y_pos)
    tn = K.sum(y_neg * y_pred_neg) / K.sum(y_neg)
    return {'true_pos': tp, 'true_neg': tn}

In case you are facing some NaN values in your output, try adding an epsilon to the denominators which will solve your problem.
Answer came from this link.
Hope it helped you.

