将Keras和Tensorflow与28x28x1数据集结合使用。当我运行以下代码时,它可以正常工作:
model = Sequential()
model.add(Convolution2D(8, 3, strides = 3, activation='relu', input_shape=(28,28,1),data_format = 'channels_last',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(16, 3, strides = 3, activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(32, 3, strides = 3, activation='relu',padding='same'))
#model.add(AveragePooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#Training the model in kreas
history = model.fit(train_dataset_kr,train_labels_kr,epochs = 500, validation_data = (valid_dataset_kr,vaild_labels_kr),batch_size = 256)
#model.train_on_batch(train_dataset_kr,train_labels_kr)
score = model.evaluate(test_dataset_kr, test_labels_kr, verbose=0)
一旦我输入了最终的pooling2d(在本例中为AveragePooling2d),我就会收到以下错误:
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'average_pooling2d_1/AvgPool' (op: 'AvgPool') with input shapes: [?,1,1,32].
根据输入文件的大小,我想我应该可以做3个pooling2D。关于我可能做错什么的任何想法?
答案 0 :(得分:1)
你为你的模特设定了太大的进步。让我们检查网络的输出形状:
model = Sequential()
model.add(Convolution2D(8, 3, strides = 3, activation='relu', input_shape=(28,28,1),data_format = 'channels_last',padding='same'))
# Shapes: (28, 28, 1) -> (10, 10, 8)
model.add(MaxPooling2D(pool_size=(2,2)))
# Shapes: (10, 10, 8) - > (5, 5, 8)
model.add(Dropout(0.25))
model.add(Convolution2D(16, 3, strides = 3, activation='relu',padding='same'))
# Shapes: (5, 5, 8) -> (2, 2, 16)
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(32, 3, strides = 3, activation='relu',padding='same'))
# Shapes: (2, 2, 16) -> (1, 1, 32)
#model.add(AveragePooling2D(pool_size=(2,2)))
# (1, 1, 32) - this shape is too small for pooling.
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
您确定要将strides
参数设置为值3
吗?它将每层的输出减少了3
。