Neabfi
Neabfi

Reputation: 4741

Relation between kernel size and input size in CNN

I have a Conv1D layer in keras with a kernel size of 3 and a stride length of 1. I have the following error when I'm trying to handle input size of 5 but everything works with input size of 6.

InvalidArgumentError (see above for traceback): Computed output size would be negative:
-1 [input_size: 0, effective_filter_size: 3, stride: 1]

I thought that kernel of size 3 needs input of size at least 3.

EDIT: Here is the model, the input size is variable, the problem I have is with input of size 5.

model = Sequential()
model.add(Conv1D(
    input_shape=(None, 4),
    filters=64,
    kernel_size=3,
    activation='relu'))
model.add(Conv1D(
    filters=32,
    kernel_size=3,
    activation='relu'))
model.add(Conv1D(
    filters=16,
    kernel_size=2,
    activation='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(number_of_classes))
model.add(Softmax(axis=-1))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Upvotes: 1

Views: 841

Answers (1)

Se7eN
Se7eN

Reputation: 608

To ensure that the size of your output feature maps is the same as your input feature maps, you have to pad the input using 'same' padding.

model.add(Conv1D(
    input_shape=(None, 4),
    filters=64,
    kernel_size=3,
    activation='relu',
    padding='same'))

Upvotes: 1

Related Questions