1

我是 Keras 的初学者。我正在尝试建立一个我正在使用顺序模型的模型。当我试图通过使用 maxpooling 函数将输入大小从 28 减少到 14 或更少时,maxpooling 函数结果不会在调用 model.summary() 函数时显示。我希望在训练后达到 0.99 或更高的准确度,即调用 model.score() 时,准确度结果应为 0.99 或更高。到目前为止的模型构建我的我可以在这里看到

from keras.layers import Activation, MaxPooling2D
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1)))
model.add(Convolution2D(32, 1, activation='relu'))
MaxPooling2D(pool_size=(2, 2))
model.add(Convolution2D(32, 26))
model.add(Convolution2D(10, 1))
model.add(Flatten())
model.add(Activation('softmax'))

model.summary()

输出 -

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_29 (Conv2D)           (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_30 (Conv2D)           (None, 26, 26, 32)        1056      
_________________________________________________________________
conv2d_31 (Conv2D)           (None, 1, 1, 32)          692256    
_________________________________________________________________
conv2d_32 (Conv2D)           (None, 1, 1, 10)          330       
_________________________________________________________________
flatten_7 (Flatten)          (None, 10)                0         
_________________________________________________________________
activation_7 (Activation)    (None, 10)                0         
=================================================================
Total params: 693,962
Trainable params: 693,962
Non-trainable params: 0
____________________________

我使用的批量大小是 32,时期数是 10。

model.compile(loss='categorical_crossentropy',
         optimizer='adam',
         metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=32, nb_epoch=10, verbose=1)

score = model.evaluate(X_test, Y_test, verbose=0)
print(score)

训练后的输出 -

[0.09016687796734459, 0.9814]
4

1 回答 1

1

您没有将 Maxpooling2D 层添加到您的模型中......

model.add(MaxPooling2D(pool_size=(2, 2)))

此外,您的 maxpooling 的输出将具有形状 (None, 13, 13, 32),下一层的卷积核 (在您的情况下为 26) 不能大于您当前的尺寸 (13)。你的代码应该是这样的:

from keras.layers import Activation, MaxPooling2D, Dense

model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1)))
model.add(Convolution2D(32, 1, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 8))
model.add(Convolution2D(10, 6))
model.add(Flatten())
model.add(Activation('softmax'))
print(model.summary())

输出

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 26, 26, 32)        1056      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 6, 6, 32)          65568     
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 1, 1, 10)          11530     
_________________________________________________________________
flatten_1 (Flatten)          (None, 10)                0         
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 78,474
Trainable params: 78,474
Non-trainable params: 0
___________________________________

PS:我会考虑在输出端使用更小的内核大小和 FC 层,因为在大多数情况下,它比尝试匹配卷积输出形状更实用

于 2019-03-01T17:19:48.850 回答