Using tf.concat in tf.while_loop results in `InvalidArgumentError: ConcatOp : Ranks of all input tensors...












0















If I run my tf.concat operation or tf.while_loop operation separately, they execute fine, but if I run them together, I get an error.



Here is the minimal code to reproduce the error



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] ))


Here is the resulting output



[[ 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()


Here is just running the concat operation without the tf.while_loop, which works fine



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] ))


Here is the output



[[ 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]])]


And here is just running the whileloop, without tf.while_loop, which works fine



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] ))


Here is the output



[[13 11  6 12 11]
[18 7 4 4 17]
[ 1 4 17 17 13]
[ 1 9 10 10 18]]
huh
[(4, array([11, 6]), array([18, 10]))]


So somehow combining the tf concat operation in a tf while loop causes the error, perhaps the loop is transforming the data somehow, but not sure what exactly it is doing.










share|improve this question



























    0















    If I run my tf.concat operation or tf.while_loop operation separately, they execute fine, but if I run them together, I get an error.



    Here is the minimal code to reproduce the error



    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] ))


    Here is the resulting output



    [[ 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()


    Here is just running the concat operation without the tf.while_loop, which works fine



    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] ))


    Here is the output



    [[ 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]])]


    And here is just running the whileloop, without tf.while_loop, which works fine



    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] ))


    Here is the output



    [[13 11  6 12 11]
    [18 7 4 4 17]
    [ 1 4 17 17 13]
    [ 1 9 10 10 18]]
    huh
    [(4, array([11, 6]), array([18, 10]))]


    So somehow combining the tf concat operation in a tf while loop causes the error, perhaps the loop is transforming the data somehow, but not sure what exactly it is doing.










    share|improve this question

























      0












      0








      0








      If I run my tf.concat operation or tf.while_loop operation separately, they execute fine, but if I run them together, I get an error.



      Here is the minimal code to reproduce the error



      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] ))


      Here is the resulting output



      [[ 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()


      Here is just running the concat operation without the tf.while_loop, which works fine



      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] ))


      Here is the output



      [[ 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]])]


      And here is just running the whileloop, without tf.while_loop, which works fine



      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] ))


      Here is the output



      [[13 11  6 12 11]
      [18 7 4 4 17]
      [ 1 4 17 17 13]
      [ 1 9 10 10 18]]
      huh
      [(4, array([11, 6]), array([18, 10]))]


      So somehow combining the tf concat operation in a tf while loop causes the error, perhaps the loop is transforming the data somehow, but not sure what exactly it is doing.










      share|improve this question














      If I run my tf.concat operation or tf.while_loop operation separately, they execute fine, but if I run them together, I get an error.



      Here is the minimal code to reproduce the error



      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] ))


      Here is the resulting output



      [[ 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()


      Here is just running the concat operation without the tf.while_loop, which works fine



      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] ))


      Here is the output



      [[ 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]])]


      And here is just running the whileloop, without tf.while_loop, which works fine



      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] ))


      Here is the output



      [[13 11  6 12 11]
      [18 7 4 4 17]
      [ 1 4 17 17 13]
      [ 1 9 10 10 18]]
      huh
      [(4, array([11, 6]), array([18, 10]))]


      So somehow combining the tf concat operation in a tf while loop causes the error, perhaps the loop is transforming the data somehow, but not sure what exactly it is doing.







      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 4:16









      SantoshGupta7SantoshGupta7

      6811515




      6811515
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The first iteration of the while loop works fine because yAll and yNew are the same shape



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


          However, after this, yAll gets a new shape that's not the same as yNew. So after the first step this should be done instead



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


          For the while loop, i solved this issue by using



          yAll = tf.cond(step < 2, lambda: tf.concat( [[yAll], [yNew]], axis=0), lambda:tf.concat( [yAll, [yNew]], axis=0))





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001128%2fusing-tf-concat-in-tf-while-loop-results-in-invalidargumenterror-concatop-ra%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The first iteration of the while loop works fine because yAll and yNew are the same shape



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


            However, after this, yAll gets a new shape that's not the same as yNew. So after the first step this should be done instead



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


            For the while loop, i solved this issue by using



            yAll = tf.cond(step < 2, lambda: tf.concat( [[yAll], [yNew]], axis=0), lambda:tf.concat( [yAll, [yNew]], axis=0))





            share|improve this answer




























              0














              The first iteration of the while loop works fine because yAll and yNew are the same shape



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


              However, after this, yAll gets a new shape that's not the same as yNew. So after the first step this should be done instead



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


              For the while loop, i solved this issue by using



              yAll = tf.cond(step < 2, lambda: tf.concat( [[yAll], [yNew]], axis=0), lambda:tf.concat( [yAll, [yNew]], axis=0))





              share|improve this answer


























                0












                0








                0







                The first iteration of the while loop works fine because yAll and yNew are the same shape



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


                However, after this, yAll gets a new shape that's not the same as yNew. So after the first step this should be done instead



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


                For the while loop, i solved this issue by using



                yAll = tf.cond(step < 2, lambda: tf.concat( [[yAll], [yNew]], axis=0), lambda:tf.concat( [yAll, [yNew]], axis=0))





                share|improve this answer













                The first iteration of the while loop works fine because yAll and yNew are the same shape



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


                However, after this, yAll gets a new shape that's not the same as yNew. So after the first step this should be done instead



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


                For the while loop, i solved this issue by using



                yAll = tf.cond(step < 2, lambda: tf.concat( [[yAll], [yNew]], axis=0), lambda:tf.concat( [yAll, [yNew]], axis=0))






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 4:55









                SantoshGupta7SantoshGupta7

                6811515




                6811515
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001128%2fusing-tf-concat-in-tf-while-loop-results-in-invalidargumenterror-concatop-ra%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas