1
$\begingroup$

My dataset has 2944424 rows and 6 columns. I am using an LSTM in Keras to forecast taxi demand. I am having problem with the input_shape parameter of the LSTM.

It gives the error:

ValueError: Error when checking input: expected lstm_15_input to have 3 dimension
$\endgroup$
  • 1
    $\begingroup$ Add your model code so that people can understand the process. $\endgroup$ – Peter Jun 16 '19 at 14:11
0
$\begingroup$

Input should be of shape (batch size, number of steps, features). See the following for details:

https://machinelearningmastery.com/reshape-input-data-long-short-term-memory-networks-keras/

| improve this answer | |
$\endgroup$
0
$\begingroup$

Following example shows that by printing shape of your train data you can pass the right value to input_shape(). Also it depends you want to feed your data in 2D or another way. As it can be seen in snippet:

trainX.shape[1]=10

trainX.shape[2]=1440

and in output based on test shape Dense(units = 960)

#Shape of train and test data
trainX, testX, trainY, testY = train_test_split(trainX,trainY, test_size=0.2 , shuffle=False)
print("train size: {}".format(trainX.shape))
print("train Label size: {}".format(trainY.shape))
print("test size: {}".format(testX.shape))
print("test Label size: {}".format(testY.shape))
#train size: (23, 10, 1440)
#train Label size: (23, 960)
#test size: (6, 10, 1440)
#test Label size: (6, 960)

# create and fit the Simple LSTM model 

#model_LSTM = Sequential()
model_LSTM.add(LSTM(units = 1440, input_shape=(trainX.shape[1], trainX.shape[2])))
model_LSTM.add(Dense(units = 960))

Edit: Following tips based on this example to help you when preparing your input data for LSTMs:

  • The LSTM input layer must be 3D.
  • The meaning of the 3 input dimensions are: samples, time steps, and features.
  • The LSTM input layer is defined by the input_shape argument on the first hidden layer.
  • The input_shape argument takes a tuple of two values that define the number of time steps and features.
  • The number of samples is assumed to be 1 or more.
  • The reshape() function on NumPy arrays can be used to reshape your 1D or 2D data to be 3D.
  • The reshape() function takes a tuple as an argument that defines the new shape.
| improve this answer | |
$\endgroup$
  • $\begingroup$ Trying your solution it says IndexError: tuple index out of range. $\endgroup$ – Saeed Ahmad Jun 16 '19 at 17:09
  • $\begingroup$ I updated the answer but would you share your code so that we can have a look!? $\endgroup$ – Mario Jun 17 '19 at 16:11
  • $\begingroup$ model.add(LSTM(50, activation='relu', input_shape=(3, 1))) This is throwing error. I am not getting that how should I change that? $\endgroup$ – Saeed Ahmad Jun 18 '19 at 5:44
  • $\begingroup$ I guess that your input_shape is not right and you can't pass the right dimension. Plz update your question by leaving your complete code including dataset and data split and so on. $\endgroup$ – Mario Jun 18 '19 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.