title: How to fix 'ValueError: Input 0 is incompatible with layer simple_rnn_1: expected shape=(None, None, 20), found shape=(None, None, 2, 20)'
body: <p>I have a few matrices that pass through multiple layers, the last one is Dense layer to produce a vector for each matrix. Now I wish to feed these matrices to an RNN of keras and that is where I face this error.</p>
  
  <p>I tried stacking the vectors together in order to pass them to the RNN. Here's a piece of code for that idea:</p>
  
  <pre><code>input1 = Dense(20, activation = "relu")(input1)
  input2 = Dense(20, activation = "relu")(input2)
  out = Lambda(lambda x: tf.stack([x[0], x[1]], axis=1), output_shape=(None, 2, 20))([input1, input2])
  out = SimpleRNN(50, activation="relu")(out)
  </code></pre>
  
  <p>And I receive:</p>
  
  <pre><code>&gt;Traceback (most recent call last):
    &gt;&gt;File "model.py", line 106, in &lt;module&gt;
      model = make_model()
  
    &gt;&gt;File "model.py", line 60, in make_model
      out = SimpleRNN(50, activation="relu")(out) 
  
    &gt;&gt;File "/home/yamini/.local/lib/python3.6/site-packages/keras/layers/recurrent.py", line 532, in __call__
      return super(RNN, self).__call__(inputs, **kwargs)
  
    &gt;&gt;File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 440, in __call__
      self.assert_input_compatibility(inputs)
  
    &gt;&gt;File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 368, in assert_input_compatibility
      str(x_shape))
  
  &gt;&gt;ValueError: Input 0 is incompatible with layer simple_rnn_1: expected shape=(None, None, 20), found shape=(None, None, 2, 20)
  </code></pre>
  
  <p>And If I change the <code>output_shape=(None, None, 20)</code> in Lambda layer, I get:</p>
  
  <pre><code>Traceback (most recent call last):
   &gt;&gt; File "model.py", line 107, in &lt;module&gt;
      model.fit([input1, input2], y_train, epochs = 15, batch_size = 20, verbose = 2)
  
    &gt;&gt;File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit
      batch_size=batch_size)
  
    &gt;&gt;File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
      exception_prefix='target')
  
    &gt;&gt;File "/home/yamini/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
      str(data_shape))
  
  &gt;&gt;ValueError: Error when checking target: expected simple_rnn_1 to have shape (50,) but got array with shape (1,)
  </code></pre>