0

I have a question concerning time series data. My training dataset has the dimension (3183, 1, 6)

My model:

model = Sequential()
model.add(LSTM(100, input_shape = (training_input_data.shape[1], training_input_data.shape[2])))
model.add(Dropout(0.2))
model.add(LSTM(100, input_shape = (training_input_data.shape[1], training_input_data.shape[2])))
model.add(Dense(1))
model.compile(optimizer = 'adam', loss='mse')

I get the following error at the second LSTM layer:

ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2 But there is no ndim parameter.

bharatk
  • 4,202
  • 5
  • 16
  • 30
Stefan Fricke
  • 11
  • 1
  • 4

2 Answers2

0

The problem is that the first LSTM layer is returning something with shape (batch_size, 100). If you want to iterate with a 2nd LSTM layer, you should probably add the option return_sequences=True in the first LSTM layer (which would then return an object of shape (batch_size, training_input_data.shape[1], 100).

Note that passing input_shape = (..) in the 2nd LSTM is not mandatory, as the input shape of this layer is autoamtically computed based on the output shape of the first one.

Raphael Meudec
  • 686
  • 5
  • 10
0

You need to set parameter return_sequences=True to stack LSTM layers.

model = Sequential()
model.add(LSTM(
    100,
    input_shape = (training_input_data.shape[1], training_input_data.shape[2]),
    return_sequences=True
))
model.add(Dropout(0.2))
model.add(LSTM(100, input_shape = (training_input_data.shape[1], training_input_data.shape[2])))
model.add(Dense(1))
model.compile(optimizer = 'adam', loss='mse')

See also How to stack multiple lstm in keras?