3
$\begingroup$

I am trying to implement a 1 channel CNN by slightly changing this article: this article. The problem is that I am new to keras and deep learning and I don't know this far why I am getting this error:

ValueError: Negative dimension size caused by subtracting 100 from 1 for 'conv2d_1/convolution' (op: 'Conv2D') with input shapes: [?,1,70,100], [100,100,100,64]

Obviously, it's a mismatch in the dimensions.

I am using this code code:

from keras.layers import Embedding
from keras.layers import Conv2D
from keras.models import Sequential
from keras.layers import MaxPooling2D
from keras.layers import Reshape
import pdb
Vocab_Size=11123
MAX_SEQUENCE_LENGTH=70
EMBED_DIM=100
model = Sequential()
embed1=Embedding(Vocab_Size+1,EMBED_DIM,input_length=MAX_SEQUENCE_LENGTH,input_shape=(MAX_SEQUENCE_LENGTH,EMBED_DIM,1))
nb_labels=6
model = Sequential()
model.add(embed1)
model.add(Reshape((1,MAX_SEQUENCE_LENGTH, EMBED_DIM)))
model.add(Conv2D(64, strides=5, kernel_size=EMBED_DIM, activation="relu", padding='valid'))
model.add(MaxPooling2D((MAX_SEQUENCE_LENGTH-5+1,1)))
model.add(Flatten())
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(len(nb_labels), activation="softmax"))

model.compile(loss='categorical_crossentropy',
                optimizer='rmsprop',
                metrics=['acc'])

Edit1: I updated as mentionned by Media padding to same. Now I have another problem for the next layer:

ValueError: Negative dimension size caused by subtracting 66 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,14,64].
$\endgroup$
2
$\begingroup$

I guess you should change the following line to solve the problem:

model.add(Conv2D(64, strides=5, kernel_size=EMBED_DIM, activation="relu", padding='valid'))

instead use this code:

model.add(Conv2D(64, strides=5, kernel_size=EMBED_DIM, activation="relu", padding='same'))

this will keep the dimension of your input. If it does not work, let me know to help you more.

| improve this answer | |
$\endgroup$
  • $\begingroup$ It worked Thanks! but now I have now another problem.Please have a look below! $\endgroup$ – ChiPlusPlus Dec 11 '17 at 12:13
  • $\begingroup$ I solved the problem by applying also padding='same' $\endgroup$ – ChiPlusPlus Dec 11 '17 at 13:48
  • $\begingroup$ Did you solve it? because I did not understand what you meant :) $\endgroup$ – Media Dec 11 '17 at 14:04
  • $\begingroup$ Yes. I got the same error with maxpooling layer. I solved exactly with the same method. $\endgroup$ – ChiPlusPlus Dec 11 '17 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.