使用 slice_input_producer 创建的队列为空 [英] Queue created with slice_input_producer is empty

查看:27
本文介绍了使用 slice_input_producer 创建的队列为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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 对打印它们,但程序没有打印任何东西,并抛出异常.

It seems to me that I should take 100 pairs and print them but the program doesn't print anything, and throws exception.

tensorflow.python.framework.errors_impl.OutOfRangeError: FIFOQueue '_0_input_producer/input_producer' is closed and has insufficient elements (requested 1, current size 0)

推荐答案

这是一个棘手的难题:事实证明你需要在你之前添加 sess.run(tf.local_variables_initializer())启动队列运行器:

This was a tricky puzzle: it turns out that you need to add sess.run(tf.local_variables_initializer()) before you start the queue runners:

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 变量一样,必须初始化此计数器.如果你不初始化它,队列运行器似乎会立即引发一个错误(遗憾的是没有打印它)并关闭队列......给出你看到的错误.

Why is this necessary? When you set num_epochs=20, TensorFlow implicitly creates a "local variable" that acts as a counter for the current epoch index; when this counter reaches 20, the queue will be closed. Like all other TensorFlow variables, this counter must be initialized. If you don't initialize it, it appears that queue runners will raise an error immediately (without printing it, sadly) and close the queue... giving the error that you were seeing.

这篇关于使用 slice_input_producer 创建的队列为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆