Error on Keras SimpleRNN when input_shape is specified to be 3-d
<p>I'm doing trying to train from text on a SimpleRNN on Keras.</p>

<p>In Keras, i specified a very simple parameters for SimpleRNN as below:</p>

<pre><code>model = Sequential()
model.add(SimpleRNN(output_dim=1, input_shape=(1,1,1))
</code></pre>

<p>I understand that input_shape should be (nb_samples, timesteps, input_dim), the same as my train_x.shape</p>

<p>so i was surprised that i received the following error.</p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/xxx/xxxx/xxx/xxx.py", line 262, in &lt;module&gt;
    model.add(SimpleRNN(output_dim=vocab_size, input_shape=train_x.shape))
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\models.py", line 275, in add
    layer.create_input_layer(batch_input_shape, input_dtype)
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\engine\topology.py", line 367, in create_input_layer
    self(x)
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\engine\topology.py", line 467, in __call__
    self.assert_input_compatibility(x)
  File "C:\Anaconda3\envs\py34\lib\site-packages\keras\engine\topology.py", line 408, in assert_input_compatibility
    str(K.ndim(x)))
Exception: Input 0 is incompatible with layer simplernn_1: expected ndim=3, found ndim=4
</code></pre>

<p>Not sure why keras "found ndim=4" when only 3 was specified!</p>

<p>for clarity, my </p>

<blockquote>
  <p>train_x.shape = (73, 84, 400)</p>
</blockquote>

<p>and </p>

<blockquote>
  <p>vocab_size=400</p>
</blockquote>

<p>. As long as input_shape is fed 3d and above, i realised an error will result.</p>

<p>Any help will be greatly appreciated!!! :))</p>

<p>You are not supposed to include <code>n_samples</code> in the input shape of the model. So you have to specify a tuple of size 2 for input shape of a your layer (or set the first element of shape to <code>None</code>). Here Keras automatically adds <code>None</code> to your input shape resulting in <code>ndim=4</code>. 
More info on this can be found <a href="https://keras.io/getting-started/sequential-model-guide/#specifying-the-input-shape" rel="nofollow">here</a>. </p>

<p>Also it appears that your <code>input_dim=400</code> (assuming you use one-hot coding representation of words in vocabulary) and that your training data consists of <code>73</code> texts (pretty small) each having length of <code>84</code>. So you should probably set <code>input_shape=(84,400).</code></p>

python neural-network theano keras recurrent-neural-network