Keras计算F1_score

实现代码:

from keras.callbacks import Callback
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score

class Metrics(Callback):
    def on_train_begin(self, logs={}):
        self.val_f1s = []
        self.val_recalls = []
        self.val_precisions = []

    def on_epoch_end(self, epoch, logs={}):
        val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()##.model
        val_targ = self.validation_data[1]###.model
        _val_f1 = f1_score(val_targ, val_predict,average='micro')
        _val_recall = recall_score(val_targ, val_predict,average=None)###
        _val_precision = precision_score(val_targ, val_predict,average=None)###
        self.val_f1s.append(_val_f1)
        self.val_recalls.append(_val_recall)
        self.val_precisions.append(_val_precision)
        #print("— val_f1: %f — val_precision: %f — val_recall: %f" %(_val_f1, _val_precision, _val_recall))
        print("— val_f1: %f "%_val_f1)
        return

f1=Metrics()
hist=cnn_net.fit(x_train,y_train,batch_size=batch_size,epochs=35,verbose=1,validation_data=(x_train,y_train),callbacks=[f1])#50validation_split=0.3

问题一:用print输出_val_f1, _val_precision, _val_recall时,会报错:TypeError: only length-1 arrays can be converted to Python scalars

目前还不知道原因,但是只输出 _val_f1却不会报错。

问题二:直接调用别人代码

_val_f1 = f1_score(val_targ, val_predict)

_val_recall = recall_score(val_targ, val_predict)

_val_precision = precision_score(val_targ, val_predict) 

会报错:ValueError: Target is multilabel-indicator but average='binary'. Please choose another average setting.

添加参数 average后,未报错。参考sklearn metrics for multiclass classification

问题三:计算F1要添加validation_data,我的代码中为了方便直接用的训练集。直接调用

self.model.validation_data[0]  

self.model.validation_data[1]

会报错:AttributeError: 'Sequential' object has no attribute 'validation_data'

应该将其改为self.validation_data[0]   self.validation_data[1],参考Sequential has no attribution “validation_data”

代码参考:keras实现f1_score(多分类、二分类),keras里面如何计算f1-score

你可能感兴趣的:(Keras)