python - tensorflow 精度指标返回值的含义

标签 python tensorflow metrics

我对模块 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)的第二个返回值是函数调用次数的平均准确度。但是,我无法理解第一个值,它应该代表准确性。为什么它与我自己计算的准确度值不同?有没有办法获取精度的非累积值?

提前致谢。

最佳答案

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_variables_initializer() 即可确保精度度量张量中的值得到重新初始化。

为什么有效?

根据文档

The accuracy function creates two local variables, total and count that are used to compute the frequency with which predictions matches labels.

如果我们不重新初始化局部变量,那么先前迭代的值将保留在其中,从而导致您遇到的错误结果。

另一种方法是使用:

tf.contrib.metrics.accuracy 而不是 tf.metrics.accuracy。但这在最后给出了一些剩余值(value),例如 0.800000011920929 而不是 0.8。也是deprecated正如 OP 在评论中指出的那样。

来源:

https://www.tensorflow.org/api_docs/python/tf/metrics/accuracy

https://github.com/tensorflow/tensorflow/issues/3971

How to properly use tf.metrics.accuracy?

关于python - tensorflow 精度指标返回值的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48909330/

相关文章:

python - 如何使用xlrd将Python日期转换为Excel日期(属性xlrd.xldate_from_date_tuple不存在)

python - Apache/Django/mod_wsgi - [Errno 13] 权限被拒绝 :

python - 回调问题 FailedPreconditionError

python - 为什么我的 GradientDescentOptimizer 会产生 NaN?

c# - 是否有可用于跟踪用户事件的框架?

java - Flink webUI 中的 "No Metrics "

prometheus - 如何计算 Prometheus Grafana 中的正常运行时间百分比或停机时间百分比

python - 使用 PIL 在另一个图像之上的图像上写文本

python - 如何打印出正确的预测类别?

Python,如何在列表末尾不需要额外的空间?