Search code examples
pythonkeraslstmbidirectional

How to get a 2D shape ready for a Bi-LSTM in Keras


I've got a 2D numpy matrix (from a DataFrame) of already condensed word vectors (I used a max pooling technique, am trying to compare a logres to a bi-LSTM approach), and I'm not sure how to prepare it to use it in a keras model.

I'm aware of the need of a 3D tensor for the Bi-LSTM model, and have tried googling solutions, but couldn't find a solution that worked.

This is what I have right now:

# Set model parameters
epochs = 4
batch_size = 32
input_shape = (1, 10235, 3072)

# Create the model
model = Sequential()
model.add(Bidirectional(LSTM(64, return_sequences = True, input_shape = input_shape)))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))

# Try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics = ['accuracy'])

# Fit the training set over the model and correct on the validation set
model.fit(inputs['X_train'], inputs['y_train'],
            batch_size = batch_size,
            epochs = epochs,
            validation_data = [inputs['X_validation'], inputs['y_validation']])

# Get score over the test set
return model.evaluate(inputs['X_test'], inputs['y_test'])

I currently got the following error:

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

The shape of my training data (inputs['X_train']) is (10235, 3072).

Thanks so much!


Solution

  • I've made it work with the suggestion of the reply by doing the following:

    1. Remove return_sequence = True;
    2. Apply the following transformations to the X sets: np.reshape(inputs[dataset], (inputs[dataset].shape[0], inputs[dataset].shape[1], 1))
    3. Change the input shape of the LSTM layer to (10235, 3072, 1) which is the shape of X_train.