我有以下代码:
import tensorflow as tf
xs = tf.random_normal([5, 2])
ys = xs[:, 0] + xs[:, 1] + tf.random_normal([5], stddev=0.01)
xs_inp, ys_inp = tf.train.slice_input_producer([xs, ys], num_epochs=20)
coord = tf.train.Coordinator()
with tf.Session() as sess:
threads = tf.train.start_queue_runners(coord=coord)
for i in range(100):
print(sess.run([xs_inp, ys_inp]))
coord.request_stop()
coord.join(threads)
在我看来,我应该拿100对并打印它们,但程序没有打印任何东西,并抛出异常。
tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue '_0_input_producer/input_producer' is closed and has insufficient elements (requested 1, current size 0)
答案 0 :(得分:4)
这是一个棘手的难题:事实证明,在启动队列运行程序之前需要添加sess.run(tf.local_variables_initializer())
:
import tensorflow as tf
xs = tf.random_normal([5, 2])
ys = xs[:, 0] + xs[:, 1] + tf.random_normal([5], stddev=0.01)
xs_inp, ys_inp = tf.train.slice_input_producer((xs, ys), num_epochs=20)
coord = tf.train.Coordinator()
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
threads = tf.train.start_queue_runners(coord=coord)
for i in range(100):
print(sess.run([xs_inp, ys_inp]))
coord.request_stop()
coord.join(threads)
为什么这有必要?当您设置num_epochs=20
时,TensorFlow会隐式创建一个"局部变量"作为当前时代指数的反制者;当此计数器达到20时,队列将被关闭。与所有其他TensorFlow变量一样,必须初始化此计数器。如果你没有初始化它,看起来队列运行器会立即引发错误(不用打印它,遗憾地)并关闭队列......给出你看到的错误。