Discussions>Input 0 is incompatible with layer lstm: expected ndim=3, found ndim=2 in Keras>

Input 0 is incompatible with layer lstm: expected ndim=3, found ndim=2 in Keras

I'm using Keras 2.2 LSTM and want to build a recurrent network, but I can't find the reason of this error. Here is the code

 model=Sequential()

 model.add(LSTM(16, return_sequences=True, activation='relu'))
 model.add(LSTM(16, activation='relu'))

 model.add(Dense(32, activation='relu'))
 model.add(Dropout(0.3))

 model.add(Dense(10, activation='softmax'))


 model.compile(optimizer='adam',
          loss='mean_squared_error',
          metrics=['accuracy'])

 Net = model.fit(x_train, y_train, batch_size=32, epochs=40)

I'm still getting this issue

ValueError: Input 0 is incompatible with layer lstm: expected ndim=3, found ndim=2

Any ideas about this?

4 votesJO242.00
1 Answers
JW270.00
5

There are 2 main points related to your issue

  1. Keras LSTM takes and input with shape of (n_examples, n_times, n_features) and your layers input has to have this shape
  2. You will have to put return_sequences=True for the second LSTM layer as well
Reply
AS
0
model = Sequential()
model.add(LSTM(units=75, activation='relu', return_sequences=True, input_shape=(timesteps,data_dim)))
model.add(LSTM(units=30),return_sequences=True)
model.add(LSTM(units=30))
model.add(Dense(1))

---------------------------------------------------------------------------TypeError Traceback (most recent call last) <ipython-input-261-ac177975b833> in <module> 1 model = Sequential() 2 model.add(LSTM(units=75, activation='relu', return_sequences=True, input_shape=(timesteps,data_dim)))----> 3 model.add(LSTM(units=30),return_sequences=True) 4 model.add(LSTM(units=30)) 5 model.add(Dense(1))TypeError: add() got an unexpected keyword argument 'return_sequences'
AS
0
model = Sequential()
model.add(LSTM(units=75, activation='relu', return_sequences=True, input_shape=(timesteps,data_dim)))
model.add(LSTM(units=30),return_sequences=True)
model.add(LSTM(units=30))
model.add(Dense(1))

---------------------------------------------------------------------------TypeError Traceback (most recent call last) <ipython-input-261-ac177975b833> in <module> 1 model = Sequential() 2 model.add(LSTM(units=75, activation='relu', return_sequences=True, input_shape=(timesteps,data_dim)))----> 3 model.add(LSTM(units=30),return_sequences=True) 4 model.add(LSTM(units=30)) 5 model.add(Dense(1))TypeError: add() got an unexpected keyword argument 'return_sequences'
AS
0
model = Sequential()
model.add(LSTM(units=75, activation='relu', return_sequences=True, input_shape=(timesteps,data_dim)))
model.add(LSTM(units=30),return_sequences=True)
model.add(LSTM(units=30))
model.add(Dense(1))

---------------------------------------------------------------------------TypeError Traceback (most recent call last) <ipython-input-261-ac177975b833> in <module> 1 model = Sequential() 2 model.add(LSTM(units=75, activation='relu', return_sequences=True, input_shape=(timesteps,data_dim)))----> 3 model.add(LSTM(units=30),return_sequences=True) 4 model.add(LSTM(units=30)) 5 model.add(Dense(1))TypeError: add() got an unexpected keyword argument 'return_sequences'
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?