count number of parameters in CNN numerically in each and every layer

970 views
Skip to first unread message

Vinayakumar R

unread,
Jan 23, 2017, 10:57:46 PM1/23/17
to Keras-users
nb_filter = 64

filter_length = 3

pool_length=2

dense (128)
dropout(0.5)

Attaching the image


Could you please tell how to compute the numerically. Is there any formula or method to obtain the number of parameters of each and every layer

Klemen Grm

unread,
Jan 24, 2017, 4:06:10 AM1/24/17
to Keras-users
params_count = 0
for layer in model.layers:
    weights_list
= layer.get_weights()
   
for array in weights_list:
        params_count
+= np.prod(array.shape)

Vinayakumar R

unread,
Jan 24, 2017, 7:59:34 AM1/24/17
to Keras-users
No, i need a manual calculation. Already the attached fig shows the total parameters and each layer total parameters

Klemen Grm

unread,
Jan 24, 2017, 8:02:59 AM1/24/17
to Keras-users
For 2d convolutional layers: (no. of channels in input tensor) * (no. of channels in output tensor) * (kernel height * kernel width) + (no. of channels in output tensor)
For dense layers: (no. of input features) * (no. of output features + 1)

Vinayakumar R

unread,
Jan 24, 2017, 8:04:04 AM1/24/17
to Keras-users
According to your code the displayed parameters are 

192
256
164096
164224
164864
164869

But is there any equation to manually calculate the number of parameters in each layer

Vinayakumar R

unread,
Jan 24, 2017, 8:04:42 AM1/24/17
to Keras-users
I am using convolution1d

Klemen Grm

unread,
Jan 24, 2017, 8:08:32 AM1/24/17
to Keras-users
1d convolutions are equivalent to 2d convolutions with one of the kernel dimensions set to 1.

On Tuesday, 24 January 2017 14:04:42 UTC+1, Vinayakumar R wrote:
I am using convolution1d

Vinayakumar R

unread,
Jan 24, 2017, 8:16:34 AM1/24/17
to Keras-users
For your code my architecture is giving the below parameters. Could you please help how I am getting these many parameters numerically in each layer. I am new to this

192
256
164096
164224
164864
164869

with detailed description as 

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to
====================================================================================================
convolution1d_1 (Convolution1D)  (None, 41, 64)        256         convolution1d_input_1[0][0]
____________________________________________________________________________________________________
maxpooling1d_1 (MaxPooling1D)    (None, 20, 64)        0           convolution1d_1[0][0]
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 1280)          0           maxpooling1d_1[0][0]
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 128)           163968      flatten_1[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 128)           0           dense_1[0][0]
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 5)             645         dropout_1[0][0]
====================================================================================================
Total params: 164,869
Trainable params: 164,869
Non-trainable params: 0

Klemen Grm

unread,
Jan 24, 2017, 8:22:39 AM1/24/17
to Keras-users
What's the shape of the input data to your network? Also, if possible post the code that defines your model.

Vinayakumar R

unread,
Jan 24, 2017, 8:24:23 AM1/24/17
to Keras-users
cnn = Sequential()
cnn.add(Convolution1D(64, 3, border_mode="same",activation="relu",input_shape=(41, 1)))
cnn.add(MaxPooling1D(pool_length=(2)))
cnn.add(Flatten())
cnn.add(Dense(128, activation="relu"))
cnn.add(Dropout(0.5))
cnn.add(Dense(5, activation="softmax"))
print(cnn.summary())
print(cnn.count_params())

Klemen Grm

unread,
Jan 24, 2017, 8:33:56 AM1/24/17
to Keras-users
For the layer convolution2d_1:
input_channels = 1, output_channels = 64, filter_length = 3,
num_params = (input_channels * output_channels * filter_length) + (output_channels) = 256

pooling and flatten layers don't have any free parameters.

for the layer dense_1, the previous layer's output shape is 1344, and its output shape is 128, therefore, it has 1344 * 128 + 128 = 172160 parameters

similarly, the layer dense_2 has 128 * 5 + 5 = 654 parameters.

Vinayakumar R

unread,
Jan 24, 2017, 8:37:27 AM1/24/17
to Keras-users
Thank you. This solves the problem

Vinayakumar R

unread,
Jan 24, 2017, 8:42:42 AM1/24/17
to Keras-users
The output from your code, I got 
192
256
164096
164224
164864
164869

From your calculated values 192 and 256 are right. But others are wrong
Reply all
Reply to author
Forward
0 new messages