首页
学习
活动
专区
工具
TVP
发布

TensorFlow中的多线程

正文共1699个字,2张图,预计阅读时间5分钟。

TensorFlow提供两个类帮助实现多线程,一个是tf.train.Coordinator,另一个是tf.train.QueueRunner。Coordinator主要用来实现多个线程同时停止,QueueRunner用来创建一系列线程。

Coordinator

根据官方文档,Coordinator主要有三个方法:

1、tf.train.Coordinator.should_stop: returns True if the threads should stop.

2、tf.train.Coordinator.request_stop: requests that threads should stop.

3、tf.train.Coordinator.join: waits until the specified threads have stopped.

接下来我们实验Coordinator,下面的代码主要实现每个线程独立计数,当某个线程达到指定值的时候,所有线程终止:

#encoding=utf-8

importthreading

importnumpyasnp

importtensorflowastf

#创建一个函数实现多线程,参数为Coordinater和线程号

deffunc(coord, t_id):

count =

whilenotcoord.should_stop():#不应该停止时计数

print('thread ID:',t_id,'count =', count)

count +=1

if(count ==5):#计到5时请求终止

coord.request_stop()

coord = tf.train.Coordinator()

threads = [threading.Thread(target=func, args=(coord, i))foriinrange(4)]

#开始所有线程

fortinthreads:

t.start()

coord.join(threads)#等待所有线程结束

运行结果如下,当0号线程打印出4时,其他线程不再计数,程序终止。

QueueRunner

QueueRunner的作用是创建一些重复进行enqueue操作的线程,它们通过coordinator同时结束。

#encoding=utf-8

import numpy as np

import tensorflow as tfbatch_size = 2

#随机产生一个2*2的张量

example = tf.random_normal([2,2])

#创建一个RandomShuffleQueue,参数意义参见API

q = tf.RandomShuffleQueue( capacity=1000, min_after_dequeue=0, dtypes=tf.float32, shapes=[2,2])

#enqueue op,每次push一个张量

enq_op = q.enqueue(example)

#dequeue op, 每次取出batch_size个张量

xs = q.dequeue_many(batch_size)

#创建QueueRunner,包含4个enqueue op线程

qr = tf.train.QueueRunner(q, [enq_op]*4)coord = tf.train.Coordinator()sess = tf.Session()

#启动QueueRuner,开始线程

enq_threads = qr.create_threads(sess, coord=coord, start=True)for i in range(10):

if coord.should_stop():

break print('step:', i, sess.run(xs))

#打印结果

coord.request_stop()coord.join(enq_threads)

总结

这两个类是实现TensorFlow pipeline的基础,能够高效地并行处理数据。个人认为在数据较大时,应该避免使用feed_dict。因为,feed_dict是利用python读取数据,python读取数据的时候,tensorflow无法计算,而且会将数据再次拷贝一份。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180305A155GD00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券