我正在对具有张量流的卷积神经网络进行首次测试。我使用了编程指南中推荐的队列运行器方法(请参见下面的会话定义)。输出是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中计算的批次精度,将其添加到epoch_accuracy中。在纪元末显示计算出的训练集精度。但是我没有在我实现的this队列线程中找到任何这样的示例(实际上是TensorFlow推荐的方法)。有人可以帮忙吗?
要计算数据流(这里是您的批处理序列)的准确性,可以tf.metrics.accuracy
在tensorflow中使用该函数。在这里查看其文档
您可以这样定义op
_, accuracy = tf.metrics.accuracy(y_true, y_pred)
然后,您可以通过以下方式更新准确性:
sess.run(accuracy)
PS:tf.metrics
(auc,recall等)中的所有功能均支持流传输
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句