# How to find the various matrix sizes in designing a CNN

I am trying to understand CNN especially the maths and working mechanism using Matlab as the coding language. I have few confusion regarding the concept and the associated programming and will be immensely grateful for an intuitive answer.

Below is the structure of my CNN for 5 classes. I could calculate only the output structure of the first Conv layer and stuck on determining the number of parameters i.e., number of neurons?

The output for the first convolution layer that I could calculate: In the first layer an input of size [50 50 2] is convolved with a set of M_1 5-dimensional filters applied over all the input channels. The first 2 D convolutional layer is composed of M_1 = 20 filters of size [5x5x 1] having the step size (stride) for traversing the input vertically and horizontally as 1 creating a feature map of size {(h-f_h+1) x (w - f_w +1)x 1x M_1} = (50-5+1)x(50-5+1)x20 = [46x46x 20] So we have 20 channels.

For a CNN layer with input of dimensions h * w * d, kernel size k * k and number of kernel filters as f, we have the number of parameters as k * k * d * f, if we ignore the biases. If use biases then the number of parameters becomes (k * k * d + 1) * f
For e.g., the 1st conv layer has 5 * 5 * 2 * 20 parameters if we are ignoring the biases. With bias, the number of parameters would become (5 * 5 * 2 + 1) * 20.
• (1) If your input is of d=2 then indeed the filter dimension should have been written [5 x 5 x 2]. In that case my calculation would also change. (My ans has been edited) (2) Yes, you need to add the number of parameters of all the layers to find the total number of parameters of the neural network. For the 2nd layer, the number of parameters becomes 5 * 5 * 20 * 40 as d=20 for the input to the 2nd conv layer whereas it there are 40 filters in the 2nd conv layer. – user1825567 Dec 27 '19 at 10:28
• Yes, you got it right that pooling and dropout does not affect the number of parameters. I got 40 from your code where you define your 2nd conv to have 2*numF number of filters where numF=20. I am glad you found my answer helpful. Please upvote my answer also :) – user1825567 Dec 28 '19 at 6:34