# How to calculate accuracy, precision and recall, and F1 score for a keras sequential model?

I want to calculate accuracy, precision and recall, and F1 score for multi-class classification problem. I am using these lines of code mentioned below.

from keras import backend as K
def precision(y_true, y_pred, average='None'):
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, average='micro'):
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, average='weighted'):
def recall(y_true, y_pred):
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):
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 + K.epsilon()))


Don't know why the value of recall is 1 for both testing and training. Please help me to calculate accuracy, precision and recall, and F1 score for multi-class classification using the Keras model.

• how do you want to compute the recall (and other)? one recall that is the mean of the recall for each class? or do you want to get a recall for each class? (the recall you compute is for only two class, for a related question, see: stackoverflow.com/questions/56261014/… ) – Frayal Nov 13 '19 at 13:39
• I want to calculate recall and precision for each class and we have a total number of classes are 12. As mentioned code is for binary classification and I want to write this type of code for multi-class. Don't know how to do so. – user85181 Nov 13 '19 at 15:37

if you want to compute this for every class then:

def recall(y_true, y_pred,class_to_analyse):
pred = K.argmax(y_pred)
true = K.argmax(y_true)
p = K.cast(K.equal(pred,class_to_analyse),'int32')
t = K.cast(K.equal(true,class_to_analyse),'int32')
# Compute the true positive
common = K.sum(K.dot(K.reshape(t,(1,-1)),K.reshape(p,(-1,1))))
# divide by all positives in t
recall = common/ (K.sum(t) + K.epsilon)
return recall

def precision(y_true, y_pred,class_to_analyse):
pred = K.argmax(y_pred)
true = K.argmax(y_true)
p = K.cast(K.equal(pred,class_to_analyse),'int32')
t = K.cast(K.equal(true,class_to_analyse),'int32')
# Compute the true positive
common = K.sum(K.dot(K.reshape(t,(1,-1)),K.reshape(p,(-1,1))))
# divide by all positives in t
precision = common/ (K.sum(p) + K.epsilon)
return precision

def fbeta(y_true, y_pred,class_to_analyse):
beta = 1 # for f1 score
precision = precision(y_true, y_pred,class_to_analyse)
recall = recall(y_true, y_pred,class_to_analyse)

beta_squared = beta ** 2
return (beta_squared + 1) * (precision * recall) / (beta_squared * precision + recall)


to make it work you need to create a list of function with a fixed class (ex: recall_1(y_true, y_pred) = recall(y_true, y_pred,class_to_analyse = 1)

• Thanks a lot. I will try this. – user85181 Nov 14 '19 at 14:22