当我尝试定义我的模型时,收到以下错误消息:ValueError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_19/convolution' (op: 'Conv2D') with input shapes: [?,1,1,32], [5,5,32,64].
以下是重现此问题的代码段:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D
import numpy as np
nb_filters = 32
batch_size = 128
epochs = 5
nb_classes = 10
pool_size = (3, 3)
kernel_size = (5, 5)
img_rows = 32
img_cols = 32
input_shape = (img_rows, img_cols, 1)
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size,
input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Convolution2D(nb_filters, kernel_size))
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=pool_size))
model.add(Convolution2D(nb_filters*2, kernel_size))
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=pool_size))
model.add(Flatten())
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
使用Python 2.7.11和Keras 2.1.1在MacOS上观察到了这个问题。
我构建模型的方式有问题吗?
这是我想要构建的架构:
答案 0 :(得分:7)
对于模型中应用的所有池,输入形状只是小的。
让我们来看看你的模型:
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size, input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Convolution2D(nb_filters, kernel_size))
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=pool_size))
直到这里一切都很好。 AveragePooling2D图层的输出形状为(1,1,32)。
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 28, 28, 32) 832
_________________________________________________________________
activation_1 (Activation) (None, 28, 28, 32) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 9, 9, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 5, 5, 32) 25632
_________________________________________________________________
activation_2 (Activation) (None, 5, 5, 32) 0
_________________________________________________________________
average_pooling2d_1 (Average (None, 1, 1, 32) 0
=================================================================
现在你想应用另一个内核大小为3的Convolution2D层,尽管最后一层的输出只是(1,1,32)。所以你想要学习一个大小(3,3)的内核,它实际上比你想要学习它的数据要小。
现在,您可以增加数据的输入形状((79,79,?)是适合您模型的最小值),也可以更改模型。
padding='same'
添加到卷积层干杯