1

How to compute the number of weight of CNN in a greyscale image.

here is the code:

Define input image size

input_shape = (32, 32, 1)
flat_input_size = input_shape[0]*input_shape[1]*input_shape[2]
num_classes = 4

Simple deep network

dnn_model = Sequential()
dnn_model.add(Dense(input_dim=flat_input_size, units=1000))
dnn_model.add(Activation("relu"))
dnn_model.add(Dense(units=512))
dnn_model.add(Activation("relu"))
dnn_model.add(Dense(units=256))
dnn_model.add(Activation("relu"))
dnn_model.add(Dense(units=num_classes))
dnn_model.add(Activation("softmax"))

The picture below is the network plot enter image description here

here is the result enter image description here

count anyone help me to compute the number of params. how to get 1025000, 512512, 131328, 1028, show some details

HungryBird
  • 1,077
  • 1
  • 11
  • 28

1 Answers1

1

For a dense layer with bias (the bias is the +1) the calculation is as follows:

(input_neurons + 1) * output_neurons

In your case for the first layer this is:

(32 * 32 + 1) * 1000 = 1025000

and for the second one:

(1000 + 1) * 512 = 512512

and so on and so forth.

Edited answer to reflect additional question in comments:

For convolutional layers, as asked in the comments, you try to learn a filter kernel for each input channel for each output channel with an additional bias. Therefore the amount of parameters in there are:

kernel_width * kernel_height * input_channels * output_channels + output_channels = num_parameters

For your example, where we go from a feature map of size (None, 16, 16, 32) to (None, 14, 14, 64) with a (3, 3) kernel we get the following calculation:

3 * 3 * 32 * 64 + 64 = 18496

That is actually the important thing in CNNs, that the number of parameters is independent of the image size.

Thomas Pinetz
  • 6,948
  • 2
  • 27
  • 46