tensorflow - 计算整个训练集的准确率

标签 tensorflow

我正在使用 tensorflow 运行卷积神经网络的第一次测试。我采用了编程指南中的队列运行程序的推荐方法(请参阅下面的 session 定义)。输出是来自 cnn 的最后结果(这里仅给出最后一步)。 label_batch_vector 是训练标签批处理。

output = tf.matmul(h_pool2_flat, W_fc1) + b_fc1
label_batch_vector = tf.one_hot(label_batch, 33)

correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(label_batch_vector, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())

print_accuracy = tf.Print(accuracy, [accuracy])

# Create a session for running operations in the Graph.
sess = tf.Session()

# Initialize the variables (like the epoch counter).
sess.run(init_op)

# Start input enqueue threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

try:
    while not coord.should_stop():
        # Run training steps or whatever
        sess.run(train_step)
        sess.run(print_accuracy)

except tf.errors.OutOfRangeError:
    print('Done training -- epoch limit reached')
finally:
    # When done, ask the threads to stop.
    coord.request_stop()

# Wait for threads to finish.
coord.join(threads)
sess.close()

我的问题是,准确度是针对每个批处理进行计算的,我希望针对每个时期进行计算。我需要执行以下操作:初始化 epoch_accuracy 张量,对于该纪元中计算的每个批处理精度,将其添加到 epoch_accuracy 中。在纪元结束时显示计算出的训练集准确性。但是,我在我实现的队列线程中没有找到任何此类示例(这实际上是 TensorFlow 推荐的方法)。有人可以帮忙吗?

最佳答案

要计算数据流(此处为批处理序列)的准确性,您可以使用tensorflow中的tf.metrics.accuracy函数。请参阅其文档 here

你像这样定义操作

_, accuracy = tf.metrics.accuracy(y_true, y_pred)

然后您可以通过以下方式更新准确性:

sess.run(accuracy)

PS:tf.metrics中的所有函数(auc、recall等)都支持流式传输

关于tensorflow - 计算整个训练集的准确率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46646984/

相关文章:

python - tensorflow API 检测盒及评测

python - Tensorflow DNNRegressor 多输出

python - 属性错误:int object has no attribute name

python - 我如何知道 keras RNN 输入数据的正确格式?

python - Tensorflow Inception v3 再训练 - 将文本/标签附加到单个图像

python - 导入错误: cannot import name model_fn: Tensorflow

tensorflow - 使用占位符值 reshape 张量

c++ - CMake 在 Windows 10 上构建 Tensorflow C++ 错误 : Test COMPILER_OPT_ARCH_NATIVE_SUPPORTED - failed

python - GPU计算能力3.0,但最低要求的Cuda能力为3.5

machine-learning - Google Cloud 上使用 GPU 的 Tensorflow