Search code examples
pythonkeraslstmtext-classificationconv-neural-network

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


I want to feed the output of the CNN layer into LSTM layer but got an error ValueError: Input 0 is incompatible with layer lstm_15: expected ndim=3, found ndim=2 with following code:

inp = Input(shape = (max_length,))
xe = Embedding(vocabulary_size, 300, weights = [embedding_matrix], trainable = False)(inp)
x = Conv1D(512,kernel_size = 2, activation='relu',kernel_initializer = "he_uniform")(xe)
x = GlobalMaxPooling1D()(x)
x = LSTM(128)(x)
x = Dense(11, activation = "sigmoid")(x)

inputs shapes:

embedding_matrix: (26441, 300)
inp : TensorShape([Dimension(None), Dimension(3146)])
X_train :(1432, 3146)
Y_train: (1432, 11)
vocabulary_size: 26441
max_length: 3146

Someone can help me


Solution

  • This is because when you apply GlobalMaxPooling1D, it returns the tensor with shape (batch_size, 512). As mentioned in the docs, this layer downsamples the input representation by taking the maximum value over the time dimension. You have two options either not to use GlobalMaxPool1D (you can instead use local pooling layers such as MaxPool1D) or you can use RepeatVector to change the shape of output of GlobalMaxPool1D from (batch_size, 512) to (batch_size, n, 512) where n is the parameter to RepeatVector defining, how many times you want the sequence to be repeated.