1
$\begingroup$

My input training and test dataset is the following size:

print(trainX.shape):(53394, 3)
print(testX.shape):(17799, 3)
print(trainY.shape):(53394,)
print(testY.shape):(17799,)

I reshaped it as follows:

trainX.shape:(1, 53394, 3)
testX.shape: (1, 17799, 3)
trainY.shape: (1, 53394)
testY.shape: (1, 17799)

Now, I pass it as the input layer of a LSTM:

model = Sequential()
model.add(LSTM(66, input_shape=(len(trainX),3)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100,batch_size=1, verbose=2)

I am getting the error:

Error Message: 
ValueError: Error when checking input: expected lstm_6_input to have shape (1, 3) but got array with shape (53394, 3)

Please help me to fit my data properly into a LSTM.

$\endgroup$
0
$\begingroup$

You should be structuring your data as a tuple: Number of samples, timesteps, features. In your case number of samples is 53394, timesteps is 1 and number of features is 3. So the input shape will be (53394, 1, 3). You can use this snippet of code for the tranformation:

trainX = trainX.reshape((trainX.shape[0], 1, 3))

And set the argument input_shape as below:

input_shape = (trainX.shape[1], trainX.shape[2])

Hope this helps!

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you..its working. I am not clear about the batch size? like in total dataset has more than 7000 entries. So should i set batch size as one? If i want to add more layer for the improvement.. how can i do that? Basiclly i am trying to predict y using x1,x2 and x3 $\endgroup$ – Hazel Jun 9 '18 at 16:41
  • $\begingroup$ Batch size is a hyper parameter. It is the number of samples processed (forward and backward pass) by the network in one iteration. After that network weights are updated. So a batch size of 1 means network weights are updated after one sample is processed. You should tune it after tuning other hyperparameters. You should not just set it to 1. $\endgroup$ – naive Jun 9 '18 at 16:56
  • $\begingroup$ You can add more layers but most of the problems are handled by one hidden layer. You need to set the argument return_sequences = True in the first hidden layer to add another hidden layer. Thanks for accepting the answer! $\endgroup$ – naive Jun 9 '18 at 17:15
  • $\begingroup$ I am able to fit the model. But i am not able to do predictions with it. Basically what i am doing is i am using values of X1,X2 and X3 nd trying to predict value of Y1. $\endgroup$ – Hazel Jun 10 '18 at 13:13
  • $\begingroup$ # make predictions trainPredict = model.predict(trainX) testPredict = model.predict(testX) # calculate root mean squared error trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0])) print('Test Score: %.2f RMSE' % (testScore)) $\endgroup$ – Hazel Jun 10 '18 at 13:14

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.