# Number of Parameters in a CNN model

Can someone confirm if the following computation shown in the figure is wrong?

The number of trainable parameters in a layer of the LeNet-5 Architecture:

Image Source - Medium Article

This same value is given by someone on Kaggle.

I think the corrected value of the trainable parameters for this specific layer - all weights and biases in all kernels - should be:
(5 * 5 * 6 * 16) + 16 = 2416.

Please let me know which one is correct? 2416 or 1516.

1 Like

The highlighed layer has 5x5 filters and the input has 6 channels. So, the number of trainable parameters for each filter is 5x5x6 + 1 (the 1 is for bias). This makes it 151 parameters per filter. Since we have 16 filters, the total number of trainable parameters is 151*16 = 2416

Hereβs lenet in code:

``````import tensorflow as tf

lenet = tf.keras.Sequential([
tf.keras.layers.Input(shape=(32,32,1)),
tf.keras.layers.Conv2D(filters=6, kernel_size=5, strides=1, activation='tanh'),
tf.keras.layers.AveragePooling2D(strides=2, pool_size=2),
tf.keras.layers.Activation('tanh'),
tf.keras.layers.Conv2D(filters=16, kernel_size=5, strides=1, activation='tanh'),
tf.keras.layers.AveragePooling2D(strides=2, pool_size=2),
tf.keras.layers.Activation('tanh'),
tf.keras.layers.Conv2D(filters=120, kernel_size=5, strides=1, activation='tanh'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=84, activation='tanh'),
tf.keras.layers.Dense(units=10, activation='softmax'),
])
lenet.summary()
``````

Output

``````Model: "sequential"
βββββββββββββββββββββββββββββββββββ³βββββββββββββββββββββββββ³ββββββββββββββββ
β Layer (type)                    β Output Shape           β       Param # β
β conv2d (Conv2D)                 β (None, 28, 28, 6)      β           156 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β average_pooling2d               β (None, 14, 14, 6)      β             0 β
β (AveragePooling2D)              β                        β               β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β activation (Activation)         β (None, 14, 14, 6)      β             0 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β conv2d_1 (Conv2D)               β (None, 10, 10, 16)     β         2,416 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β average_pooling2d_1             β (None, 5, 5, 16)       β             0 β
β (AveragePooling2D)              β                        β               β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β activation_1 (Activation)       β (None, 5, 5, 16)       β             0 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β conv2d_2 (Conv2D)               β (None, 1, 1, 120)      β        48,120 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β flatten (Flatten)               β (None, 120)            β             0 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β dense (Dense)                   β (None, 84)             β        10,164 β
βββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββ€
β dense_1 (Dense)                 β (None, 10)             β           850 β
βββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββββββββ

Total params: 61,706 (241.04 KB)
Trainable params: 61,706 (241.04 KB)
Non-trainable params: 0 (0.00 B)
``````
3 Likes