标签:with 返回 构造 lock pre ase training parallel example
#首先有一个需要读取的文件名列表#然后将文件名列表通过函数string_input_producer放进文件名队列。#有时候因为数据量太大,需要把他们放进不同的tfrecord文件中filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])#对不同格式的文件有不同的readerreader = tf.TextLineReader()#通过reader的read函数extract a record from a file whose name is in the queue,#如果该文件中所有记录都被抽取完,dequeue这个filename,参考readerbase- #read()返回下一个record
key, value = reader.read(filename_queue)# decoded record,decode方式和文件内部record格式相关,然后拼接成需要的格式record_defaults = [[1], [1], [1], [1], [1]]col1, col2, col3, col4, col5 = tf.decode_csv(value, record_defaults=record_defaults)features = tf.stack([col1, col2, col3, col4])with tf.Session() as sess:# Start populating the filename queue.coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(coord=coord)for i in range(1200):# Retrieve a single instance:example, label = sess.run([features, col5])coord.request_stop()coord.join(threads)
Coordinator方法:should_stop,request_stop,join
# Thread body: loop until the coordinator indicates a stop was requested.# If some condition becomes true, ask the coordinator to stop.def MyLoop(coord):while not coord.should_stop():#should_stop返回true or false,表示线程是否该结束...do something...if ...some condition...:coord.request_stop()#当某些条件发生时,一个进程request_stop,其他进程因为should_stop返回true而终止
# Main thread: create a coordinator.coord = tf.train.Coordinator()
# Create 10 threads that run ‘MyLoop()‘threads = [threading.Thread(target=MyLoop, args=(coord,)) for i in xrange(10)]
# Start the threads and wait for all of them to stop.for t in threads:t.start()coord.join(threads)
example = ...ops to create one example...# Create a queue, and an op that enqueues examples one at a time in the queue.#区别于filename queue,这是example queue。可以是接着上面读数据解析然后放进这个queuequeue = tf.RandomShuffleQueue(...)enqueue_op = queue.enqueue(example)#定义入队操作# Create a training graph that starts by dequeuing a batch of examples.inputs = queue.dequeue_many(batch_size)train_op = ...use ‘inputs‘ to build the training part of the graph...# Create a queue runner that will run 4 threads in parallel to enqueue# examples.#QueueRunner的构造函数,queuerunner是为一个queue的入队操作多线程化服务的,#第二个参数是入队操作列表qr = tf.train.QueueRunner(queue, [enqueue_op] * 4)# Launch the graph.sess = tf.Session()# Create a coordinator, launch the queue runner threads.coord = tf.train.Coordinator()#queuerunner为queue创造多线程,并且把这些线程的结束交由coordinator管理enqueue_threads = qr.create_threads(sess, coord=coord, start=True)# Run the training loop, controlling termination with the coordinator.for step in xrange(1000000):if coord.should_stop():breaksess.run(train_op)# When done, ask the threads to stop.coord.request_stop()# And wait for them to actually do it.coord.join(enqueue_threads)
标签:with 返回 构造 lock pre ase training parallel example
原文地址:http://www.cnblogs.com/wyh1993/p/bcbc052b45df440275a94341c02e4cdc.html