a1letterword
a1letterword

Reputation: 307

Input Dimensionality for an LSTM in Keras

So I'm pretty sure I'm inputting the dimensions correctly. I think the error lies in the reshape of the input, but not really sure.

Here's what I'm working with:

df_matrix = df_model.as_matrix()
df_matrix = np.reshape(df_matrix,(-1,588425,26))
df_matrix.shape
y_matrix = y.as_matrix()
y_matrix = np.reshape(y_matrix,(-1,588425,1))
df_matrix2 = df_model.as_matrix()

model.add(LSTM(32, input_shape=(588425, 26), return_sequences = True))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(df_matrix2, y, epochs=2, batch_size=1, verbose=2)

Which is popping out this error: ValueError: Input 0 is incompatible with layer lstm_17: expected ndim=3, found ndim=2

The output for df_matrix2.shape is (588425, 26). I also tried df_matrix which I reshaped into a 3D array and the output for df_matrix is (1, 588425, 26). Both failed, so I'm unsure what the problem in the input space is? Since both a 2-d and 3-d input gave me the same error.

Upvotes: 1

Views: 334

Answers (1)

Vadim
Vadim

Reputation: 4537

the answer for your question is already in your question:

Which is popping out this error: ValueError: Input 0 is incompatible with layer lstm_17: expected ndim=3, found ndim=2

So, what should you do?

You have an input list which has shape like this:

(N,N)

But, for LSTMs you need shape:

(N,N,N)

The simples solution would be to make something like this:

y_matrix = np.reshape(y_matrix,(588425,1,1))

Also, don't forget to change the number in your NN:

model.add(LSTM(32, input_shape=(None, 1), return_sequences = True))

Upvotes: 1

Related Questions