Skip to content

Instantly share code, notes, and snippets.

@felipessalvatore
Last active September 28, 2017 15:18
Show Gist options
  • Save felipessalvatore/f5c295086504f981b53565de9b75dcf3 to your computer and use it in GitHub Desktop.
Save felipessalvatore/f5c295086504f981b53565de9b75dcf3 to your computer and use it in GitHub Desktop.
Exploring queue tensorflow 1
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