Search code examples
pythonkerastime-serieslstm

Understanding the input_shape of LSTM model


I divided my database in 40 rolling windows so I have a dataframe with shape (2000000, 132). Let's concentrate in the first window: it has 50k rows and the 132 columns so its shape is (50000, 132). I want to use all of this to predict the value of a label in the following window but I don't know what values are needed in the input_shape. I put input_shape=(X_train.shape[1], 1))) because I think in each iteration I have X_train.shape[1] = 132 columns and 1 timestep but I'm not sure. (I do each iteration with a for loop from 1 to 41)

The model is:

model = Sequential()
model.add(LSTM(units=50, activation='sigmoid', return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(units=50, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_24 (LSTM)              (None, 132, 50)           10400     
                                                                 
 lstm_25 (LSTM)              (None, 50)                20200     
                                                                 
 dense_11 (Dense)            (None, 1)                 51        
                                                                 
=================================================================
Total params: 30,651
Trainable params: 30,651
Non-trainable params: 0
_________________________________________________________________

Solution

  • In LSTM, the input_shape is (batch_size, timesteps, features).

    First, you need to convert X_train to a 3D tensor. Each 50000x132 matrix will be a sample. So, X_train will be (n_samples, 50000, 132).

    Now, you can set the input shape to (x_train.shape[1], x_train.shape[2]).