Time series prediction with LSTM using Keras: Wrong number of dimensions: expected 3, got 2 with shape
<p>I am trying to predict the next value in the time series using the previous 20 values. Here is a sample from my code:</p>

<p><code>X_train.shape</code> is <code>(15015, 20)</code></p>

<p><code>Y_train.shape</code> is <code>(15015,)</code></p>

<pre><code>EMB_SIZE = 1
HIDDEN_RNN = 3

model = Sequential()
model.add(LSTM(input_shape = (EMB_SIZE,), input_dim=EMB_SIZE, output_dim=HIDDEN_RNN, return_sequences=True))
model.add(LSTM(input_shape = (EMB_SIZE,), input_dim=EMB_SIZE, output_dim=HIDDEN_RNN, return_sequences=False))
model.add(Dense(1))
model.add(Activation('softmax'))
model.compile(optimizer='adam', 
              loss='binary_crossentropy', 
              metrics=['accuracy'])

model.fit(X_train, 
          Y_train, 
          nb_epoch=5, 
          batch_size = 128, 
          verbose=1, 
          validation_split=0.1)
score = model.evaluate(X_test, Y_test, batch_size=128)
print score
</code></pre>

<p>Though when I ran my code I got the following error:</p>

<p><code>TypeError: ('Bad input argument to theano function with name "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py:484"  at index 0(0-based)', 'Wrong number of dimensions: expected 3, got 2 with shape (32, 20).')</code></p>

<p>I was trying to replicate the results in this post: <a href="https://medium.com/@alexrachnog/neural-networks-for-algorithmic-trading-part-one-simple-time-series-forecasting-f992daa1045a#.muimc84k6" rel="nofollow">neural networks for algorithmic trading</a>. Here is a link to the git repo: <a href="https://github.com/Rachnog/Deep-Trading" rel="nofollow">link</a></p>

<p>It seems to be a conceptual error. Please post any sources where I can get a better understanding of LSTMS for time series prediction. Also please explain me how I fix this error, so that I can reproduce the results mentioned in the article mentioned above.</p>

<p>If I understand your problem correctly, your input data a set of 15015 1D sequences of length 20. According to Keras doc, the input is a 3D tensor with shape (nb_samples, timesteps, input_dim). In your case, the shape of <code>X</code> should then be <code>(15015, 20, 1)</code>.</p>

<p>Also, you just need to give <code>input_dim</code> to the first <code>LSTM</code> layer. <code>input_shape</code> is redundant and the second layer will infer its input shape automatically:</p>

<pre><code>model = Sequential()
model.add(LSTM(input_dim=EMB_SIZE, output_dim=HIDDEN_RNN, return_sequences=True))
model.add(LSTM(output_dim=HIDDEN_RNN, return_sequences=False))
</code></pre>

python machine-learning time-series keras