Last active
September 28, 2017 15:18
-
-
Save felipessalvatore/f5c295086504f981b53565de9b75dcf3 to your computer and use it in GitHub Desktop.
Exploring queue tensorflow 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
NUM_THREADS = 4 | |
data = np.array([[0.2, 0.3], [0.4, 0.9], [0.8, -1.2], [-1.3, -0.2]]) | |
target = np.array([1, 0, 1, 0]) | |
queue = tf.FIFOQueue(capacity=3, | |
dtypes=[tf.float32, tf.int32], | |
shapes=[[2], []]) | |
enqueue_op = queue.enqueue_many([data, target]) | |
data_sample, label_sample = queue.dequeue() | |
qr = tf.train.QueueRunner(queue, [enqueue_op] * NUM_THREADS) | |
with tf.Session() as sess: | |
coord = tf.train.Coordinator() | |
enqueue_threads = qr.create_threads(sess, coord=coord, start=True) | |
try: | |
for step in range(8): | |
if coord.should_stop(): | |
break | |
data_batch, label_batch = sess.run([data_sample, label_sample]) | |
print("step = {}, x ={}, y={}".format(step, data_batch, label_batch)) | |
except Exception as e: | |
print("Exception") | |
coord.request_stop() | |
finally: | |
coord.request_stop() | |
coord.join(enqueue_threads) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment