title: InvalidArgumentError : ConcatOp : Dimensions of inputs should match
body: <p>Tensorflow 1.7 when using dynamic_rnn.It runs fine at first , but at the 32th(it changes when i run the code) step , the error appears. When i used smaller batch , it seems the code can run longer , however the error still poped up .Just cannt figure out what's wrong.</p>
  <pre><code>    from mapping import *
  
  
  def my_input_fn(features, targets, batch_size=20, shuffle=True, num_epochs=None, sequece_lenth=None):
      ds = tf.data.Dataset.from_tensor_slices(
          (features, targets, sequece_lenth))  # warning: 2GB limit
      ds = ds.batch(batch_size).repeat(num_epochs)
  
      if shuffle:
          ds = ds.shuffle(10000)
      features, labels, sequence = ds.make_one_shot_iterator().get_next()
      return features, labels, sequence
  
  
  def lstm_cell(lstm_size=50):
      return tf.contrib.rnn.BasicLSTMCell(lstm_size)
  
  
  class RnnModel:
      def __init__(self,
                   batch_size,
                   hidden_units,
                   time_steps,
                   num_features
                   ):
          self.batch_size = batch_size
          self.hidden_units = hidden_units
          stacked_lstm = tf.contrib.rnn.MultiRNNCell(
              [lstm_cell(i) for i in self.hidden_units])
          self.initial_state = stacked_lstm.zero_state(batch_size, tf.float32)
          self.model = stacked_lstm
          self.state = self.initial_state
          self.time_steps = time_steps
          self.num_features = num_features
  
      def loss_mean_squre(self, outputs, targets):
          pos = tf.add(outputs, tf.ones(self.batch_size))
          eve = tf.div(pos, 2)
          error = tf.subtract(eve,
                              targets)
          return tf.reduce_mean(tf.square(error))
  
      def train(self,
                num_steps,
                learningRate,
                input_fn,
                inputs,
                targets,
                sequenceLenth):
  
          periods = 10
          step_per_periods = int(num_steps / periods)
  
          input, target, sequence = input_fn(inputs, targets, self.batch_size, shuffle=True, sequece_lenth=sequenceLenth)
  
          initial_state = self.model.zero_state(self.batch_size, tf.float32)
  
          outputs, state = tf.nn.dynamic_rnn(self.model, input, initial_state=initial_state)  
  
          loss = self.loss_mean_squre(tf.reshape(outputs, [self.time_steps, self.batch_size])[-1], target)
          optimizer = tf.train.AdamOptimizer(learning_rate=learningRate)
          grads_and_vars = optimizer.compute_gradients(loss, self.model.variables)
          optimizer.apply_gradients(grads_and_vars)
  
          init_op = tf.global_variables_initializer()
          with tf.Session() as sess:
  
              for i in range(num_steps):
                  sess.run(init_op)
                  state2, current_loss= sess.run([state, loss])
                  if i % step_per_periods == 0:
                      print(&quot;period &quot; + str(int(i / step_per_periods)) + &quot;:&quot; + str(current_loss))
          return self.model, self.state
  
  
  def processFeature(df):
      df = df.drop('class', 1)
      features = []
  
      for i in range(len(df[&quot;vecs&quot;])):
          features.append(df[&quot;vecs&quot;][i])
  
      aa = pd.Series(features).tolist()  # tramsform into list
      featuresList = []
      for i in features:
          p1 = []
          for k in i:
              p1.append(list(k))
          featuresList.append(p1)
  
      return featuresList
  
  
  def processTargets(df):
      selected_features = df[
          &quot;class&quot;]
      processed_features = selected_features.copy()
      return tf.convert_to_tensor(processed_features.astype(float).tolist())
  
  
  if __name__ == '__main__':
      dividNumber = 30
      &quot;&quot;&quot;
      some code here to modify my data to input 
      
      it looks like this:
      inputs before use input function : [fullLenth, charactorLenth, embeddinglenth]
      &quot;&quot;&quot;
  
      model = RnnModel(15, [100, 80, 80, 1], time_steps=dividNumber, num_features=25)
      model.train(5000, 0.0001, my_input_fn, training_examples, training_targets, sequenceLenth=trainSequenceL)
  </code></pre>
  <p><strong>And error is under here</strong></p>
  <pre><code>Traceback (most recent call last):
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1330, in _do_call
          return fn(*args)
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1315, in _run_fn
          options, feed_dict, fetch_list, target_list, run_metadata)
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1423, in _call_tf_sessionrun
          status, run_metadata)
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\framework\errors_impl.py&quot;, line 516, in __exit__
          c_api.TF_GetCode(self.status.status))
      tensorflow.python.framework.errors_impl.InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [20,25] vs. shape[1] = [30,100]
           [[Node: rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device=&quot;/job:localhost/replica:0/task:0/device:CPU:0&quot;](rnn/while/TensorArrayReadV3, rnn/while/Switch_4:1, rnn/while/rnn/multi_rnn_cell/cell_3/basic_lstm_cell/Const)]]
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File &quot;D:/programming/mlwords/dnn_gragh.py&quot;, line 198, in &lt;module&gt;
          model.train(5000, 0.0001, my_input_fn, training_examples, training_targets, sequenceLenth=trainSequenceL)
        File &quot;D:/programming/mlwords/dnn_gragh.py&quot;, line 124, in train
          state2, current_loss, nowAccuracy = sess.run([state, loss, accuracy])
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 908, in run
          run_metadata_ptr)
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1143, in _run
          feed_dict_tensor, options, run_metadata)
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1324, in _do_run
          run_metadata)
        File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1343, in _do_call
          raise type(e)(node_def, op, message)
      tensorflow.python.framework.errors_impl.InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [20,25] vs. shape[1] = [30,100]
           [[Node: rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device=&quot;/job:localhost/replica:0/task:0/device:CPU:0&quot;](rnn/while/TensorArrayReadV3, rnn/while/Switch_4:1, rnn/while/rnn/multi_rnn_cell/cell_3/basic_lstm_cell/Const)]]
  
  Caused by op 'rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/concat', defined at:
    File &quot;D:/programming/mlwords/dnn_gragh.py&quot;, line 198, in &lt;module&gt;
      model.train(5000, 0.0001, my_input_fn, training_examples, training_targets, sequenceLenth=trainSequenceL)
    File &quot;D:/programming/mlwords/dnn_gragh.py&quot;, line 95, in train
      outputs, state = tf.nn.dynamic_rnn(self.model, input, initial_state=initial_state)#,sequence_length=sequence
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn.py&quot;, line 627, in dynamic_rnn
      dtype=dtype)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn.py&quot;, line 824, in _dynamic_rnn_loop
      swap_memory=swap_memory)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 3205, in while_loop
      result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 2943, in BuildLoop
      pred, body, original_loop_vars, loop_vars, shape_invariants)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 2880, in _BuildLoop
      body_result = body(*packed_vars_for_body)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 3181, in &lt;lambda&gt;
      body = lambda i, lv: (i + 1, orig_body(*lv))
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn.py&quot;, line 795, in _time_step
      (output, new_state) = call_cell()
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn.py&quot;, line 781, in &lt;lambda&gt;
      call_cell = lambda: cell(input_t, state)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py&quot;, line 232, in __call__
      return super(RNNCell, self).__call__(inputs, state)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\layers\base.py&quot;, line 714, in __call__
      outputs = self.call(inputs, *args, **kwargs)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py&quot;, line 1283, in call
      cur_inp, new_state = cell(cur_inp, cur_state)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py&quot;, line 339, in __call__
      *args, **kwargs)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\layers\base.py&quot;, line 714, in __call__
      outputs = self.call(inputs, *args, **kwargs)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py&quot;, line 620, in call
      array_ops.concat([inputs, h], 1), self._kernel)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\array_ops.py&quot;, line 1181, in concat
      return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\ops\gen_array_ops.py&quot;, line 1101, in concat_v2
      &quot;ConcatV2&quot;, values=values, axis=axis, name=name)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\framework\op_def_library.py&quot;, line 787, in _apply_op_helper
      op_def=op_def)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\framework\ops.py&quot;, line 3309, in create_op
      op_def=op_def)
    File &quot;D:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\framework\ops.py&quot;, line 1669, in __init__
      self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
  
  InvalidArgumentError (see above for traceback): ConcatOp : Dimensions of inputs should match: shape[0] = [20,25] vs. shape[1] = [30,100]
       [[Node: rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device=&quot;/job:localhost/replica:0/task:0/device:CPU:0&quot;](rnn/while/TensorArrayReadV3, rnn/while/Switch_4:1, rnn/while/rnn/multi_rnn_cell/cell_3/basic_lstm_cell/Const)]]
  </code></pre>
  <p>this is my code used to check my input</p>
  <pre><code>def checkData(inputs, targets, sequencelence):
      batch_size = 20
      features, target, sequece = my_input_fn(inputs, targets, batch_size=batch_size, shuffle=True, num_epochs=None,
                                              sequece_lenth=sequencelence)
      with tf.Session() as sess:
          for i in range(1000):
              features1, target1, sequece1 = sess.run([features, target, sequece])
              assert len(features1) == batch_size
              for sentence in features1 :
                  assert len(sentence) == 30
                  for word in sentence:
                      assert len(word) == 25
  
              assert len(target1) == batch_size
              assert len(sequece1) == batch_size
              print(target1)
      print(&quot;OK&quot;)
  </code></pre>