Reputation: 322
I do classification task with Keras, I make simple custom loss function in Keras and it works
import keras.backend as K
def customLoss(yTrue,yPred):
return K.abs(yTrue-yPred)
to make more complex loss function that i want, i need to calculate True Positive, True Negative , False Positive , False Negative
How to calculate them ?
i cant calculate them because i dont know the type of yTrue and yPred . Are they 2D array or list or anything else. if i know, maybe i can calculate TP,TN,FP,FN using for , like this:
TP=0
for x,y in zip(yTrue,yPred):
if x == 1 and y > 0.5:
TP=TP+1
Upvotes: 1
Views: 1981
Reputation: 804
According to the Keras Documentation the data types of yTrue/yPred are TensorFlow/Theano tensor depending on the backend you are using.
Therefore, you cannot use a for loop for the loss function, otherwise you will get an error.
But you can use logical and for this matter:
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)
After that you can add them up:
TN = K.sum(K.variable(TN))
FP = K.sum(K.variable(FP))
Upvotes: 2