# How are the numbers of a filter decided in a Convolutional neural network?

I know how a filter in a Convolutional Neural Network "scans" the input image and multiplies the values of the kernel with the corresponding receptive field in the input image and adds it all up to get a new pixel in the output activation map. But I unsure how the numbers in a filter is decided.

Is the kernel a patch from the image that is chosen? Like a 5x5 patch of the image that the network must decide to be good to be used as a filter? Or are they random numbers that backpropagation will soon change to fit best with the data? And would these numbers in the filter be considered as the weights of the network?

Or are they random numbers that backpropagation will soon change to fit best with the data?

In short, yes.

The numbers in a filter (yes, they are called the weights) are usually randomly initialized with a normal distribution centered at 0. From there, the weights are updated with back propagation, typically using stochastic gradient descent or some variant of it.

In other words, the network tries to predict an output using the current weights, and the weights are updated according to the derivative of the weight with respect to the loss/cost function (which measures how incorrect the network's prediction was).

Filter design is an art, in part. You come up with a filter to amplify certain features in your vectors or matrices.

For instance, say I'm looking for local peaks in the vector, i.e. a value that is greater than its preceding and following neighbors. On the next figure the local peaks are 5,7 and 9 (blue line).

In fact, the blue line was generated randomly with MATLAB:

>> u=rand(1,10)

u =

0.8416    0.7342    0.5710    0.1769    0.9574    0.2653    0.9246    0.2238    0.3736    0.0875


I could try a filter [-1 1 -1], the idea's that the value in the middle is larger than the values before and after. So, I'll be suppressing the edges and amplifying the middle. I'll be applying this filter as a moving window starting from the start to the finish of the vector:

>> v=[-1 1 -1]

v =

-1     1    -1
>> y = conv(u,v,'valid')

y =

-0.1767   -0.4347   -0.3599   -0.2702   -0.5382    0.2119   -0.9628    0.1713


I'll plotted the convolution result y in red on the chart above. As you see points #5,7 and 9 are the greatest in magnitude: my filter found the peaks in the sequence. If next you apply the max pool, you get the point #5. That would be the most likely location of the feature in the vector if there is one present at all.

Did I really explain how to create filters? No. That would be too much for an answer here, but hopefully you got an idea of the intuition behind them

• So is the filter [-1,1,-1] like the numbers that the backpropagation must optimize to get. That is, if there was 3 random numbers in that place, the backpropgagation algorithm would try to optimize to get the numbers as [-1,1,-1] ? Nov 14, 2017 at 11:57
• I suppose you could optimize the filter too, but in a common CNN design you don't do it. You design the filter separately in advance. Nov 14, 2017 at 14:13
• CNNs are all about optimizing the filter, I'm sorry but I think you have it exactly incorrect. Sep 11, 2018 at 15:43