Search code examples
keraslstm

LSTM - Incompatible layer, dimension mismatch?


I'm trying to set up an LSTM model with Keras. The training data has the dimension [7165, 27], and with my current setup it throws the following error:

  File "C:\Users\Eier\Anaconda3\lib\site-packages\keras\models.py", line 441, in __init__
    self.add(layer)

  File "C:\Users\Eier\Anaconda3\lib\site-packages\keras\models.py", line 497, in add
    layer(x)

  File "C:\Users\Eier\Anaconda3\lib\site-packages\keras\layers\recurrent.py", line 500, in __call__
    return super(RNN, self).__call__(inputs, **kwargs)

  File "C:\Users\Eier\Anaconda3\lib\site-packages\keras\engine\topology.py", line 575, in __call__
    self.assert_input_compatibility(inputs)

  File "C:\Users\Eier\Anaconda3\lib\site-packages\keras\engine\topology.py", line 474, in assert_input_compatibility
    str(K.ndim(x)))

ValueError: Input 0 is incompatible with layer lstm_64: expected ndim=3, found ndim=4 

I know this error is fairly common, but none of the many different solutions found online have worked for me yet. I have already trying reshaping the training data to a 3D matrix, fooling around with different layer combinations, explictly stating batch size, using Flatten() and more to no avail. Would be very grateful if someone could push me in the right direction for fixing this.

Code snippet:

input_dim = 27
units = 5
timesteps = 1 
samples =  X_train.shape[0]

X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

model = Sequential([
    LSTM(units, return_sequences=True, stateful = True, input_shape=(samples,timesteps,input_dim)),
    Dropout(0.2),
    LSTM(units,return_sequences=False),
    Dropout(0.2),
    Dense(1),
    Activation('softmax'),
])

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
model.fit(X_train, y_train, batch_size = 32, epochs = 60)

Solution

  • As pointed out by @ShubhamPanchal in comments you don't need to specify samples dimension. LSTM layer expects input to have shape [batch_size, time_steps, channels), so when you pass input_shape argument you have to pass tuple specifying time_steps and channels dimension.

    LSTM(32, return_sequences=True, stateful = True, input_shape=(time_steps, input_dim))
    

    Since you are using stateful lstm you need to specify the batch_size argument also. So the full code for the model would be,

    model = Sequential([
        LSTM(units, return_sequences=True, stateful = True, input_shape=(timesteps,input_dim), batch_size=batch_size),
        Dropout(0.2),
        LSTM(units,return_sequences=False),
        Dropout(0.2),
        Dense(1),
        Activation('softmax'),
    ])