【发布时间】:2018-02-21 15:14:13
【问题描述】:
我对模块 tf.metrics 的函数返回的值有点困惑(例如 tf.metrics.accuracy)。
一段简单的代码,我在其中使用 tf.metrics.accuracy 并使用 tp、tn、fp 和 fn 计算准确度。
import tensorflow as tf
# true and predicted tensors
y_p = tf.placeholder(dtype=tf.int64)
y_t = tf.placeholder(dtype=tf.int64)
# Count true positives, true negatives, false positives and false negatives.
tp = tf.count_nonzero(y_p * y_t)
tn = tf.count_nonzero((y_p - 1) * (y_t - 1))
fp = tf.count_nonzero(y_p * (y_t - 1))
fn = tf.count_nonzero((y_p - 1) * y_t)
acc = tf.metrics.accuracy(y_p, y_t)
# Calculate accuracy, precision, recall and F1 score.
accuracy = (tp + tn) / (tp + fp + fn + tn)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
for i in range(4):
if i == 0:
yop = [0,0,0,0,0,0,0,0,0,0]
elif i == 1:
yop = [0,0,0,0,0,0,0,0,1,1]
elif i == 2:
yop = [1,1,1,0,0,0,0,0,0,1]
else:
yop = [0,1,1,1,1,1,1,0,0,0]
tf_a = sess.run(acc, feed_dict={y_p: [0,0,0,0,0,0,0,0,0,0], y_t: yop})
my_a = sess.run(accuracy, feed_dict={y_p: [0,0,0,0,0,0,0,0,0,0], y_t: yop})
print("TF accuracy: {0}".format(tf_a))
print("My accuracy: {0}".format(my_a))
哪个输出
TF accuracy: (0.0, 1.0)
My accuracy: 1.0
TF accuracy: (1.0, 0.9)
My accuracy: 0.8
TF accuracy: (0.9, 0.8)
My accuracy: 0.6
TF accuracy: (0.8, 0.7)
My accuracy: 0.4
我了解 tf.metrics.accuracy 的第二个返回值(update_op)是函数调用次数的平均准确度。但是,我无法理解第一个值,它应该代表准确性。为什么它与我自己计算的精度值不同?有没有办法获得精度的非累积值?
提前致谢。
【问题讨论】:
标签: python tensorflow metrics