使用队列和多线程读取数据架构--加快训练速度、异步读取和训练
1.创建协调器tf.train.coordinator实例,系统器用于协调多个线程同时停止、报告出错和等待线程停止。示例代码:
coord = Coordinator()
2.利用tf.train.queue_runner创建线程,并将协调器传入每一个线程。并在线程中,完成所需工作。典型带有协调器的线程示例代码:
threads = tf.train.start_queue_runners(coord=coord)
try:
for i in range(5000):
# ... without sampling from Python and without a feed_dict !
_, loss = sess.run([train_op, loss_op])
# We regularly check the loss
if i % 500 == 0:
print('iter:%d - loss:%f' % (i, loss))
except tf.errors.OutOfRangeError:
print 'Done training -- epoch limit reached'
finally:
# When done, ask the threads to stop.
coord.request_stop()
3.使用coord.join等待线程停止,开始主线程示例。
coord.join(threads)
代码转载于https://zhuanlan.zhihu.com/p/29729954
参考
1.https://medium.com/@penolove15/threading-and-queues-ref-68551db634ff
2.https://zhuanlan.zhihu.com/p/29729954