2
votes

I have the following network:

model = Sequential()
model.add(Embedding(400000, 100, weights=[emb], input_length=12, trainable=False))

model.add(Conv2D(256,(2,2),activation='relu'))

the output from the embedding layer is of shape (batchSize, 12, 100). The conv2D layer requires an input of shape (batchSize, filter, 12, 100), and I get the following error:

 Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=3

So, how can I expand the output from the embedding layer to make it proper for the Conv2D layer?

I'm using Keras with Tensorflow as the back end.

2
Replace your Conv2d with Conv1D. Expand dimension is not needed.Amir

2 Answers

2
votes

Adding a reshape Layer should be the way to go https://keras.io/layers/core/#reshape Depending on the concrete situation Conv1D cold although work.

2
votes

I managed to add another dimension with the following piece of code:

model = Sequential()
model.add(Embedding(400000, 100, weights=[emb], input_length=12, trainable=False))
model.add(Lambda(lambda x: expand_dims(x, 3)))
model.add(Conv2D(256,(2,2),activation='relu'))