Python生成器混乱

时间:2017-08-01 19:41:33

标签: python tensorflow generator queueing

我想知道是否有一种方法可以从python生成器(特别是本例中的x_input_data的值)输入张量流队列的输入。

import tensorflow as tf

def generator():
    x = 0
    while True:
        x+=1
        yield x

def generate_inputs(g):
    x = []
    for a in range(6):
        x.append(g.next())
    return x

g = generator()

x_input_data = tf.cast(generate_inputs(g), tf.float32)
# x_input_data = tf.random_normal([6], mean=-1, stddev=4)  #<---previous implementation

q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)

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)

numberOfThreads = 1 
qr = tf.train.QueueRunner(q, [enqueue_op] * numberOfThreads)
tf.train.add_queue_runner(qr) 

input = q.dequeue() 
input = tf.Print(input, data=[q.size(), input], message="Nb elements left, input:")

# fake graph: START
y = input + 1
# fake graph: END 

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for steps in range(100):
        sess.run(y)

    coord.request_stop()
    coord.join(threads)

我希望打印出来:

[1 2 3 4 5 6]
[7 8 9 10 11 12]
etc...

0 个答案:

没有答案