trainer_nn.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:deep_learning 作者: wecliqued 项目源码 文件源码
def _calculate_f1_score(self):
        '''
        F1 score is used instead of accuracy in case of strongly biased classes. Google it up :)
        :return: F1 score, what else?!?
        '''
        with tf.name_scope('stats'):
            prediction = self.prediction
            y = self.y
            with tf.name_scope('true_positive'):
                tp = tf.reduce_sum(tf.to_int32(tf.logical_and(tf.equal(prediction, y), tf.equal(prediction, 1))))
            with tf.name_scope('true_negative'):
                tn = tf.reduce_sum(tf.to_int32(tf.logical_and(tf.equal(prediction, y), tf.equal(prediction, 0))))
            with tf.name_scope('false_positive'):
                fp = tf.reduce_sum(tf.to_int32(tf.logical_and(tf.not_equal(prediction, y), tf.equal(prediction, 1))))
            with tf.name_scope('false_negative'):
                fn = tf.reduce_sum(tf.to_int32(tf.logical_and(tf.not_equal(prediction, y), tf.equal(prediction, 0))))

            with tf.name_scope('precision'):
                self.precision = tp / (tp + fp)
            with tf.name_scope('recall'):
                self.recall = tp / (tp + fn)
            with tf.name_scope('accuracy'):
                self.accuracy = (tp+tn) / (tp+tn+fp+fn)

            with tf.name_scope('f1_score'):
                self.f1_score = 2 * self.precision * self.recall / (self.precision + self.recall)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号