1

I wanted to create a custom metric based on callback in keras. While browsing issues in Keras, I came across the following code for f1 metric:

class Metrics(keras.callbacks.Callback):
    def on_epoch_end(self, batch, logs={}):
        predict = np.asarray(self.model.predict(self.validation_data[0]))
        targ = self.validation_data[1]
        self.f1s=f1(targ, predict)
        return
metrics = Metrics()
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=[X_test,y_test], 
       verbose=1, callbacks=[metrics])

But how is the callback returning the accuracy? I wanted to implement unweighted recall = (recall class1 + recall class2)/2. I can think of the following code but would appreciate any help to complete it

from sklearn.metrics import recall_score
class Metrics(keras.callbacks.Callback):
    def on_epoch_end(self, batch, logs={}):
        predict = np.asarray(self.model.predict(self.validation_data[0]))
        targ = self.validation_data[1]
        # --- what to store the result in?? ---
        self.XXXX=recall_score(targ, predict, average='macro')
        # we really dont need to return anything ??
        return
metrics = Metrics()
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=[X_test,y_test], 
       verbose=1, callbacks=[metrics])
Aditya
  • 5,509
  • 4
  • 31
  • 51
  • Please refer https://stackoverflow.com/questions/48742662/custom-macro-for-recall-in-keras. It also answers this question! – Aditya Feb 15 '18 at 18:15

0 Answers0