Stack multiple keras lstm layers

I'm using Keras 2.1.* and want to stack multiple keras lstm layers into one model.

I was trying something like this.

model = Sequential()
model.add(LSTM(10, input_shape =(n, s)))
model.add(LSTM(10))

but it didn't work and I get the following error.

Exception: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2
keraslstm
3 votesLP190.00
1 Answers
JW234.00
2

It's just because you are using it wronlgy.

The input shape should be: (batch_size, timesteps, data_dim) and you have to put return_sequences=True for the first layer.

Here is how it looks like

# shape: (batch_size, n, s)
model = Sequential()
model.add(LSTM(10, return_sequences=True,
               input_shape=(n, s)))
model.add(LSTM(10, return_sequences=True))
model.add(LSTM(10))
model.add(Dense(1, activation='sigmoid'))

Also there is another discussion with the same issue. Take a look at it.

Input 0 is incompatible with layer lstm

Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?