Use of LSTM layer inside CNN provides ValueError

Issue

I have a problem. I want to use LSTM inside my CNN for a NLP problem. But unfortunately what I got is the following error ValueError: Input 0 of layer "conv1d_37" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 128). How can I use the LSTM layer?

from keras.models import Sequential
from keras.layers import Input, Embedding, Dense, GlobalMaxPooling1D, Conv2D, MaxPool2D, LSTM, Bidirectional, Lambda, Conv1D, MaxPooling1D, GlobalMaxPooling1D

model_lstm = Sequential()

model_lstm.add(
        Embedding(vocab_size
                ,embed_size
                ,weights = [embedding_matrix] #Supplied embedding matrix created from glove
                ,input_length = maxlen
                ,trainable=True)
         )
model_lstm.add(SpatialDropout1D(rate = 0.4))
model_lstm.add(Conv1D(256, 7, activation="relu"))
model_lstm.add(MaxPooling1D())
model_lstm.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model_lstm.add(Conv1D(128, 5, activation="relu"))
model_lstm.add(MaxPooling1D())
model_lstm.add(GlobalMaxPooling1D())
model_lstm.add(Dropout(0.3))
model_lstm.add(Dense(128, activation="relu")))
model_lstm.add(Dense(4, activation='softmax'))
print(model_lstm.summary())
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [481], in <cell line: 25>()
     23 model_lstm.add(MaxPooling1D())
     24 model_lstm.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
---> 25 model_lstm.add(Conv1D(128, 5, activation="relu"))
     26 model_lstm.add(MaxPooling1D())
     27 #model_lstm.add(Flatten())

File ~\Anaconda3\lib\site-packages\tensorflow\python\training\tracking\base.py:587, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
    585 self._self_setattr_tracking = False  # pylint: disable=protected-access
    586 try:
--> 587   result = method(self, *args, **kwargs)
    588 finally:
    589   self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

File ~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File ~\Anaconda3\lib\site-packages\keras\engine\input_spec.py:228, in assert_input_compatibility(input_spec, inputs, layer_name)
    226   ndim = x.shape.rank
    227   if ndim is not None and ndim < spec.min_ndim:
--> 228     raise ValueError(f'Input {input_index} of layer "{layer_name}" '
    229                      'is incompatible with the layer: '
    230                      f'expected min_ndim={spec.min_ndim}, '
    231                      f'found ndim={ndim}. '
    232                      f'Full shape received: {tuple(shape)}')
    233 # Check dtype.
    234 if spec.dtype is not None:

ValueError: Input 0 of layer "conv1d_37" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 128)

Solution

May be try using return_sequences=True . It may resolve the error.

Bcz the dimensions LSTM is expecting is (None, 1, 128) but right now it is getting only 2 dimensions which are (None, 128).

Answered By – Furqan Ali

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published