I have difficulty understanding the output shapes and number of parameters of layers in a Keras CNN model.
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:
_________________________________________________________________
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. The number of parameters is (40 + 1) * 7 = 287, to take the bias into account. So, I'm OK with it.
But on which dimension will operate the second Conv1D
layer? 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. 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?