Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras conv1d layer parameters: filters and kernel_size

I am very confused by these two parameters in the conv1d layer from keras: https://keras.io/layers/convolutional/#conv1d

the documentation says:

filters: Integer, the dimensionality of the output space (i.e. the number output of filters in the convolution). kernel_size: An integer or tuple/list of a single integer, specifying the length of the 1D convolution window. 

But that does not seem to relate to the standard terminologies I see on many tutorials such as https://adeshpande3.github.io/adeshpande3.github.io/A-Beginner's-Guide-To-Understanding-Convolutional-Neural-Networks/ and https://machinelearningmastery.com/sequence-classification-lstm-recurrent-neural-networks-python-keras/

Using the second tutorial link which uses Keras, I'd imagine that in fact 'kernel_size' is relevant to the conventional 'filter' concept which defines the sliding window on the input feature space. But what about the 'filter' parameter in conv1d? What does it do?

For example, in the following code snippet:

model.add(embedding_layer) model.add(Dropout(0.2)) model.add(Conv1D(filters=100, kernel_size=4, padding='same', activation='relu')) 

suppose the embedding layer outputs a matrix of dimension 50 (rows, each row is a word in a sentence) x 300 (columns, the word vector dimension), how does the conv1d layer transforms that matrix?

Many thanks

like image 688
Ziqi Avatar asked Sep 30 '17 14:09

Ziqi


People also ask

What are filters in Conv1D?

filters: Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). kernel_size: An integer or tuple/list of a single integer, specifying the length of the 1D convolution window.

What is the difference between Conv1D and conv2d?

conv1d is used when you slide your convolution kernels along 1 dimensions (i.e. you reuse the same weights, sliding them along 1 dimensions), whereas tf. layers. conv2d is used when you slide your convolution kernels along 2 dimensions (i.e. you reuse the same weights, sliding them along 2 dimensions).

What is Kernel_size?

kernel_size: An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions.

What is stride in Conv1D?

From Conv1D, the default stride length is 1. Unless you have a concrete justification for another length, a stride length of 1 is usually appropriate.


Video Answer


1 Answers

You're right to say that kernel_size defines the size of the sliding window.

The filters parameters is just how many different windows you will have. (All of them with the same length, which is kernel_size). How many different results or channels you want to produce.

When you use filters=100 and kernel_size=4, you are creating 100 different filters, each of them with length 4. The result will bring 100 different convolutions.

Also, each filter has enough parameters to consider all input channels.


The Conv1D layer expects these dimensions:

(batchSize, length, channels) 

I suppose the best way to use it is to have the number of words in the length dimension (as if the words in order formed a sentence), and the channels be the output dimension of the embedding (numbers that define one word).

So:

batchSize = number of sentences     length = number of words in each sentence    channels = dimension of the embedding's output.     

The convolutional layer will pass 100 different filters, each filter will slide along the length dimension (word by word, in groups of 4), considering all the channels that define the word.

The outputs are shaped as:

(number of sentences, 50 words, 100 output dimension or filters)    

The filters are shaped as:

(4 = length, 300 = word vector dimension, 100 output dimension of the convolution)   
like image 75
Daniel Möller Avatar answered Oct 03 '22 21:10

Daniel Möller