Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stack multiple lstm in keras?

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

like image 744
Tamim Addari Avatar asked Oct 30 '16 17:10

Tamim Addari


People also ask

How do I add multiple LSTM layers in keras?

The Solution. The solution is to add return_sequences=True to all LSTM layers except the last one so that its output tensor has ndim=3 (i.e. batch size, timesteps, hidden state). Setting this flag to true lets Keras know that LSTM output should contain all historical generated outputs along with time stamps (3D).

How do you stack multiple LSTM layers?

Running the example outputs a single value for the input sequence as a 2D array. To stack LSTM layers, we need to change the configuration of the prior LSTM layer to output a 3D array as input for the subsequent layer. We can do this by setting the return_sequences argument on the layer to True (defaults to False).

Should you stack LSTM layers?

"Stacking LSTM hidden layers makes the model deeper, more accurately earning the description as a deep learning technique ... The additional hidden layers are understood to recombine the learned representation from prior layers and create new representations at high levels of abstraction.

What is difference between LSTM and stacked LSTM?

The original LSTM model is comprised of a single hidden LSTM layer followed by a standard feedforward output layer. The stacked LSTM is an extension to this model that has multiple hidden LSTM layers where each layer contains multiple memory cells.

How to implement stacked LSTMs in keras?

Implement Stacked LSTMs in Keras. Each LSTMs memory cell requires a 3D input. When an LSTM processes one input sequence of time steps, each memory cell will output a single value for the whole sequence as a 2D array. We can demonstrate this below with a model that has a single hidden LSTM layer that is also the output layer.

What is stacked LSTM in Python?

Gentle introduction to the Stacked LSTM with example code in Python. The original LSTM model is comprised of a single hidden LSTM layer followed by a standard feedforward output layer. The Stacked LSTM is an extension to this model that has multiple hidden LSTM layers where each layer contains multiple memory cells.

How do I stack LSTM layers?

To stack LSTM layers, we need to change the configuration of the prior LSTM layer to output a 3D array as input for the subsequent layer. We can do this by setting the return_sequences argument on the layer to True (defaults to False). This will return one output for each input time step and provide a 3D array.

Can We continue to add hidden LSTM layers after we've added them?

We can continue to add hidden LSTM layers as long as the prior LSTM layer provides a 3D output as input for the subsequent layer; for example, below is a Stacked LSTM with 4 hidden layers.


1 Answers

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")

like image 94
Daniel De Freitas Avatar answered Sep 23 '22 13:09

Daniel De Freitas