在tf.while_loop中使用tf.concat会导致InvalidArgumentError:ConcatOp:所有输入张量的秩均应匹配。但是Ops单独工作正常

时间:2019-01-02 04:16:17

标签: python tensorflow

如果我分别运行tf.concat操作或tf.while_loop操作,它们执行得很好,但是如果我一起运行它们,则会收到错误消息。

这是再现错误的最少代码

@Valid
@ConvertGroup.List( {
    @ConvertGroup(from=New.class, to=Pk.class),
    @ConvertGroup(from=LocationGroup.class, to=Pk.class)
} )
// Other constraints
private BuildingDto building;

这是结果输出

import tensorflow as tf
import numpy as np

#Initialize matrix
testData = np.random.randint(0, 20, size=(4, 5 ))
print(testData)
#Convert matrix to tensorflow tensor
testData2 = tf.convert_to_tensor(testData, dtype=tf.int64)
step = tf.constant(1)

#Tensors that I want to concatinate in a while loop. Extracts values from testData2
yAll = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[0], 2], tf.int64)
yNew = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[0], 2], tf.int64)

#Define while loop condition 
def cond(step, yAll, yNew):
    return step < 4

#Define while loop body 
def body(step, yAll, yNew):
    p=7
    print('huh')
    yNew = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[step], 2], tf.int64)
    yAll = tf.concat( [[yAll], [yNew]], axis=0)
    return step + 1, yAll, yNew

#define while loop 
u = tf.while_loop(cond, body, loop_vars=[step, yAll, yNew], shape_invariants=[step.get_shape(), yAll.get_shape() , yNew.get_shape()])

#Print data
with tf.Session( ) as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run([ u] ))

这里只是在没有tf.while_loop的情况下运行concat操作,效果很好

[[ 1  6  2  7  7]
 [ 6  8  2 16  2]
 [13 18  5  6  8]
 [ 7 15  7  7 16]]
huh
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333     try:
-> 1334       return fn(*args)
   1335     except errors.OpError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1318       return self._call_tf_sessionrun(
-> 1319           options, feed_dict, fetch_list, target_list, run_metadata)
   1320 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1406         self._session, options, feed_dict, fetch_list, target_list,
-> 1407         run_metadata)
   1408 

InvalidArgumentError: ConcatOp : Ranks of all input tensors should match: shape[0] = [1,2,2] vs. shape[1] = [1,2]
     [[{{node while_1/concat}}]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-2-45e2bf149017> in <module>()
     25 with tf.Session( ) as sess:
     26     sess.run(tf.global_variables_initializer())
---> 27     print(sess.run([ u] ))

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    927     try:
    928       result = self._run(None, fetches, feed_dict, options_ptr,
--> 929                          run_metadata_ptr)
    930       if run_metadata:
    931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1150     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1151       results = self._do_run(handle, final_targets, final_fetches,
-> 1152                              feed_dict_tensor, options, run_metadata)
   1153     else:
   1154       results = []

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1326     if handle is None:
   1327       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328                            run_metadata)
   1329     else:
   1330       return self._do_call(_prun_fn, handle, feeds, fetches)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1346           pass
   1347       message = error_interpolation.interpolate(message, self._graph)
-> 1348       raise type(e)(node_def, op, message)
   1349 
   1350   def _extend_graph(self):

InvalidArgumentError: ConcatOp : Ranks of all input tensors should match: shape[0] = [1,2,2] vs. shape[1] = [1,2]
     [[node while_1/concat (defined at <ipython-input-2-45e2bf149017>:18) ]]

Errors may have originated from an input operation.
Input Source operations connected to node while_1/concat:
 Const_2 (defined at <ipython-input-2-45e2bf149017>:6)  
 strided_slice_2/stack (defined at <ipython-input-2-45e2bf149017>:9)    
 Const_3 (defined at <ipython-input-2-45e2bf149017>:7)

Original stack trace for 'while_1/concat':
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/lib/python3.6/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.6/dist-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-45e2bf149017>", line 21, in <module>
    u = tf.while_loop(cond, body, loop_vars=[step, yAll, yNew], shape_invariants=[step.get_shape(), yAll.get_shape() , yNew.get_shape()])
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 3560, in while_loop
    return_same_structure)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 3089, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 3024, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "<ipython-input-2-45e2bf149017>", line 18, in body
    yAll = tf.concat( [[yAll], [yNew]], axis=0)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py", line 1256, in concat
    return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1149, in concat_v2
    "ConcatV2", values=values, axis=axis, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 501, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
    self._traceback = tf_stack.extract_stack()

这是输出

testData = np.random.randint(0, 20, size=(4, 5 ))
print(testData)
testData2 = tf.convert_to_tensor(testData, dtype=tf.int64)

yAll = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[0], 2], tf.int64)
yNew = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[2], 2], tf.int64)
yAll = tf.concat( [[yAll], [yNew]], axis=0)

#Print data
with tf.Session( ) as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run([ yAll] ))

这里只是运行while循环,没有tf.while_loop,这很好用

[[ 6 13 10  7 17]
 [17 13  8  1  3]
 [ 6 18  0 12  0]
 [14 14  0 19 19]]
[array([[ 7, 13],
       [ 0,  0]])]

这是输出

testData = np.random.randint(0, 20, size=(4, 5 ))
print(testData)
testData2 = tf.convert_to_tensor(testData, dtype=tf.int64)
step = tf.constant(1)

yAll = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[0], 2], tf.int64)
yNew = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[0], 2], tf.int64)

def cond(step, yAll, yNew):
    return step < 4
def body(step, yAll, yNew):
    p=7
    print('huh')
    yNew = tf.py_func(lambda x, s: np.random.choice(x.reshape(-1),s, replace=False), [testData2[step], 2], tf.int64)
#     yAll = tf.concat( [[yAll], [yNew]], axis=0)
    return step + 1, yAll, yNew

u = tf.while_loop(cond, body, loop_vars=[step, yAll, yNew], shape_invariants=[step.get_shape(), yAll.get_shape() , yNew.get_shape()])

#Print data
with tf.Session( ) as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run([ u] ))

因此,以某种方式在tf while循环中组合tf concat操作会导致错误,也许是循环以某种方式转换了数据,但不确定它到底在做什么。

1 个答案:

答案 0 :(得分:0)

while循环的第一次迭代工作正常,因为yAll和yNew的形状相同

yAll = tf.concat( [[yAll], [yNew]], axis=0)

但是,在此之后,yAll会得到一个与yNew不同的新形状。因此,在第一步之后,应改为

yAll = tf.concat( [yAll, [yNew]], axis=0)

对于while循环,我通过使用

解决了此问题
yAll = tf.cond(step < 2, lambda: tf.concat( [[yAll], [yNew]], axis=0), lambda:tf.concat( [yAll, [yNew]], axis=0))