Explore how the size and number of filters, input channels, and biases dictate the total number of trainable parameters in a Convolutional layer.
A Single Filter (3D)
A filter is not just a 2D square! It extends through all input channels.
KxK
C_in
Parameters in ONE Filter:
3 × 3 × 3 = 27 weights
+ 1 bias term
Understanding Parameter Calculation in CNNs
How to accurately count the trainable weights and biases inside a Convolutional Layer.
The 3D Nature of Filters
A common misconception is that a filter (kernel) in a CNN is just a 2D grid, like a 3x3 square. While it moves across the image in 2D (up/down, left/right), a filter is actually a 3D block.
A filter must stretch across the entire depth of its input volume. If the input image is an RGB image, it has 3 channels (Red, Green, Blue). Therefore, the filter must also have a depth of 3. So a "3x3" filter on an RGB image is actually a 3x3x3 block of weights.
Step-by-Step Calculation
Let's break down the formula: ((K × K × C_in) + 1) × F
Weights per filter: Multiply the width and height of the kernel (K × K) by the number of input channels (C_in). Example: A 3x3 filter on an RGB image has 3 × 3 × 3 = 27 weights.
Add the Bias: Each filter usually has one bias term that is added to the output sum. Example: 27 weights + 1 bias = 28 parameters per filter.
Multiply by the number of filters: A convolutional layer applies multiple different filters (F) to extract different features (e.g., one filter for vertical edges, one for horizontal). Example: If we use 32 filters, the total parameters are 28 × 32 = 896.
Why does output size not matter?
Notice that the size of the input image (e.g., 1000x1000 vs 28x28) and the resulting output feature map are not in the formula. Because of Parameter Sharing, the same filter is slid across the entire image. The number of parameters depends only on the filter configuration, not the image dimensions!
Key Variables
K: Spatial dimensions of the kernel (e.g., 3, 5).
C_in: Number of channels in the incoming data.
F (or C_out): The number of filters used in the layer (determines output depth).
Bias: +1 per filter (unless explicitly removed).
Cheat sheet
Calculating Parameters in CNN
Explore how the size and number of filters, input channels, and biases dictate the total number of trainable parameters in a Convolutional layer.