python - tf.metrics.accuracy 与实际准确度不匹配

标签 python tensorflow

我正在尝试使用 TensorFlow(而不是 Keras)重现 Coursera ML 类(class)的 NN 练习。

我发现使用tf.metrics.accuracy计算准确度给出的结果低于我计算时的准确度。

相关代码是:

accuracy, update_op = tf.metrics.accuracy(labels=y, predictions=tf.argmax(tf.sigmoid(output), axis=1))
...
# in session:
acc = sess.run(accuracy, feed_dict={tf_x: X, tf_y: y})
sess.run(update_op, feed_dict={tf_x: X, tf_y: y})
print(f'step {step} - accuracy: {acc}')
...
# real accuracy
predictions = sess.run(tf.argmax(tf.sigmoid(output), axis=1), feed_dict={tf_x: X})
pred_y = predictions == y
print(f'Training Set Accuracy after training: {np.mean(pred_y) * 100}%')

甚至可以有 30% 的差异(即 acc 为 0.5,实际精度为 0.8)

我做错了什么吗?

请注意,如果我这样做:

equal = tf.equal(tf.cast(tf.argmax(tf.sigmoid(output), 1), tf.int32), y)
acc_op = tf.reduce_mean(tf.cast(equal, tf.float32))
acc = sess.run(acc_op, feed_dict={tf_x: X, tf_y: y})

我得到了相同的结果...那么 tf.metrics.accuracy 是否以其他方式计算?

最佳答案

解决方案:第一次调用 sess.run(update_op, feed_dict) ,然后sess.run(accuracy) 。如果喂入新批处理,并且需要该批处理的准确性,则必须首先重置一些隐藏变量 - 工作流程如下:

accuracy, update_op = tf.metrics.accuracy(tf_labels, tf_predictions, scope="my_metrics")
running_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="my_metrics")
running_vars_initializer = tf.variables_initializer(var_list=running_vars)
for i in range(num_batches):
    # explicitly initialize/reset 'total' and 'count' to 0
    sess.run(running_vars_initializer) 

    # feed labels and predictions at i-th batch to update_ops
    feed_dict={tf_labels: y[i], tf_predictions: tf.argmax(tf.sigmoid(output[i]), axis=1)}
    session.run(update_op, feed_dict=feed_dict)

    # compute and print accuracy from current 'total' and 'count'
    print('Batch {} accuracy: {}'.format(i, session.run(accuracy)))

<小时/> 详细信息:tf.metrics.accuracy使用两个运行时变量 total (正确预测的数量)和 count (输入的标签数量),在幕后本地初始化。 accuracy 更新一次 update_op被调用 - 步骤:

  • totalcount初始化为零
  • sess.run(update_op, feed_dict) --> totalcountfeed_dict 更新
  • sess.run(accuracy) --> accuracy使用当前 totalcount计算指标
  • sess.run(accuracy, feed_dict) --> accuracy使用当前 totalcount计算指标

最后两个说的是,feed_dict实际上没有做任何改变accuracy ; accuracy运行于 totalcount ,仅通过 update_op 更新。最后,

  • sess.run(accuracy, ...)是否没有重置totalcount到0

这很大程度上就是 total 的原因和count完全使用 - 用于可扩展性;它通过保存运行历史记录来计算太大而无法一次性放入内存的数据指标。

最后,您的占位符逻辑看起来不对 - 您将数据输入 tf_xtf_y ,但在 tf.metrics.accuracy(...) 内任何地方都找不到。 - 但这是一个简单的解决方法。

<小时/> 引用文献/进一步阅读:StackOverflow ,好blog entry

关于python - tf.metrics.accuracy 与实际准确度不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58007918/

相关文章:

python - 如何返回 top-n 概率及其相关类别?

Python 语法 : using loops inside a timeit statement

python - Tensorflow 服务 REST API 抛出错误

python - 在 Tensorflow 中将函数按元素应用于具有不同参数的张量

python - 如何在 tensorflow 中的张量上自定义逐元素函数?

python - "import pandas.io.data as web "给我一个错误,说没有 pandas.io.data 的模块名称

python - 在 OpenCV python 中使用 seamlessClone 生成具有 "ghost"个对象的图像

python - 为什么会出现这个错误 "input_2_1:0 is both fed and fetched"?

python-3.x - TPU : double, 不支持的数据类型由输出 IteratorGetNext:0 引起

python - 有效地更改列表列表中的值