通过model.summary()输出模型结构信息
全连接神经网络创建模型代码
model = tf.keras.Sequential()
model.add(Flatten(input_shape=(32,32,3)))
model.add(Dropout(0.1))
model.add(Dense(2048,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1024,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(256,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(10,activation='softmax'))
全连接神经网络的模型结构计算详解
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param
=================================================================
flatten (Flatten) (None, 3072) 0
_________________________________________________________________
dropout (Dropout) (None, 3072) 0
_________________________________________________________________
dense (Dense) (None, 2048) 6293504
_________________________________________________________________
dropout_1 (Dropout) (None, 2048) 0
_________________________________________________________________
dense_1 (Dense) (None, 1024) 2098176
_________________________________________________________________
dropout_2 (Dropout) (None, 1024) 0
_________________________________________________________________
dense_2 (Dense) (None, 512) 524800
_________________________________________________________________
dropout_3 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 256) 131328
_________________________________________________________________
dropout_4 (Dropout) (None, 256) 0
_________________________________________________________________
dense_4 (Dense) (None, 10) 2570
=================================================================
Total params: 9,050,378
Trainable params: 9,050,378
Non-trainable params: 0
全连接层神经网络的Param,说明的是每层神经元权重的个数,所以它的计算如下:
Param = (输入数据维度+1)* 神经元个数 之所以要加1,是考虑到每个神经元都有一个Bias。
卷积神经网络创建模型代码
model = Sequential()
model.add(Convolution2D(
input_shape=(32,32,3),
filters=32,
kernel_size=(5,5),
padding='same',
))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size=(2,2),
strides=(2,2),
padding='same',
))
model.add(Convolution2D(
filters=64,
kernel_size=(5,5),
padding='same',
))
model.add(Activation('relu'))
model.add(MaxPooling2D(
pool_size=(2,2),
strides=(2,2),
padding='same',
))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
卷积神经网络的模型结构计算详解
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param
=================================================================
conv2d (Conv2D) (None, 32, 32, 32) 2432
_________________________________________________________________
activation (Activation) (None, 32, 32, 32) 0
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 16, 16, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 16, 16, 64) 51264
_________________________________________________________________
activation_1 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 8, 8, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 4096) 0
_________________________________________________________________
dense (Dense) (None, 64) 262208
_________________________________________________________________
activation_2 (Activation) (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 650
_________________________________________________________________
activation_3 (Activation) (None, 10) 0
=================================================================
Total params: 316,554
Trainable params: 316,554
Non-trainable params: 0
Param的计算方法如下:
(卷积核长度卷积核宽度通道数+1)* 卷积核个数
- 第一个CONV层,Convolution2D(32, kernel_size=(5, 5),
input_shape=(32,32,3)),所以计算得Param=(5*5*3+1)*32 = 2432. - 第二个CONV层,Convolution2D(64, kernel_size=(5, 5),activation=‘relu’),经过第一个层32个卷积核的作用,第二层输入数据通道数为32,Param=(5*5*32+1)*64 = 51264.
- Flatten扁平:由池化后的(8,8,64)打成一维,即(8*8*64) = 4096
- 第一个Dense层,由于经过Flatten操作后,输出变成了4096,而第一个Dense中有64个卷积核,所以计算得Param =
64*(4096+1)= 262208 - 第二个Dense层,有10个卷积核,所以计算得Param =
10*(64+1)= 650
|