我正在运行具有张量流的卷积神经网络的第一次测试.我使用编程指南中的队列运行程序调整了推荐的方法(参见下面的会话定义).输出是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()
Run Code Online (Sandbox Code Playgroud)
我的问题是每个批次的计算精度,我想为每个时期计算.我需要执行以下操作:初始化epoch_accuracy张量,为纪元中的每个计算的批处理精度将其添加到epoch_accuracy.在时代结束时显示计算的训练集准确度.但是我没有找到我实现的这个队列线程的任何这样的例子(实际上是TensorFlow推荐的方法).有人可以帮忙吗?
要计算数据流(这里是您的批处理序列)的准确性,可以tf.metrics.accuracy在tensorflow中使用该函数。在这里查看其文档
您可以这样定义op
_, accuracy = tf.metrics.accuracy(y_true, y_pred)
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过以下方式更新准确性:
sess.run(accuracy)
Run Code Online (Sandbox Code Playgroud)
PS:tf.metrics(auc,recall等)中的所有功能均支持流传输
| 归档时间: |
|
| 查看次数: |
2644 次 |
| 最近记录: |