真实 1 | 真实 0 | |
---|---|---|
预测 1 | True Positive(TP)真阳性 | False Positive(FP)假阳性 |
预测 0 | False Negative(FN)假阴性 | True Negative(TN)真阴性 |
import tensorflow as tf
def f1(y_hat, y_true, model='multi'):
'''
输入张量y_hat是输出层经过sigmoid激活的张量
y_true是label{0,1}的集和
model指的是如果是多任务分类,single会返回每个分类的f1分数,multi会返回所有类的平均f1分数(Marco-F1)
如果只是单个二分类任务,则可以忽略model
'''
epsilon = 1e-7
y_hat = tf.round(y_hat)#将经过sigmoid激活的张量四舍五入变为0,1输出
tp = tf.reduce_sum(tf.cast(y_hat*y_true, 'float'), axis=0)
#tn = tf.sum(tf.cast((1-y_hat)*(1-y_true), 'float'), axis=0)
fp = tf.reduce_sum(tf.cast((1-y_hat)*y_true, 'float'), axis=0)
fn = tf.reduce_sum(tf.cast(y_hat*(1-y_true), 'float'), axis=0)
p = tp/(tp+fp+epsilon)#epsilon的意义在于防止分母为0,否则当分母为0时python会报错
r = tp/(tp+fn+epsilon)
f1 = 2*p*r/(p+r+epsilon)
f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
if model == 'single':
return f1
if model == 'multi':
return tf.reduce_mean(f1)
import tensorflow as tf
y_true = tf.constant([[1,1,0,0,1], [1,0,1,1,0], [0,1,1,0,0]])
y_hat = tf.constant([[0,1,1,1,1], [1,0,0,1,1], [1,0,1,0,0]])
with tf.Session() as sess:
f1 = f1(y_hat, y_true)
print('F1 score:', sess.run(f1))
F1 score: 0.5999999
(2019.1.12更新)
import numpy as np
def f1(y_hat, y_true, THRESHOLD=0.5):
'''
y_hat是未经过sigmoid函数激活的
输出的f1为Marco-F1
'''
epsilon = 1e-7
y_hat = y_hat>THRESHOLD
y_hat = np.int8(y_hat)
tp = np.sum(y_hat*y_true, axis=0)
fp = np.sum((1-y_hat)*y_true, axis=0)
fn = np.sum(y_hat*(1-y_true), axis=0)
p = tp/(tp+fp+epsilon)#epsilon的意义在于防止分母为0,否则当分母为0时python会报错
r = tp/(tp+fn+epsilon)
f1 = 2*p*r/(p+r+epsilon)
f1 = np.where(np.isnan(f1), np.zeros_like(f1), f1)
return np.mean(f1)
[1] https://baike.baidu.com/item/F1分数/13864979?fr=aladdin
[2] https://www.kaggle.com/guglielmocamporese/macro-f1-score-keras
[3] 分类问题的几个评价指标(Precision、Recall、F1-Score、Micro-F1、Macro-F1)