Conv2D 层中的权重是如何组织的?

数据挖掘 喀拉斯 美国有线电视新闻网
2022-02-25 12:13:38

我从这个例子中使用 Keras 训练了一个模型模型摘要向我展示了这个结果

    Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               1179776   
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290      
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0

有 8 层,model.get_weights()也显示了 8 的第一个维度。正如我正确理解的那样,Pooling 和 Flatten 之类的东西是输入矩阵上的“运算符”,那么为什么它被呈现为一个层呢?如何理解权重数组中存储的内容,例如在池化层(model.get_weights()[2])中?

2个回答

如果您使用下面的 for 循环打印这些形状

weights_m=model.get_weights()
for i in range(8):
  print(weights_m[i].shape)

你会得到输出

(3, 3, 1, 32)
(32,)
(3, 3, 32, 64)
(64,)
(9216, 128)
(128,)
(128, 10)
(10,)

所以我们会得到一层权重和偏差。我们总共有 4 层(2 conv + 2 dense)所以 8 个权重向量。

model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))

3x3 weights + 1 bias for each feature map. It has 32 feature map.
This is first 2 lines. (3X3+1)X32. At this points feature size is 26X26(valid padding)

model.add(Conv2D(64, (3, 3), 激活='relu'))

3x3 weights connected to 32 previous feature maps + 1 bias for each feature map. It has 64 feature maps.
This is 3rd and 4th lines (3X3X32+1)X64. At this points feature size is 24X24(valid padding)

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

At this points, feature size is 12X12(Pooling)

model.add(Dropout(0.25)) model.add(Flatten())

64 feature maps of size 12X12 are flattened. 144X64=9216 mapped to 128 neurons + 128 biases
This is 5th and 6th lines

model.add(Dense(128, activation='relu')) model.add(Dropout(0.5))

128 neurons are mapped to 10 neurons + 10 biases
This is 7th and 8th lines

model.add(密集(num_classes,激活='softmax'))




如何计算卷积神经网络的参数数量