简单的卷积
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
对应的架构
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 13, 13, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 11, 11, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2D) (None, 5, 5, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 3, 3, 64) 36928
_________________________________________________________________
flatten_1 (Flatten) (None, 576) 0
_________________________________________________________________
dense_1 (Dense) (None, 64) 36928
_________________________________________________________________
dense_2 (Dense) (None, 10) 650
=================================================================
Total params: 93,322
Trainable params: 93,322
conv2d_1 (Conv2D) 输入为(28,28,1),卷积核尺寸为(3,3),过滤器个数为32,步长默认(1, 1),所以:
Output: (输入尺寸-卷积核尺寸+2xpadding)/步长+1
(28-3+2x0)/1+1=26--->(26,26,32)
Shape Param: 卷积核尺寸*2 x 通道数 x 过滤器数+过滤器数
W=3*2x1x32=288,
B=32,
Param=288+32=B+W=320
conv2d_1 (Conv2D)为(26,26,32),经过池化大小为(2,2),步长默认: If None, it will default to pool_size
.
输出尺寸: (输入尺寸-池化尺寸)/步长+1
(26-2)/2+1=13---->(13,13,32)
1.卷积后的全连接成的参数计算
全连接的前层conv2d_3 (Conv2D) (None, 3, 3, 64)
全连接层的神经元数 64
Shape Param:前层的尺寸*2x前层的核数x全连接层的神经元数+全连接层的神经元数
3*2 x 64 x 64 + 64= 36928
2.隐藏层的下一层全连接成的参数计算
前层神经元数:64
当前神经元数:10
Shape Param:前层神经元数 x 当前神经元数 + 当前神经元数
64 x 10 + 10=650
为所以参数的总和:320 + 18496 +36928 + 36928 +650 = 93322