2

When I try to add a second LSTM layer to my model I get the following error message: "Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2".

I also get the same message when return_sequences is set to True. I would like it to be True, but it only works when set to False. I have tried Keras Reshape with no luck.

Any help would be greatly appreciated, thanks!

# X is a 3D array with roughly 1000 2D arrays of size (n_steps, n_features)
X = X.reshape((X.shape[0], X.shape[1], n_features))

# setup model
model = Sequential()
lstmSize = 32
dropout = 0.2
denseSize = 1
returnSequences = False # doesn't work when set to True

model.add(LSTM(lstmSize, activation='relu', return_sequences=returnSequences, input_shape=(n_steps, n_features)))
model.add(Dropout(dropout))
model.add(LSTM(lstmSize, activation='relu', return_sequences=returnSequences, input_shape=(n_steps, n_features))) # error here
model.add(Dropout(dropout))
model.add(Dense(denseSize, activation='softmax'))
model.compile(optimizer='adam', loss='mse')
model.fit(X, y, batch_size=None, epochs=700, verbose=0)

The output works when I only have 1 LSTM layer or return_sequences = False but gives the error 'Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2' otherwise.

PV32
  • 81
  • 1
  • 6
  • can you try `input_shape=(None,n_steps, n_features)` – Ashwin Geet D'Sa Jul 23 '19 at 23:14
  • You do not need to provide input shape for the second layer. – SaTa Jul 23 '19 at 23:52
  • Possible duplicate of [Input Shape Error in Second-layer (but not first) of Keras LSTM](https://stackoverflow.com/questions/42331396/input-shape-error-in-second-layer-but-not-first-of-keras-lstm) – MaximeKan Jul 24 '19 at 00:51
  • I complied your model without error – meowongac Jul 24 '19 at 04:00
  • I've found that setting return_sequences=True for the first LSTM and return_sequences=False for the second LSTM works, however if I replace ```model.add(Dense(denseSize, activation='softmax'))``` with ```model.add(TimeDistributed(Dense(denseSize, activation='softmax')))``` I get the same error again. – PV32 Jul 24 '19 at 16:39
  • Right now I'm just trying to predict a sine wave for testing purposes (with no luck) if that helps. – PV32 Jul 24 '19 at 16:41

0 Answers0