美文网首页TensorFlow
Keras每次迭代计算F1-Score

Keras每次迭代计算F1-Score

作者: azim | 来源:发表于2018-08-29 20:43 被阅读470次
    from keras import backend as K
     
    def f1(y_true, y_pred):
        def recall(y_true, y_pred):
            """Recall metric.
            Only computes a batch-wise average of recall.
            Computes the recall, a metric for multi-label classification of
            how many relevant items are selected.
            """
            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)))
            recall = true_positives / (possible_positives + K.epsilon())
            return recall
     
        def precision(y_true, y_pred):
            """Precision metric.
            Only computes a batch-wise average of precision.
            Computes the precision, a metric for multi-label classification of
            how many selected items are relevant.
            """
            true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
            predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
            precision = true_positives / (predicted_positives + K.epsilon())
            return precision
        precision = precision(y_true, y_pred)
        recall = recall(y_true, y_pred)
        return 2*((precision*recall)/(precision+recall+K.epsilon()))
     
     
    model.compile(loss='binary_crossentropy',
              optimizer= "adam",
              metrics=[f1])
    

    原文

    相关文章

      网友评论

        本文标题:Keras每次迭代计算F1-Score

        本文链接:https://www.haomeiwen.com/subject/lbtlwftx.html