How to compute number of weights of CNN?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Counting the weights in a convolutional neural network means counting its trainable parameters. That number matters because it affects memory usage, training time, and how easily the model can overfit. The core idea is simple: each trainable layer has a formula, and the total parameter count is the sum across layers.
Count Parameters in Convolution Layers
A standard 2D convolution layer learns one small kernel per output channel. Each kernel spans the full input depth, so the parameter count depends on kernel height, kernel width, input channels, and output channels.
For a regular convolution, the formula is:
kernel_height * kernel_width * input_channels * output_channels + output_channels
The final output_channels term is the bias count if biases are enabled.
For example, suppose an RGB image enters a layer with 32 filters of size 3 x 3:
3 * 3 * 3 * 32 + 32 = 896
That is why convolution parameter counts do not depend on the image width or height. Spatial size affects activations and compute cost, but not the number of learned weights.
Count Parameters in Dense and Other Trainable Layers
A dense layer connects every input value to every output unit. Its formula is:
input_units * output_units + output_units
If a dense layer receives 512 values and produces 128 outputs, the count is:
512 * 128 + 128 = 65664
Pooling layers such as max pooling or average pooling have no trainable parameters. They change tensor size, but they do not learn weights.
Batch normalization is trainable in a different way. In common deep learning libraries, it usually learns two values per channel: scale and shift. Running mean and variance are stored too, but those are normally not trainable.
Depthwise convolutions and grouped convolutions use different formulas, so they often have far fewer parameters than a regular convolution. If you are working with MobileNet-style blocks, do not apply the regular convolution formula blindly.
Worked Example
Consider this small CNN:
- Input image:
64 x 64 x 3 - Conv layer: 16 filters,
3 x 3, bias enabled - Max pooling
- Conv layer: 32 filters,
3 x 3, bias enabled - Flatten
- Dense layer: 128 units
- Output layer: 10 units
Now count each trainable layer.
First convolution:
3 * 3 * 3 * 16 + 16 = 448
Second convolution takes 16 input channels from the previous layer:
3 * 3 * 16 * 32 + 32 = 4640
Assume the tensor shape after the second pooling layer is 16 x 16 x 32. Flattening produces 8192 input values for the dense layer.
Dense layer:
8192 * 128 + 128 = 1048704
Output layer:
128 * 10 + 10 = 1290
Total trainable parameters:
448 + 4640 + 1048704 + 1290 = 1055082
The dense layer dominates the total. That is a common pattern in older CNNs and a useful signal when trying to shrink a model.
Verify the Count in Code
If you are building a model in Keras, you can confirm the math directly.
This is the practical way to validate your manual count. Manual calculation helps you reason about architecture choices, while model.summary() catches mistakes in tensor shapes.
Why Parameter Count Matters
A larger parameter count increases model capacity, but it also increases training cost and memory usage. More parameters are not automatically better. If two models solve the same task, the one with fewer parameters is often easier to train and deploy.
Parameter count is also useful when comparing layers. Replacing a huge dense layer with global average pooling can dramatically reduce the model size without changing the convolution stack very much.
Common Pitfalls
- Counting activation values instead of trainable weights. Feature map size is not parameter count.
- Forgetting the bias term when the layer uses bias.
- Using image width and height in the convolution parameter formula.
- Forgetting that the next convolution uses the previous layer's output channels as its input depth.
- Applying the regular convolution formula to depthwise or grouped convolutions.
- Missing batch normalization parameters when estimating trainable size.
Summary
- Convolution parameters depend on kernel size, input channels, and output channels.
- Dense layers often contribute most of the weights in small CNNs.
- Pooling layers do not add trainable parameters.
- Bias terms must be counted when enabled.
- Manual calculation is useful, but checking with
model.summary()is the safest verification step.
Related reading
- How to compute the cosine_similarity in pytorch for all rows in a matrix with respect to all rows in another matrix
- How to compute the second derivatives diagonal of the Hessian in TensorFlow 2.0
- How to concatenate two layers in keras?
- How to concatenate two tensors horizontally in TensorFlow?
- How to compute precision, recall, accuracy and f1-score for the multiclass case with scikit learn?
- How to construct a network with two inputs in PyTorch
- How to connect LSTM layers in Keras, RepeatVector or return_sequenceTrue?
- How to continue training model using ModelCheckpoint of Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.