# How to set input for proper fit with lstm?

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.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)


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!

• 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 – Hazel Jun 9 '18 at 16:41
• 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. – naive Jun 9 '18 at 16:56
• 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! – naive Jun 9 '18 at 17:15
• 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. – Hazel Jun 10 '18 at 13:13
• # 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)) – Hazel Jun 10 '18 at 13:14