使用 Keras 输出 CNN 的形状和参数

Output shapes and parameters of a CNN with Keras

我很难理解 Keras CNN 模型中层的输出形状和参数数量。

让我们举个例子:

model = Sequential()
model.add(Conv1D(7, kernel_size=40, activation="relu", input_shape=(60, 1)))
model.add(Conv1D(10, kernel_size=16, activation="relu"))
model.add(MaxPooling1D(pool_size=3))
model.summary()

输出为:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_17 (Conv1D)           (None, 21, 7)             287       
_________________________________________________________________
conv1d_18 (Conv1D)           (None, 6, 10)             1130      
_________________________________________________________________
max_pooling1d_11 (MaxPooling (None, 2, 10)             0         
=================================================================
Total params: 1,417
Trainable params: 1,417
Non-trainable params: 0
_________________________________________________________________

对于第一个 Conv1D 层,有 7 个过滤器,每个过滤器的输出大小为 (60 - 40 + 1) = 21。参数数量为 (40 + 1) * 7 = 287,以考虑偏差。所以,我可以接受。

但是第二个Conv1D层将在哪个维度上运行?我猜输出过滤器的大小是 21 - 16 + 1 = 6,但我不明白我们可以通过哪种操作将最后一个维度从 7 变为 10。我也不明白参数个数是怎么计算的

最后,我不明白 MaxPooling1D 层的输出形状,因为我希望输出大小为 6 - 3 + 1 = 4 而不是 2。它是如何计算的?

... but I don't understand by which operation we can go from 7 to 10 for the last dimension.

通过在第一层中从 1 到 7 的相同操作:卷积滤波器应用于其输入的整个最后一个轴(即维度),并在每个应用程序中产生一个数字 window .第二个卷积层中有 10 个过滤器,因此每个过滤器会生成 10 个值 window,因此最后一个轴的维度将为 10(同样的推理也适用于第一个卷积层)。

I don't understand either how the number of parameters is computed.

有 10 个过滤器。正如我上面提到的,过滤器应用于整个最后一个轴。所以它们的宽度必须为 7(即输入的最后一个轴大小)。内核大小为 16。所以我们有:10 * (16 * 7) + 10(每个过滤器 1 个偏差)= 1130.

Finally, I don't understand the output shape of the MaxPooling1D layer, since I would expect the output size to be 6 - 3 + 1 = 4 and not 2. How is it computed?

1D-pooling层的stride默认等于pool_size。因此,在长度为 6 的序列上应用,大小为 3 的池化层将只有 2 个应用 windows.

注意:您可能还会发现 关于 1D-conv 的工作原理很有用。