How to calculate F1 Macro in Keras?

后端 未结 6 1700
逝去的感伤
逝去的感伤 2020-11-28 23:13

i\'ve tried to use the codes given from Keras before they\'re removed. Here\'s the code :

def precision(y_true, y_pred):
    true_positives = K.sum(K.round(K         


        
6条回答
  •  忘掉有多难
    2020-11-28 23:18

    I also suggest this work-around

    • install keras_metrics package by ybubnov
    • call model.fit(nb_epoch=1, ...) inside a for loop taking advantage of the precision/recall metrics outputted after every epoch

    Something like this:

        for mini_batch in range(epochs):
            model_hist = model.fit(X_train, Y_train, batch_size=batch_size, epochs=1,
                                verbose=2, validation_data=(X_val, Y_val))
    
            precision = model_hist.history['val_precision'][0]
            recall = model_hist.history['val_recall'][0]
            f_score = (2.0 * precision * recall) / (precision + recall)
            print 'F1-SCORE {}'.format(f_score)
    

提交回复
热议问题