0
点赞
收藏
分享

微信扫一扫

keras中计算precision和recall的一点思考

火热如冰 2022-03-27 阅读 59
算法python
需要对模型的precision和recall进行衡量,希望使用metrics在训练的时候将这两个指标体现出来。    keras2.0中已经删除了F1score、precision和recall的计算。按照[github上文章](https://github.com/keras-team/keras/issues/5400)的说法,可以自己编写batch_wise的precision和recall。自定义了如下的代码。
def precision(y_true,y_pred,n=0):#精准率
    threshold = K.constant(n)
    true_positives = K.sum(K.cast(K.greater(y_true,threshold)&K.greater(y_pred,threshold),tf.float32))
    possible_positives = K.sum(K.cast(K.greater(y_pred,threshold),tf.float32))
    precision = true_positives / (possible_positives + K.epsilon())
    return precision
def recall(y_true,y_pred,n=0):#召回率
    threshold = K.constant(n)
    true_positives = K.sum(K.cast(K.greater(y_true,threshold)&K.greater(y_pred,threshold),tf.float32))
    possible_positives = K.sum(K.cast(K.greater(y_true,threshold),tf.float32))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall
代码设置了个阈值n,精准率是预测值中大于n的数量中,真实值也大于n的比例。召回率是真实值大于n的数量中,预测值也大于n的比例。
然而,越测试越感觉不对,batch_wise的结果是我们想要的吗?问题出在一个batch中TP都为0的情况下,此时precision的计算结果为0;而分母并非全都非0的,为0的数字不该记到分母的总和里面去。当batch很小,或者准确率很低的情况下问题就会很突出。显示的结果不能反映真实的情况。
所以还是要从callback函数入手去解决这个问题。
举报

相关推荐

0 条评论