I'm new to ML and learning LSTM with this tutorial https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/. Scroll down to part "LSTM Network for Regression", the model is defined as this code:

# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

My questions are:

  • The input_shape is wrong, isn't it? According to my understanding, the number 1 is the time step, the look_back must be the number of feature?
  • If time step is 1, why this model still works (I tried to model myself, the result is decent)? As my understanding, LSTM must look back some time steps to predict the next number, if look_back == 1 then this LSTM is just a vanilla neuron, how does it still work?

1 Answer 1


You are correct, the example doesn't make much sense. They even make it explicit in the tutorial

We can phrase the problem as a regression problem.

That is, given the number of passengers (in units of thousands) this month, what is the number of passengers next month?

But to learn from one point, you don't need LSTM! LSTM won't be able to use its recurrent nature if there are no previous values. In such a case, it does not much more than can be achieved with Dense layers, if not just simple linear regression.

In fact, if you look at RMSE when using a very naive forecasting method: take the value from the previous timestep as a prediction, then the result is nearly the same as compared to the "LSTM" in the tutorial:

trainPredict = scaler.inverse_transform([trainX.flatten()])
trainY = scaler.inverse_transform([trainY.flatten()])
testPredict = scaler.inverse_transform([testX.flatten()])
testY = scaler.inverse_transform([testY.flatten()])

trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[0]))
print('Test Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[0]))
print('Test Score: %.2f RMSE' % (testScore))

## Test Score: 23.18 RMSE
## Test Score: 48.66 RMSE

where the "LSTM" in the tutorial achieved:

## Train Score: 22.93 RMSE
## Test Score: 47.53 RMSE

The only valid way to use LSTM is when you have multiple timepoints per each sample or use stateful=True where the LSTM passes the state between the batches.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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