![]() |
Calculating the number of parameters in Convolutional Neural Networks (CNNs) is important for understanding the model complexity, computational requirements, and potential overfitting. Parameters in CNNs are primarily the weights and biases learned during training. This article will walk you through calculating these parameters in various layers of a CNN. Table of Content
Steps of Calculate the number of Parameter in CNNTo calculate the total number of parameters in a 2D convolutional neural network, sum the parameters from all layers, including convolutional, fully connected, and batch normalization layers, while excluding pooling layers as they contribute zero parameters. 1. Convolutional LayerFor a convolutional layer, the number of parameters is determined by the size of the filters (kernels), the number of filters, and the number of input channels. We can calculate the parameter in the following manner: Parameters=[Tex](k_w \times k_h \times C_{in} +1)\times C_{out}[/Tex] Where:
The “+ 1” accounts for the bias term for each filter. Let’s consider an example, where the convolutional layer with 32 filters of size 3×3 is given, and has an input with 3 channels: [Tex] Parameters=(3×3×3+1)×32 =(27+1)×32=28×32 =896[/Tex] 2. Fully Connected (Dense) LayersFor a fully connected layer, the number of parameters is given by the number of input units times the number of output units, plus one bias term for each output unit. [Tex]Parameters=(\text{input units} × \text{output units})+ \text{output units}[/Tex] Let’s take an example, where the fully connected layer has 128 input units and 64 output units the computed parameters are: [Tex]Parameters=(128×64)+64=8192+64=8256 [/Tex] 3. Batch Normalization LayersFor batch normalization layers, each feature channel has two parameters (gamma and beta). [Tex]Parameters=2×\text{num features}[/Tex] Let’s suppose that batch normalization layer is applied to an output of 64 channels then parameters can be computed as: [Tex]Parameters = 2 \times 64 =128 [/Tex] 4. Pooling LayersPooling layers (e.g., max pooling, average pooling) do not have learnable parameters, so they contribute 0 to the parameter count. 5. Combining All LayersTo find the total number of parameters in the CNN, sum the parameters from all the layers calculated above. Example: Calculating the Number of Parameter in CNNConsider a simple CNN with the following layers:
Conv Layer 1: [Tex]\text{Parameters}=(3×3×3+1)×16=(27+1)×16=448[/Tex] Batch Norm 1: [Tex]\text{Parameters}=2×16=32[/Tex] Conv Layer 2: [Tex]\text{Parameters}=(3×3×16+1)×32=(144+1)×32=4640[/Tex] Batch Norm 2: [Tex]\text{Parameters}=2×32=64[/Tex] Fully Connected Layer: [Tex]\text{Parameters}=(128×64)+64=8256[/Tex] Total Parameters: [Tex]\text{Total Parameters} = 448 + 32 + 4640 +64 + 8256 = 13440 [/Tex] So, the total number of parameters in this simple CNN example is 13,440. Parameter Calculation for 3D ConvolutionsFor a 3D convolutional layer, the number of parameters depends on the size of the filters (kernels), the number of filters, and the number of input channels. [Tex]\text{Parameters} = (k_d \times k_h \times k_w \times C_{in} + 1) \times C_{out}[/Tex] Where:
The “+ 1” accounts for the bias term for each filter. Let’s consider an example for 3D Convolution Neural Network where we will compute the parameters for:
Conv Layer 1: [Tex]\text{Parameters}=(3×3×3×3+1)×16=82×16=1312[/Tex] Batch Norm 1: [Tex]\text{Parameters}=2×16=32[/Tex] Conv Layer 2: [Tex]\text{Parameters}=(3×3×3×16+1)×32=(432+1)×32=433×32=13856 [/Tex] Batch Norm 2: [Tex]\text{Parameters}=2×32=64[/Tex] Fully Connected Layer: [Tex]\text{Parameters}=(128×64)+64=8256[/Tex] Total Parameters: [Tex]\text{Total Parameters} = 1312+32+13856+64+8256=23520[/Tex] So, the total number of parameters in this simple 3D CNN example is 23,520. Factors Affecting Parameter Calculation
ConclusionCalculating the number of parameters in CNNs is fundamental for designing and understanding neural network models. By systematically calculating the parameters for each layer, one can evaluate the complexity and feasibility of the model for a given application. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |