Saturday, May 10, 2025
Alternative Way
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use
No Result
View All Result
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use
No Result
View All Result
Alternative Way
No Result
View All Result

How to calculate the number of parameters for a Convolutional and Dense layer in Keras?

Gordon James by Gordon James
October 3, 2021
in World Tech Code
0 0
0
Home World Tech Code

In this lesson, we define what a parameter is and how to compute the number of such parameters in each layer using a simple convolutional neural network.

Table of Contents

Toggle
  • Which parameters are examined?
  • Make the model
  • Preload
  • Parameters for input layer
  • Convolution layer parameter
    • Convolution layer 1
    • Convolution layer 2
  • dense layer
      • Related Tags:

Which parameters are examined?

Stochastic Gradient Descent (SGD) is used to learn and optimize weights and neural network biases during training. These weights and biases are really learnable parameters. In fact, all parameters in our model that are learned by the DMS are considered as learnable parameters. These parameters are also called trainable because they are optimized in the training process.

Make the model

Here we build a simple CNN model for image classification with an input layer, three hidden convolution layers and a dense output layer.

def create_model() :
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(kernel_size=(3,3), filters=32, padding= ‘same’, activation= ‘relu’, input_shape=[img_SIZE,img_SIZE,3],name=Conv1)

tf.keras.layers.Conv2D(kernel_size=(3,3), filters=64, padding=’same’, activation=’relu’,name=Conv2,use_bias=True),
tf.keras.layers.MaxPooling2D(pool_size=2,name=Max1),

tf.keras.layers.Conv2D(kernel_size=(3,3), filters=128, padding= ‘same’, activation= ‘relu’,name=Conv3),
tf.keras.layers.Conv2D(kernel_size=(1,1), filters=256, padding= ‘same’, activation= ‘relu’,name=Conv4),

tf.keras.layers.GlobalAveragePooling2D(name=GAP1),
tf.keras.layers.Dense(10,’relu’,name=Dense1),
tf.keras.layers.Dense(10,’softmax’,name=Output)]

model.compile(optimizer=tf.keras.optimizers.RMSprop(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
return model

model=create_model()
model.summary()

Our input layer consists of 32x32x3 image input, where 32×32 defines the width and height of the images and 3 defines the number of channels. The three channels indicate that our images are in the RGB color space, and these three channels represent the input features in this layer.

Our first convolutional layer consists of 32 3×3 filters. Our second convolutional layer consists of 64 3×3 filters. And our exit layer is a dense layer at 10 knots.

How to calculate the number of parameters for a Convolutional and Dense layer in Keras?

Preload

First, we need to understand whether or not the layer contains an offset for each layer. If that is the case, then we are only increasing the level of bias. The number of offsets is equal to the number of nodes (filters) in the layer. Moreover, we assume that our network is biased. This means that there are terms of bias in our hidden and output layers.

Parameters for input layer

The input layer has no parameters that can be examined, since the input layer consists only of input data and the output data of the layer is actually only treated as input data for the next layer.

Convolution layer parameter

The convolutional layer contains filters, also called nuclei. We must first determine how many filters are in the convolutional layer and how large they are. We must include these elements in our calculations.

The input for the convolution layer depends on the previous layer types. If it was a dense layer, it’s just the number of nodes in the previous dense layer.

If it was a convolutional layer, the input is the number of filters of the previous convolutional layer.

At the output of the convolution layer, the number of filters is multiplied by the filter size. With a dense layer, it was just the number of branches.

Let us calculate the number of studied parameters in the convolutional layer.

Convolution layer 1

tf.keras.layers.Conv2D(kernel_size=(3,3), filters=32, padding=’same’, activation=’relu’, input_shape=[IMG_SIZE,IMG_SIZE,3],name=Conv1)

We have three input signals from our input layer. The number of outputs is the number of filters multiplied by the filter size. So we have 32 filters, each of 3×3 size. So, 32*3*3 = 288. If we multiply our three inputs by 288 outputs, we get 864 weights.  What is the extent of the prejudice? There are 32 in total, since the number of offsets equals the number of filters. Thus, we obtain 896 common study parameters in this layer.

Convolution layer 2

tf.keras.layers.Conv2D(kernel_size=(3,3), filters=64, padding=’same’, activation=’relu’,name=Conv2,use_bias=True)

Let’s go to the next convolutional layer. How many inputs come from the previous level? We have 32, which is the number of filters from the previous layer. How many exits? Well, we have 64 filters, always 3×3. That makes 64*3*3 = 576 outputs. Multiplying our 32 inputs from the previous layer by 576 outputs, we have 18432 weights in this layer. Adding the offset conditions of the 64 filters gives 18496 digestible parameters at this level.

Then we perform the same calculation for the other layers of the network

dense layer

For a dense layer, this is indicated by the number of parameters examined:

Inputs * Outputs + Offsets

In general, the number of parameters to be examined in the computed layer is the number of inputs multiplied by the number of outputs plus the number of offsets.

This gives us the number of digestible parameters in a given layer. You can add up all the results to get the total number of parameters examined for the entire network.

Related Tags:

how to calculate parameters in keras model, convolution and fully connected layers contain parameters, pooling does not., how to count number of parameters in a model, calculate number of parameters in fully connected neural network, bidirectional rnn number of parameters, keras model summary, number of parameters in embedding layer, convolutional neural network

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
ShareTweet
Gordon James

Gordon James

Next Post
🥇 Download Free Pinterest Videos  Step by Step Guide ▷ 2021

🥇 Download Free Pinterest Videos  Step by Step Guide ▷ 2021

  • Trending
  • Comments
  • Latest
How To Get Free Internet On Android Without Service

How To Get Free Internet On Android Without Service

March 10, 2022
🥇 +4 Neo Geo Emulators for Android  List ▷ 2021

🥇 +4 Neo Geo Emulators for Android  List ▷ 2021

October 3, 2021

Fix: Notifications not working on Nova Launcher

October 3, 2021
How to Fix OpenVPN Connected but Not Changing IP Address

How to Fix OpenVPN Connected but Not Changing IP Address

October 3, 2021

Setting Up Directory Sync Between On-Premises Active Directory with Microsoft 365 Azure AD

0
🥇 DELETE ACCOUNT from PS4  ▷ Step by Step Guide ▷ 2020

🥇 DELETE ACCOUNT from PS4  ▷ Step by Step Guide ▷ 2020

0
🥇 PPTX File Extension  What is .Pptx and how to open them? ▷ 2020

🥇 PPTX File Extension  What is .Pptx and how to open them? ▷ 2020

0
🥇 Make a Crossword in Microsoft Word  Step by Step Guide ▷ 2020

🥇 Make a Crossword in Microsoft Word  Step by Step Guide ▷ 2020

0
What to Know About Car Shipping Services

What to Know About Car Shipping Services

May 7, 2025
CS2 Skins-Why Trade Them?

CS2 Skins-Why Trade Them?

May 7, 2025
Alternative Routes: Successfully Exiting Your Wyndham Timeshare Without The Stress

Alternative Routes: Successfully Exiting Your Wyndham Timeshare Without The Stress

May 6, 2025
The Ultimate Seiko Watch Gift Guide

The Ultimate Seiko Watch Gift Guide

May 1, 2025

There's always an alternative Way!
Find us at 4145 Zolynthian Street, Vylorthos, QP 78425
No Result
View All Result
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use

© 2022 - Alternative Way

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • Travel News

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.