如何使用tensorflow的队列和多线程读取数据

使用队列和多线程读取数据架构--加快训练速度、异步读取和训练

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值