How to add function (Get F1-score) in Keras metrics and record F1 value after each epoch?

from keras.callbacks import Callback,ModelCheckpoint
from keras.models import Sequential,load_model
from keras.layers import Dense, Dropout
from keras.wrappers.scikit_learn import KerasClassifier
import keras.backend as K
def get_f1(y_true, y_pred): #taken from old keras source code
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)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
recall = true_positives / (possible_positives + K.epsilon())
f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
return f1_val
## Define Model

input_shape = X_train_tfidf.shape[1]

def mlp_v2():
mdl = Sequential()
mdl.add(Dense(512, init='glorot_uniform', activation='relu',input_shape=(input_shape,)))
mdl.add(Dense(128, init='glorot_uniform', activation='relu'))
mdl.add(Dense(1, activation='sigmoid'))
mdl.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=[get_f1])
return mdl

mode_path = '../models/mlp_v2.h5'
callbacks = [ModelCheckpoint(filepath=mode_path, save_best_only=True)]
## Run Model
estimator = KerasClassifier(build_fn=mlp_v2, epochs=5, batch_size=128)
history =, y_train,\
Model Structure, F1 score on Training and validation data



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store