Size of output of a Conv1D layer in Keras

Viewed 3348

I am trying to understand the output of a 1D convolution layer applied on a number of batches (3 in this case) of 2D input shapes (6x6).

The output of the code below is (4, 10, 32). This answer is quite straight-forward for the first 2 indices.

  • (4) We plug in N examples, we get out N examples.
  • (8) When doing the convolution between a (10, 128) * (3, 1) and because the default padding is set to "valid", two of the values in our input space won't be mapped to our final result, that's why we get 8.
  • I don't understand why the layer outputs 32 as the final index. What is the actual role of the filters argument in the Conv1D layer? It's not really intuitive for me how does the layer operates the final output as stated by the phrase down below.

By the documentation this should be the output shape

Output shape: 3+D tensor with shape: batch_shape + (new_steps, filters) steps value might have changed due to padding or strides.

input_shape = (4, 10, 128)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv1D(
    32, 3, input_shape=input_shape[1:])(x)
print(y.shape) # (4, 10, 32)
1 Answers

Your two first statement are correct. The filter can be seen as the number of time you'll performe convolution over the series.

In the following picture, you can see the operation performed. The left object represents your input, the middle one your kernel (of size (3,1)) and the right one your output.

enter image description here

  • In your case, if your number of filters is equal to 1, you generate 128 kernels of size 3 which browse your series.
  • For each kernel, it processes the convolution operation : multiplies kernel values with series values and sums the 3 products.
  • Then the 128 results are summed up.
  • If you set your number of filter equal to 32, you operate this whole operation 32 times.
  • As a result the number of parameters for Conv1D (without biases) is : kernel_size * input_depth * number_filters = 3 * 128 * 32 = 12,288.
  • With biases, you add the number of filters to your previous result (12,288 + 32 = 12320).

So, the number of filters is the number of time this operation is processed. The outputs are then stacked and the process is repeated with the next convolutional layer.

Unfortunately, I didn't find any representation to well understand conv1D. This one, for Conv2D, explains the process which is the same for Conv1D. (With inuth_depth = 3 and the number_filters = output_depth = 2)

enter image description here

Related