Python tensorflow精度度量返回值的含义

Python tensorflow精度度量返回值的含义,python,tensorflow,metrics,Python,Tensorflow,Metrics,我对模块tf.metrics(例如)的函数返回的值有点困惑 一段简单的代码,其中我使用tf.metrics.accurity和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, fa

我对模块tf.metrics(例如)的函数返回的值有点困惑

一段简单的代码,其中我使用tf.metrics.accurity和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.accurity(update_op)的第二个返回值是函数调用次数的平均精度。然而,我无法理解第一个值,它应该代表准确性。为什么它与我自己计算的精度值不同?难道没有办法获得精度的非累积值吗

提前谢谢

import tensorflow as tf
from sklearn.metrics import accuracy_score

# 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(predictions=y_p, labels=y_t)

# Calculate accuracy, precision, recall and F1 score.
accuracy = (tp + tn) / (tp + fp + fn + tn)

with tf.Session() as sess:
    for i in range(4):
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())


        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]
        print('accuracy_score', accuracy_score([0,0,0,0,0,0,0,0,0,0], yop))
        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))
        print()
输出:

accuracy_score 1.0
TF accuracy: (0.0, 1.0)
My accuracy: 1.0

accuracy_score 0.8
TF accuracy: (0.0, 0.8)
My accuracy: 0.8

accuracy_score 0.6
TF accuracy: (0.0, 0.6)
My accuracy: 0.6

accuracy_score 0.4
TF accuracy: (0.0, 0.4)
My accuracy: 0.4
只需在循环中移动
tf.local\u variables\u initializer()
,即可确保精度度量张量中的值重新初始化

它为什么起作用?

根据文件

精度函数创建两个局部变量,total和count 用于计算预测匹配的频率 标签

如果我们不重新初始化局部变量,那么来自上一次迭代的值将保留在其中,从而导致错误的结果

另一种方法是使用:

tf.contrib.metrics.accurity
而不是
tf.metrics.accurity
。但这会在末尾给出一些残值,如
0.800000011920929
,而不是
0.8
。OP在评论中也指出了这一点

资料来源:


谢谢,将初始化操作移到循环中确实起到了作用。然后,返回的第一个值是平均精度的前一个值(因此,在重新初始化后,它等于
0.0
)。但仍然不确定为什么有人会需要它,而不是准确度的实际值。请注意,
tf.contrib.metrics.accurity
似乎很酷,我将把这些信息添加到答案中。工作正在进行,根据我所读的,讨论使其更直观。@pangasio但这回答了你的问题,对吗?初始化部分?明白。是的,它确实回答了我的问题,我把它标记为已接受的答案。再次感谢。谢谢@pangasio:)