Sasquatch Man
Sasquatch Man

Reputation: 73

Issue trying to implement stacked LSTM layers with Keras

I'm trying to add more LSTM layers to my neural net, but I keep getting the following error:

ValueError: Error when checking target: expected dense_4 to have 2 dimensions, but got array with shape (385, 128, 1) 

The code for my model is as follows:

model = Sequential()

model.add(LSTM(60, return_sequences=True, input_shape=(128, 14)))

model.add(LSTM(60, return_sequences=False))

model.add(Dense(1))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(data_train, RUL_train, epochs=number_epochs, batch_size=batch_size, verbose=1)

It works fine when I remove the second LSTM layer. Or if I add more dense layers. Just not when I add the LSTM layer. RUL_train has shape (385, 128, 1). The output of model.summary is as follows:

_________________________________________________________________
   Layer (type)                 Output Shape              Param #   
=================================================================
lstm_15 (LSTM)               (None, 128, 60)           18000     
_________________________________________________________________
lstm_16 (LSTM)               (None, 60)                29040     
_________________________________________________________________
dense_7 (Dense)              (None, 1)                 61        
=================================================================
Total params: 47,101
Trainable params: 47,101
Non-trainable params: 0
_________________________________________________________________

Any help appreciated.

Upvotes: 1

Views: 311

Answers (2)

Daniel Möller
Daniel Möller

Reputation: 86600

Your labels array has three dimensions: (385,128,1).

So, what is your purpose?

  • Classify all steps in each sequence? - All LSTMs must use return_sequence=True
  • One class for the entire sequence? - Fix your labels array somehow to be (samples,1).

Upvotes: 1

Brendan
Brendan

Reputation: 101

This is a bug that was introduced in Keras 2.1.0 (and wasn't completely fixed in 2.1.1). Try installing Keras 2.0.9 or earlier:

pip uninstall keras
pip install keras==2.0.9

https://github.com/fchollet/keras/issues/8481

Upvotes: 0

Related Questions