How to stack multiple lstm in keras?

Tamim Addari picture Tamim Addari · Oct 30, 2016 · Viewed 40.9k times · Source

I am using deep learning library keras and trying to stack multiple LSTM with no luck. Below is my code

model = Sequential()
model.add(LSTM(100,input_shape =(time_steps,vector_size)))
model.add(LSTM(100))

The above code returns error in the third line Exception: Input 0 is incompatible with layer lstm_28: expected ndim=3, found ndim=2

The input X is a tensor of shape (100,250,50). I am running keras on tensorflow backend

Answer

Daniel Adiwardana picture Daniel Adiwardana · Oct 31, 2016

You need to add return_sequences=True to the first layer so that its output tensor has ndim=3 (i.e. batch size, timesteps, hidden state).

Please see the following example:

# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32))  # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))

From: https://keras.io/getting-started/sequential-model-guide/ (search for "stacked lstm")