# How to calculate the number of weights for a CNN?

Can somebody help me with the formula needed to calculate the number of weights for a CNN, using the following sample question as the basis for it?

Suppose we have a convolutional neural network with a 5x5x1 input volume, followed by one convolutional layer with 5 filters that have a 2x2x1 receptive field, followed by one fully connected output layer with 5 neurons. How many weights does the network have in total?

Any help appreciated because I keep getting the wrong answer. I think the first part is:

5 * (5 * 5 * 1) + 5

And this gives 130, it might be completely wrong anyway I am doing:

filters * (5 x 5 x 1) + filters

For the second part (2 * 2 * 1) with five filters I get:

filters * ( 2 * 2 * 1) + filters = 25

So now I have a total value of 155 and after this I am unsure how to factor in the output neurons and arrive at the answer (which I know is 430).

## 1 Answer

Each receptive field of a filter has a weight. Furthermore, the whole filter has a single bias. This gives for a single filter: 2*2*1+1 = 5 weights per filter. 5 filters * 5 weights = 25 weights for all filters.

The conv layer produces shape (4, 4, 5) if we assume the stride is 1.

The fully connected output layer (dense layer) has 5 neurons. Each of them is connected to the output of the conv layer. So it's (4*4*5) * 5 neurons = 400 connections. Each of these connections has a weight. Each neuron in the dense layer also has a bias, so there are 5 more weights.

To sum up: 25 parameters for conv layer, 405 parameters for dense layer. 430 in total.