2018-08-13multi-threads

settings: pretreatment gonna slower the training proceedings


multithreads.png

queue

#create
tf.FIFOQueue(num_elements, dtype)
tf.RandomShuffleQueue()
#initialize
tf.enqueue_many([])
#out
tf.dequeue()
#in
tf.enqueue([])
tf.Coordinator and tf.QueueRunner
#enable the thread
tf.Coordinator()
# the work thread will do
def Loop(coord,thread_id)
# check and ask whether it should be stoped
     while not coord.should_stop():
         if ...
            coord.request_stop()
         else:
            print(thread_id)
         time.sleep(1)
coord=tf.train.Coordinator()
threads=[
threading.Thread(target=Loop,args=[coord,i,]))  for i in xrange(5)
for t in threads: t,start()
coord.join(threads)

manage the queue
queue= tf.FIFQueue(100,'float')
enqueue_op=queue.enqueue([tf.random_normal([1])])
#start 5 threads to execute the enqueue_op
qr=tf.train.QueueRunner(queue,[enqueue_op*5])
# add the QueueRunner to the specified collection on the graph, if no,then default
tf.train.add_queue_runner(qr)
out_tensor=queue.dequeue()
with tf.session() as sess:
       coord=tf.train.Coordinator()
       threads=tf.train.start_queue_runners(sess=sess,coord=coord)
       for _ in range(3): print sess.run(out_tensor) [0]
       coord.request_stop ()
       coord_join(threads)
input_file queue

tf.train.match_filenames_once()#get the files matched
tf.train.string_input_producer()#generate the input_file queue

files=tf.train.match_filenames_once('path_regex')
filename_queue=tf.train.string_input_producer(files,shuffle=False)
reader=tf.TFRecorder()
_,serialized_example=reader.read(filename_queue)
features=tf.parse_single_example(
serialized_example,
features={
'': tf.FixedLenFeature([],dtype)
...
}
with tf.session() as sess:
       tf.initialize_all_variables().run()
       print(sess.run(files))
       coord=tf.train.Coordinator()
       threads=tf.train.start_queue_runners(sess=sess,coord=coord)
       for i in range(6):
            print(sess.run([features['i'],features['j']]))
       coord.request_stop()
       coord.join(threads)
)
batching

tf.train.batch()
tf.train.shuffle_batch()

input_data processing framework
framework.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • MachineLP阅读 2,463评论 0 0
  • 这篇文章是针对有tensorflow基础但是记不住复杂变量函数的读者,文章列举了从输入变量到前向传播,反向优化,数...
    horsetif阅读 4,894评论 0 1
  • protocol buffer:data format(like json,xml) read note: whe...
    镜中无我阅读 1,589评论 0 0
  • 从事这个行业的第1000单成交。虽然我没有从事过纯粹的商业,但银行后期的营销工作仍给我不少的经验和心理上的锻炼,没...
    Bruceshaoshao阅读 2,934评论 0 0
  • 其一(黛玉) 姑苏病西子,冲龄失双亲。 清水一竖荷,偏往泥淖行。 本为绛珠草,还泪与神瑛。 可恨曹夫子,何必写此卿。
    长安旧人阅读 1,810评论 3 3

友情链接更多精彩内容