CNN
weights calculation
deep learning
neural networks
machine learning

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.

Practice ML system design

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:

  1. Input image: 64 x 64 x 3
  2. Conv layer: 16 filters, 3 x 3, bias enabled
  3. Max pooling
  4. Conv layer: 32 filters, 3 x 3, bias enabled
  5. Flatten
  6. Dense layer: 128 units
  7. 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.

python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3
4model = models.Sequential([
5    layers.Input(shape=(64, 64, 3)),
6    layers.Conv2D(16, (3, 3), activation="relu"),
7    layers.MaxPooling2D(pool_size=(2, 2)),
8    layers.Conv2D(32, (3, 3), activation="relu"),
9    layers.MaxPooling2D(pool_size=(2, 2)),
10    layers.Flatten(),
11    layers.Dense(128, activation="relu"),
12    layers.Dense(10, activation="softmax"),
13])
14
15model.summary()
16print("Total trainable params:", model.count_params())

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.