简体   繁体   中英

ValueError: Negative dimension size caused by subtracting 2 from 1 for '{{node conv2d_3/Conv2D}}

For this CNN model:

# Construct model
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(num_rows, num_columns, num_channels), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))

model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))

model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=1))
model.add(Dropout(0.2))
model.add(GlobalAveragePooling2D())

model.add(Dense(num_labels, activation='softmax'))

I am getting the ValueError message:

ValueError: Negative dimension size caused by subtracting 2 from 1 for '{{node conv2d_3/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](Placeholder, conv2d_3/Conv2D/ReadVariableOp)' with input shapes: [?,1,84,64], [2,2,64,128].

I tried putting padding='same' in the last convolution layer like:

model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
model.add(MaxPooling2D(pool_size=1), padding='same')

as suggested here, but it still did not work.

Why is this error occurring and how to fix it? Thanks!

This error is occurring because the dimensions of your input are too small. With you current setup, the num_rows and num_columns cannot have values lower than 9. To fix the issue, one thing you could do is to increase the values of the variables (and thus your input size) num_rows and num_columns to at least 9 .

There is one more thing you could do. There are certain layers in your.network that decrease the dimensionality of the data as it propagates through your.network. These are your Conv2D layers, and your first MaxPooling2D layer. You can preserve the size of your input by making changes to these layers. For the Conv2D layer, you can use the padding="same" argument when creating your layer like so:

model.add(Conv2D(filters=32, kernel_size=2, activation='relu', padding='same'))

For the MaxPooling2D layer, you can use the pool_size=1 argument when creating your layer, as you are already using with all of your MaxPooling2D layers but the first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM