How to calculate the total number of parameters in a convolutional neural network?

允我心安 提交于 2019-11-28 06:08:30

问题


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.



来源:https://stackoverflow.com/questions/50158474/how-to-calculate-the-total-number-of-parameters-in-a-convolutional-neural-networ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!