How to calculate the total number of parameters in

2020-02-13 05:22发布

问题:

how to calculate the total number of params in a CNN network

here is the code:

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

cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), padding='same',
                 input_shape=input_shape))
cnn_model.add(Activation('relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Conv2D(64, (3, 3)))
cnn_model.add(Activation('relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Dropout(0.25))
cnn_model.add(Conv2D(128, (3, 3), padding='same'))
cnn_model.add(Activation('relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
cnn_model.add(Dropout(0.25))
cnn_model.add(Flatten())
cnn_model.add(Dense(512))
cnn_model.add(Activation('relu'))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(num_classes))
cnn_model.add(Activation('softmax'))

here is the result

How to get 320, 18496, 73856, 590336, 2052, could anyone explain it?

回答1:

You can use this general formula:

channels_in * kernel_width * kernel_height * channels_out + num_channels

So the first example:

1 * 3 * 3 * 32 + 32 = 320

And the second:

32 * 3 * 3 * 64 + 64 = 18,496

The addition of the number of channels is the bias terms.