2

I have a test data of 1025643 entries and 72 features/attributes. I have trained an lstm with input data trainX with shape (245, 30, 72) and trainY with shape (245, ). Also note that I have specified look-back to be 30 steps back hence (245, 30, 72) shape of trainX.

Now after training the model if I do

model.output_shape

The output is:

(None, 1)

What I understand is that it will give one step prediction for the test set. But I want it to be 30 rows of predictions, one for each future step, for every row in test set (like output should have shape in my case to be (1025643 , 30, 1)). What changes do I need to do with data shapes? I am using keras lstm with tensorflow backend and python 3.6.

Code for my model is:

model = Sequential()
model.add(LSTM(100, return_sequences=True, input_shape = (trainX.shape[1], trainX.shape[2])))
model.add(LSTM(100, return_sequences = False))
model.add(Dense(1, activation='linear'))

model.compile(loss='mse', optimizer='adam')

model.fit(trainX, trainY, epochs=50, shuffle=False, verbose=1)
Fawad Khalil
  • 357
  • 3
  • 20

1 Answers1

1

The return_sequences=False parameter on the last LSTM layer causes the LSTM to only return the output after all 30 time steps. If you want 30 outputs (one after each time step) use return_sequences=True on the last LSTM layer, this will result in an output shape of (None, 30, 1).

For a more detailed explanation of LSTMs in Keras, see here.

Chris K
  • 1,703
  • 1
  • 14
  • 26
  • Thanks a lot :) Now will I have to make timesteps of 30 for each sample in test data to have it with shape (None, 30, 1), because the model asks me to do so? What if I wanted to provide only single row and the model gave me output of 30 rows (30 timesteps in sequence). like my test data still have shape (1025643, 72) and results i get have shape (1025643 , 30, 1). – Fawad Khalil Oct 17 '17 at 23:51
  • If I understand correctly, you want to provide an single time step of input and produce the next 30 time steps of output. The LSTM requires input for each time step. To produce 30 time steps of output from a single input you could duplicate the first input each time step, or use the output of the LSTM as the input for the next time step (the input dimension of your LSTM does not match the output, so the latter option is not possible for your architecture). – Chris K Oct 18 '17 at 22:31
  • Yeah, the idea of duplicating makes sense. Thank you again :) – Fawad Khalil Oct 18 '17 at 22:58