【问题标题】:error arise when using queue runner in tensorflow在张量流中使用队列运行器时出现错误
【发布时间】:2018-06-30 20:03:45
【问题描述】:

我是 tensorflow 的新手,我现在正在学习如何使用 queue runner。我想要做的是从目录中读取二进制文件并使每个文件成为一个数组。我使用两个线程并批量制作 4 个数组。代码如下。

  import glob

  import tensorflow as tf

  def readfile(filenames_queue):

        filename = filenames_queue.dequeue()
        value_strings = tf.read_file(filename)
        array = tf.decode_raw(value_strings,tf.uint8)
        return [array]
 def input_pipeline(filenames,batch_size,num_threads=2):

       filenames_queue = tf.train.string_input_producer(filenames)
       thread_lists = [readfile(filenames_queue) for _ in range(num_threads)] 
       min_after_dequeue = 1000 
       capacity = min_after_dequeue+3*batch_size
       arrays = tf.train.shuffle_batch_join(thread_lists,batch_size,capacity,min_after_dequeue)
       return arrays
if __name__ == "__main__":

      filenames = glob.glob('dir/*')
      arrays_batch = input_pipeline(filenames,4)
      with tf.Session() as sess:
           tf.global_variables_initializer().run()
           coord = tf.train.Coordinator()
           threads = tf.train.start_queue_runners(sess,coord)
           for i in range(100):
                 print sess.run(arrays_batch)
           coord.request_stop()
           coord.join(threads)

我已经修复了 Victor 和 Sorin 指出的错误,但是出现了一个新的错误:

文件“input_queue.py”,第 36 行,打印 sess.run(im_arrays_batch)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 889 行,运行中 run_metadata_ptr)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 1120 行,在 _run feed_dict_tensor、选项、run_metadata)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 1317 行,在 _do_run 选项,run_metadata)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,第 1336 行,在 _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_1_shuffle_batch_join/random_shuffle_queue' 已关闭且元素不足(请求 2,当前大小 0) [[节点:shuffle_batch_join = QueueDequeueManyV2[component_types=[DT_UINT8], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch_join/random_shuffle_queue, shuffle_batch_join/n )]]

由op u'shuffle_batch_join'引起,定义在:

文件“input_queue.py”,第 30 行,在 im_arrays_batch = input_pipeline(文件名,2)

文件“input_queue.py”,第 23 行,在 input_pipeline 中 arrays_batch = tf.train.shuffle_batch_join(thread_lists,batch_size,capacity,min_after_dequeue)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/input.py”,第 1367 行,在 shuffle_batch_join 名称=名称)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/input.py”,第 833 行,在 _shuffle_batch_join dequeued = queue.dequeue_many(batch_size, name=name)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py”,第 464 行,在 dequeue_many self._queue_ref, n=n, component_types=self._dtypes, name=name)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py”,第 2418 行,在 _queue_dequeue_many_v2 component_types=component_types, timeout_ms=timeout_ms, name=name)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py”,第 787 行,在 _apply_op_helper op_def=op_def)

文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py”,第 2956 行,在 create_op op_def=op_def)

init 中的文件“/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py”,第 1470 行 self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

OutOfRangeError(有关回溯,请参见上文):RandomShuffleQueue '_1_shuffle_batch_join/random_shuffle_queue' 已关闭且元素不足(请求的 2 个,当前大小为 0) [[节点:shuffle_batch_join = QueueDequeueManyV2[component_types=[DT_UINT8], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch_join/random_shuffle_queue, shuffle_batch_join/n )]]

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    您的 readfile(...): 函数应该返回一个可迭代对象,以便您可以返回特征和标签或其他类似的东西。

    所以要修复你的代码更改 readfile(...):

    return [arrays]
    

    【讨论】:

      【解决方案2】:

      来自tf.train.shuffle_batch_join

      tensors_list 参数是张量元组的列表

      在这里,您对tf.decode_raw produces Tensor instances 的呼叫,您将它们与thread_lists = [readfile(filenames_queue) for _ in range(num_threads)] 放在一个列表中。

      因此,它不是您提供的张量元组列表,而是张量列表,因此张量正在尝试迭代,因此出现错误 TypeError: 'Tensor' object is not iterable

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-13
        • 1970-01-01
        • 2016-06-27
        • 2017-07-15
        • 2018-08-23
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        相关资源
        最近更新 更多