1
votes

I have a error in Keras and I can't find the solution. I have searched the whole internet and I have still no answer ^^ Here is my Code.

model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=X.values.shape))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(10, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])

The error is the second line. It says "ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50)" The Shape of my Dataframe X is correct. And when I try to train the model the Error pops up

model.fit(X.values, Y.values, batch_size=200, epochs=10, validation_split=0.05)

I hope someone could help me :-)

[EDIT] Btw. here is the model.summary()


Layer (type) Output Shape Param #

lstm_1 (LSTM) (None, 393613, 32) 10624


lstm_2 (LSTM) (None, 393613, 32) 8320


lstm_3 (LSTM) (None, 32) 8320


dense_1 (Dense) (None, 10) 330

Total params: 27,594 Trainable params: 27,594 Non-trainable params: 0


Kind regards Niklas.

2
can you share a sample of X.Vikash Singh
try: X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))Vikash Singh
It dosen't work :( Error: ValueError: Must pass 2-d inputNiklas

2 Answers

1
votes

While initialising the first layer you are passing 2 values as input_shape =X.values.shape

keras already expects number of rows per batch as NONE. At runtime this value is determined by batch_size= (200 in your case)

So basically it internally changed shape of input for layer 1 as NO_OF_FEATURES, NO_OF_ROWS_IN_DATA_SET, NO_OF_ROWS_PER_BATCH

To fix this all you have to do is pass 1 parameter as input_shape, which is no of features. Keras already accepts NONE as a placeholder for no of rows per batch.

So input_shape=(X.values.shape[1],) should do the trick.

model.add(LSTM(32, return_sequences=True, input_shape=(X.values.shape[1],)))

0
votes

You have made two mistakes:

  1. LSTM processes sequential data. That is the input data to LSTM is 3 dimensional. In, your 1st lstm layer you set input_shape = X.values.shape which is wrong because input_shape you have to specify number of timesteps and number of features i.e. input_shape = (num_time_steps,num_features). Your rows of array are not your time steps. The rows basically indicate the batch number. As far as syntax is concerned, this is still right and you will not get any error while compiling the code.

  2. You have to reshape your data set so that it is 3 dimensional i.e. batch_size,time_step,feature. You can easily that using numpy.reshape as follows:

    X_train_arr = numpy.reshape(X.values,(X.values.shape[0],1,X.values.shape[1]))

    This is assuming you have only one time steps. if you have more time steps than you will need to do more tweaking.