Building a LSTM Cell using Keras
<p>I'm trying to build a RNN for text generation. I'm stuck at building my LSTM cell. The data is shaped like this- X is the input sparse matrix of dim(90809,2700) and Y is the output matrix of dimension(90809,27). The following is my code for defining the LSTM Cell-</p>

<pre><code>model = Sequential()
model.add(LSTM(128, input_shape=(X.shape[0], X.shape[1])))
model.add(Dropout(0.2))
model.add(Dense(Y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
</code></pre>

<p>My understanding is that the input_shape should be the dimension of the input matrix, and the dense layer should be the size of the output for each observation, i.e 27 in this case. However, I get the following error-</p>

<pre><code>Exception: Error when checking model input: expected lstm_input_3 to have 3 dimensions, but got array with shape (90809, 2700)
</code></pre>

<p>I'm not able to figure out what is going wrong. Can anyone please help me figure out why is the lstm_input expecting 3 dimensions?</p>

<p>I tried the following as well-</p>

<pre><code>X= np.reshape(np.asarray(dataX), (n_patterns, n_vocab*seq_length,1))
Y=np.reshape(np.asarray(dataY), (n_patterns, n_vocab,1))
</code></pre>

<p>This gave me the following error-</p>

<pre><code>Exception: Error when checking model input: expected lstm_input_7 to have shape (None, 90809, 2700) but got array with shape (90809, 2700, 1)
</code></pre>

<p>Any help will be appreciated. Thanks!</p>

<p>You should read about the difference between <code>input_shape</code>, <code>batch_input_shape</code> and <code>input_dim</code> <a href="https://keras.io/getting-started/sequential-model-guide/#specifying-the-input-shape" rel="nofollow">here</a>.</p>

<p>For <code>input_shape</code>, we don't need to define the <code>batch_size</code>. This is how your LSTM layer should look like.</p>

<pre><code>model.add(LSTM(128, input_shape=(X.shape[1], 1)))
</code></pre>

<p>or</p>

<pre><code>model.add(LSTM(128, batch_input_shape=(X.shape[0], X.shape[1], 1)))
</code></pre>

python keras recurrent-neural-network lstm