使用keras

时间:2018-02-12 23:12:32

标签: tensorflow machine-learning callback keras precision-recall

我想根据keras中的回调创建自定义指标。在browsing issues in Keras期间,我遇到了f1指标的以下代码:

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])

但回调如何恢复准确性?我想实施unweighted recall = (recall class1 + recall class2)/2。我可以想到以下代码,但希望有任何帮助来完成它

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])

0 个答案:

没有答案