aldemar
aldemar

Reputation: 17

Unable to add CNN Layer after Embedding Layer Keras

I have 7 categorical Featues And i am trying to add A CNN Layer after Embedding Layer

My first Layer is input Layer Second Layer is Embedding Layer Third Layer I want to add a Conv2D Layer

I've tried input_shape=(7,36,1) in Conv_2D but that didn't work

input2 = Input(shape=(7,))
embedding2 = Embedding(76474, 36)(input2)

# 76474 is the number of datapoints (rows)
# 36 is the output dim of embedding Layer

cnn1 = Conv2D(64, (3, 3), activation='relu')(embedding2)
flat2 = Flatten()(cnn1)

But i'm getting this error

 Input 0 of layer conv2d is incompatible with the layer: expected 
 ndim=4, found ndim=3. Full shape received: [None, 7, 36]

Upvotes: 0

Views: 516

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

The output of an embedding layer is 3D, namely (samples, seq_length, features), where features = 36 is the dimensionality of the embedding space, and seq_length = 7 is the sequence length. A Conv2D layer requires an image, which is usually represented as a 4D tensor (samples, width, height, channels).

Only a Conv1D layer would make sense, as it also takes 3D-shaped data, typically (samples, width, channels), and then you need to decide if you want to do convolution across the sequence length, or across the features dimension. That's something you need to experiment with, which in the end is to decide which is the "spatial dimension" in the output of the embedding

Upvotes: 1

Related Questions