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)
评论列表
文章目录