简体   繁体   English

具有 Keras 的 CNN 的 Output 形状和参数

[英]Output shapes and parameters of a CNN with Keras

I have difficulty understanding the output shapes and number of parameters of layers in a Keras CNN model.我很难理解 Keras CNN model 中的 output 形状和层参数数。

Let's take this toy example:让我们以这个玩具为例:

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()

The output is: output 是:

_________________________________________________________________
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
_________________________________________________________________

For the first Conv1D layer, there are 7 filters of output size (60 - 40 + 1) = 21 each.对于第一个Conv1D层,有 7 个过滤器,output 大小 (60 - 40 + 1) = 21 个。 The number of parameters is (40 + 1) * 7 = 287, to take the bias into account.考虑到偏差,参数数量为 (40 + 1) * 7 = 287。 So, I'm OK with it.所以,我没问题。

But on which dimension will operate the second Conv1D layer?但是第二个Conv1D层将在哪个维度上运行呢? I guess that the output filter size is 21 - 16 + 1 = 6, but I don't understand by which operation we can go from 7 to 10 for the last dimension.我猜 output 过滤器大小是 21 - 16 + 1 = 6,但我不明白通过哪个操作我们可以将 go 从 7 到 10 用于最后一维。 I don't understand either how the number of parameters is computed.我也不明白如何计算参数数量。

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?最后,我不明白MaxPooling1D层的 output 形状,因为我希望 output 大小为 6 - 3 + 1 = 4 而不是 2。它是如何计算的?

... but I don't understand by which operation we can go from 7 to 10 for the last dimension. ...但我不明白我们可以通过哪个操作将 go 从 7 到 10 作为最后一个维度。

By the same operation that it went from 1 to 7 in the first layer: the convolution filters are applied on whole last axis (ie dimension) of their input and produce a single number at each application window.通过与第一层从 1 到 7 相同的操作:卷积滤波器应用于其输入的整个最后一个轴(即维度),并在每个应用程序 window 处生成单个数字。 There are 10 filters in the second convolution layer, therefore 10 values would be generated for each window, hence the dimension of last axis would be 10 (the same reasoning applies to the first convolution layer as well).第二个卷积层有 10 个过滤器,因此每个 window 将生成 10 个值,因此最后一个轴的维度将为 10(同样的推理也适用于第一个卷积层)。

I don't understand either how the number of parameters is computed.我也不明白如何计算参数数量。

There are 10 filters.有 10 个过滤器。 As I mentioned above, the filter is applied on the whole last axis.正如我上面提到的,过滤器应用于整个最后一个轴。 So they must have a width of 7 (ie last axis size of their input).所以它们的宽度必须为 7(即输入的最后一个轴大小)。 And the kernel size is 16. So we have: 10 * (16 * 7) + 10 (1 bias per filter) = 1130. kernel 大小为 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?最后,我不明白MaxPooling1D层的 output 形状,因为我希望 output 大小为 6 - 3 + 1 = 4 而不是 2。它是如何计算的?

The stride of 1D-pooling layer is by default equal to the pool_size .一维池化层的stride默认等于pool_size Therefore, applying on a sequence of length 6, a pooling layer of size 3 would have only 2 application windows.因此,应用于长度为 6 的序列,大小为 3 的池化层将只有 2 个应用程序 windows。

Note: You may also find this relevant answer useful about how 1D-conv works.注意:您可能还会发现此相关答案对 1D-conv 的工作原理很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM