Input 0 of layer "bidirectional_2" is incompatible with the layer: expected ndim=3, found ndim=2

1.3k views Asked by At

I am trying to classify text with bi-lstm but while I run model.predict on new dataset it is giving me this error: Input 0 of layer "bidirectional_2" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 100) Shape of my training data is:(39780, 2) Shape of my testing data is: (28619, 2)

model = Sequential()
model.add(Embedding(len(word_index) + 1, embed_size, weights=[embedding_matrix]))
model.add(Bidirectional(LSTM(50, return_sequences=True, dropout=0.1, recurrent_dropout=0.1)))
model.add(Bidirectional(LSTM(30,return_sequences=True)))
model.add(GlobalMaxPool1D())
model.add(Dense(50, activation="relu"))
model.add(Dropout(0.1))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

history=model.fit(X_train, Y_train, batch_size=64, epochs=5)
y_pred = model.predict([X_test], batch_size=26, verbose=1)

1

There are 1 answers

0
Arman Asgharpoor On

you should use Reshape layer after Bidirectional layer

This might works:

model = Sequential()
model.add(Embedding(len(word_index) + 1, embed_size, weights=[embedding_matrix]))
model.add(Bidirectional(LSTM(50, return_sequences=True, dropout=0.1, recurrent_dropout=0.1)))
model.add(Reshape((100, 1), input_shape = (100, )))
model.add(Bidirectional(LSTM(30,return_sequences=True)))