slice_input_producer(q.dequeue())不起作用

时间:2017-02-08 09:08:08

标签: tensorflow

slice_input_producer()的参数是Tensor,所以我给了slice_input_producer()q.dequeue(),但是slice_input_producer会立即关闭队列(我不知道它是内部队列还是我的队列) q)。

我预计它会挂起,直到某些东西入队。

import tensorflow as tf

q = tf.FIFOQueue(capacity=1000, dtypes=tf.int32)
enq = q.enqueue(3)
deq = q.dequeue()
producer = tf.train.slice_input_producer([deq], shuffle=False)


sess = tf.Session()

sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

print(sess.run(enq))
print('enqueued')

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess, coord)

print(sess.run(producer))
print('got slice_input_producer')

coord.request_stop()
coord.join(threads)

sess.close()

但这有一个错误:

INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.InvalidArgumentError'>, slice index 0 of dimension 0 out of bounds.
     [[Node: input_producer/strided_slice = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](input_producer/Shape/_1, input_producer/strided_slice/stack, input_producer/strided_slice/stack_1, input_producer/strided_slice/stack_2)]]
     [[Node: input_producer/input_producer/range/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_13_input_producer/input_producer/range", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'input_producer/strided_slice', defined at:
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python3.5/dist-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/usr/local/lib/python3.5/dist-packages/tornado/ioloop.py", line 887, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 390, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py", line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-f15ef75111e3>", line 4, in <module>
    producer = tf.train.slice_input_producer([deq], shuffle=False)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/input.py", line 300, in slice_input_producer
    range_size = array_ops.shape(tensor_list[0])[0]
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 482, in _SliceHelper
    name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 636, in strided_slice
    shrink_axis_mask=shrink_axis_mask)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3503, in strided_slice
    shrink_axis_mask=shrink_axis_mask, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): slice index 0 of dimension 0 out of bounds.
     [[Node: input_producer/strided_slice = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](input_producer/Shape/_1, input_producer/strided_slice/stack, input_producer/strided_slice/stack_1, input_producer/strided_slice/stack_2)]]
     [[Node: input_producer/input_producer/range/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_13_input_producer/input_producer/range", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

更新

此代码使用动态张量重现input_producer的未定义行为。

import tensorflow as tf
import numpy as np
q = tf.FIFOQueue(capacity=1000, dtypes=tf.int32, shapes=[1])
enq = q.enqueue_many(np.array([[3], [4], [5], [6], [7], [8]]))
deq = q.dequeue_many(2)
producer = tf.train.slice_input_producer([deq], shuffle=False)


sess = tf.Session()

sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

sess.run(enq)
print('enqueued')

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess, coord)

print('reading slice_input_producer')
print(sess.run(producer))
print(sess.run(producer))

coord.request_stop()
coord.join(threads)

sess.close()

结果:

enqueued
reading slice_input_producer
[array([3])]
got slice_input_producer
[array([6])]
got slice_input_producer

1 个答案:

答案 0 :(得分:1)

TL; DR: tf.train.slice_input_producer(tensor_list) <button id="btn1" onClick="copyToClipboard(0)" >Btn 1</button> <button id="btn2" onClick="copyToClipboard(1)" >Btn 2</button> <button id="btn3" onClick="copyToClipboard(2)" >Btn 3</button>参数中的张量必须等于1(即矢量)或更高。

tensor_list函数将其输入张量沿维度0&amp; mdash分割为切片,并一次生成一个切片。例如,如果输入张量是一个矩阵,它会产生矩阵的行。

在您的示例中,tf.train.slice_input_producer()的输入是标量张量,没有维度0来沿其切片。此值来自队列的事实是无关紧要的(尽管由于队列中没有声明形状,因此会出现运行时错误而不是图形构造时错误)。以下定义是等效的,并且还会产生错误:

tf.train.slice_input_producer()

N.B。您可能会发现从队列中定义tf.train.slice_input_producer()会产生意外结果。特别是,每次使用切片时,当前实现都会在producer = tf.train.slice_input_producer([3], shuffle=False) 中评估张量。这适用于静态张量,但在你的程序中,它会从每个切片的队列中出一个新元素,这不太可能是你想要的。