NLP-準確率、精確率、召回率和F1值
阿新 • • 發佈:2018-12-11
記錄準確率(Accuracy)、精確率(Precision)、召回率(Recall)和F1值(F-Measure)計算公式,和如何使用TensorFlow實現
一、計算公式
二、TensorFlow實現
# Accuracy with tf.name_scope("accuracy"): correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy") # TODO: Reconsider the metrics calculation # Number of correct predictions with tf.name_scope("num_correct"): correct = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) self.num_correct = tf.reduce_sum(tf.cast(correct, "float"), name="num_correct") # Calculate Fp with tf.name_scope("fp"): fp = tf.metrics.false_positives(labels=tf.argmax(self.input_y, 1), predictions=self.predictions) self.fp = tf.reduce_sum(tf.cast(fp, "float"), name="fp") # Calculate Fn with tf.name_scope("fn"): fn = tf.metrics.false_negatives(labels=tf.argmax(self.input_y, 1), predictions=self.predictions) self.fn = tf.reduce_sum(tf.cast(fn, "float"), name="fn") # Calculate Recall with tf.name_scope("recall"): self.recall = self.num_correct / (self.num_correct + self.fn) # Calculate Precision with tf.name_scope("precision"): self.precision = self.num_correct / (self.num_correct + self.fp) # Calculate F1 with tf.name_scope("F1"): self.F1 = (2 * self.precision * self.recall) / (self.precision + self.recall) # Calculate AUC with tf.name_scope("AUC"): self.AUC = tf.metrics.auc(self.softmax_scores, self.input_y, name="AUC")