📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" Example to demonstrate how to use queues1Author: Chip Huyen2Prepared for the class CS 20SI: "TensorFlow for Deep Learning Research"3cs20si.stanford.edu4"""5import os6os.environ['TF_CPP_MIN_LOG_LEVEL']='2'78import numpy as np9import tensorflow as tf1011N_SAMPLES = 100012NUM_THREADS = 413# Generating some simple data14# create 1000 random samples, each is a 1D array from the normal distribution (10, 1)15data = 10 * np.random.randn(N_SAMPLES, 4) + 116# create 1000 random labels of 0 and 117target = np.random.randint(0, 2, size=N_SAMPLES)1819queue = tf.FIFOQueue(capacity=50, dtypes=[tf.float32, tf.int32], shapes=[[4], []])2021enqueue_op = queue.enqueue_many([data, target])22data_sample, label_sample = queue.dequeue()2324# create ops that do something with data_sample and label_sample2526# create NUM_THREADS to do enqueue27qr = tf.train.QueueRunner(queue, [enqueue_op] * NUM_THREADS)28with tf.Session() as sess:29# create a coordinator, launch the queue runner threads.30coord = tf.train.Coordinator()31enqueue_threads = qr.create_threads(sess, coord=coord, start=True)32try:33for step in range(100): # do to 100 iterations34if coord.should_stop():35break36data_batch, label_batch = sess.run([data_sample, label_sample])37print(data_batch)38print(label_batch)39except Exception as e:40coord.request_stop(e)41finally:42coord.request_stop()43coord.join(enqueue_threads)4445