-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaby_queue.py
45 lines (37 loc) · 1.85 KB
/
baby_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tensorflow as tf
# This time, let's start with 6 samples of 1 data point
# x_input_data = tf.random_normal([6], mean=-1, stddev=4)
x_input_data = [[1, 2, 3, 4, 5, 6]]
# Note that the FIFO queue has still a capacity of 3
q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)
# To check what is happening in this case:
# we will print a message each time "x_input_data" is actually computed
# to be used in the "enqueue_many" operation
# x_input_data = tf.Print(x_input_data, data=[x_input_data], message="Raw inputs data generated:", summarize=6)
enqueue_op = q.enqueue_many(x_input_data)
# To leverage multi-threading we create a "QueueRunner"
# that will handle the "enqueue_op" outside of the main thread
# We don't need much parallelism here, so we will use only 1 thread
numberOfThreads = 1
qr = tf.train.QueueRunner(q, [enqueue_op] * numberOfThreads)
# Don't forget to add your "QueueRunner" to the QUEUE_RUNNERS collection
tf.train.add_queue_runner(qr)
input = q.dequeue()
# We start the session as usual ...
with tf.Session() as sess:
# But now we build our coordinator to coordinate our child threads with
# the main thread
coord = tf.train.Coordinator()
# Beware, if you don't start all your queues before runnig anything
# The main threads will wait for them to start and you will hang again
# This helper start all queues in tf.GraphKeys.QUEUE_RUNNERS
threads = tf.train.start_queue_runners(coord=coord)
# The QueueRunner will automatically call the enqueue operation
# asynchronously in its own thread ensuring that the queue is always full
# No more hanging for the main process, no more waiting for the GPU
for i in range(30):
print(sess.run(input))
# We request our child threads to stop ...
coord.request_stop()
# ... and we wait for them to do so before releasing the main thread
coord.join(threads)