'How to change the shape of input dimention for Conv1D convolutional error in keras?

I have a binary classification problem. I want to include a Conv1D layer but am having a trouble with the input shape with changing the input shape from 2D from 3D (https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv1D).

so my code is

#Hyperparameters
EMBEDDING_DIM = 50
MAXLEN = 500 #1000, 1400
VOCAB_SIZE =  33713

DENSE1_DIM = 64
DENSE2_DIM = 32

LSTM1_DIM = 32 
LSTM2_DIM = 16
WD = 0.001
FILTERS = 64  
KERNEL_SIZE = 5

# Stacked hybrid model
model_lstm = tf.keras.Sequential([
    tf.keras.layers.Embedding(VOCAB_SIZE+1, EMBEDDING_DIM, input_length=MAXLEN,weights=[EMBEDDINGS_MATRIX], trainable=False),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(LSTM1_DIM, dropout=0.5, kernel_regularizer = regularizers.l2(WD), return_sequences=True)), 
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(LSTM2_DIM, dropout=0.5, kernel_regularizer = regularizers.l2(WD))), 
    tf.keras.layers.Dense(DENSE2_DIM, activation='relu'),

#    tf.keras.layers.Conv1D(FILTERS, KERNEL_SIZE, activation='relu'),

#    tf.keras.layers.Dropout(0.1),
#    tf.keras.layers.GlobalAveragePooling1D(), 
#    tf.keras.layers.Dense(1, activation='sigmoid')
])
...

which gives this summary

Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 embedding_10 (Embedding)    (None, 500, 50)           1685700   
                                                                 
 bidirectional_19 (Bidirecti  (None, 500, 64)          21248     
 onal)                                                           
                                                                 
 bidirectional_20 (Bidirecti  (None, 32)               10368     
 onal)                                                           
                                                                 
 dense_11 (Dense)            (None, 32)                1056      
                                                                 
=================================================================
Total params: 1,718,372
Trainable params: 32,672
Non-trainable params: 1,685,700

So if I use the Conv1D layer, I get this error:

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

I have tried, for example, input_shape = (None, 16, 32) as a parameter in the Conv1D layer, but it does not work this way..

thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source