使用 slice

编程入门 行业动态 更新时间:2024-10-09 11:27:55
本文介绍了使用 slice_input_producer 创建的队列为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有以下代码:

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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 01:14:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:slice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!