Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
How to create Models in Keras?
Next article icon

How to create Models in Keras?

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Keras is an open-source API used for solving a variety of modern machine learning and deep learning problems. It enables the user to focus more on the logical aspect of deep learning rather than the brute coding aspects. Keras is an extremely powerful API providing remarkable scalability, flexibility, and cognitive ease by reducing the user's workload. It is written in Python and uses TensorFlow or Theano as its backend. 

Models in Keras

A typical model in Keras is an aggregate of multiple training and inferential layers. There are two broad methods of creating models using Keras.

The Functional API

The Functional API handles non-linear models with diverse functionalities. These models are extremely scalable and flexible. You can specify your neural network's forward pass starting right from the input and all the way down to the output to create personalized models. It provides a resilient architecture wherein pairs of layers can connect to multiple layers in any fashion. The functional API can be said to be a way to build graphs of layers and ad-hoc acyclic network graphs. This helps users tailor complex networks like the Siamese network with extreme ease. Creating a model with the functional API is a multi-step process that is defined here.

1.) Define Inputs

The first step in creating a Keras model using the functional API is defining an input layer. The input layer accepts the shape argument which is actually a tuple. This is used to define the dimensionality of the input. 

Python
# defining Input from keras.layers import Input visible = Input(shape=(10,)) 

The last dimension of the aforementioned one-dimensional input is always left hanging so as to compensate for the shape of the mini-batch size used when splitting the data for training, testing, and validation. Hence, the vacancy after the 'comma'.

2.) Connecting Layers

After we have created the input layer, we shall now create a dense, hidden layer. This hidden layer shall be connected to our input layer and receive input from it. 

Python
# connecting layers from keras.layers import Input from keras.layers import Dense visible = Input(shape=(10,)) # now connecting hidden layer to visible input layer hidden = Dense(2)(visible) 

This is how the hidden layer is connected to the visible input layer. The Dense layer is used for passing the outputs from one layer of neurons to a separate layer of neurons as inputs. 

Note that in this case inputs from only one layer are passed into the output layer due to personal demands. However, functional models allow for multiple inputs to be fed into the neural network to reap multiple outputs also. Functional models are extremely popular due to the ease with which connections could be established. You can now add as many layers as you want. 

3.)  Create your Model 

The desired model can now be created in the easiest way by using the Model class. Only the input and output layers need to be defined. 

Python
#import all required modules from keras.models import Model from keras.layers import Input from keras.layers import Dense visible = Input(shape=(10,)) hidden = Dense(2)(visible) model = Model(inputs=visible, outputs=hidden) 

The model is now created with the input being the visible layer and the output being the hidden layer. This is how the functional API helps users create neural network models without any hassle. 

The Sequential API

The Sequential API is a slightly less refined API for modelling simpler neural networks. Each layer in the network accepts only one input and passes on a single output.  It is important to note that sequential models don't work when the model requires multiple inputs or outputs or when layers need to be shared. A sequential model can only be used in a network that has a linear topology. 

Create a Sequential Model

Let us create a sequential model with three layers. After all the imports have been made, we start by creating the layers. 

Python
model = keras.Sequential(     [         layers.Dense(2, activation="relu"),         layers.Dense(3, activation="relu"),         layers.Dense(4),     ] ) 

This is how we create a model with three layers. The activation function used in this case is 'ReLu', or rectified linear activation unit. The integral arguments passed into Dense indicate the number of neurons in each layer. 

Note that the add() method can also be used to add layers into a model. 

Python
model = keras.Sequential() model.add(layers.Dense(2, activation="relu")) model.add(layers.Dense(3, activation="relu")) model.add(layers.Dense(4)) 

Next Article
How to create Models in Keras?

S

ssanya0904
Improve
Article Tags :
  • Machine Learning
  • AI-ML-DS
  • Neural Network
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    How to Create a Custom Loss Function in Keras
    Creating a custom loss function in Keras is crucial for optimizing deep learning models. The article aims to learn how to create a custom loss function. Need to create Custom Loss Functions Loss function is considered as a fundamental component of deep learning as it is helpful in error minimization
    3 min read
    How to Save the Final Model Using Keras
    Saving your final model in Keras using the HDF5 format is an effective way to capture all aspects of the model for later use, whether for further training, evaluation, or deployment. It’s a critical step for preserving your work and making your models reusable and shareable. Always ensure that the e
    2 min read
    ML | Word Encryption using Keras
    Machine Learning is something which is one of the most in-demand fields of today's tech market. It is very interesting to learn how to teach your system to do some specific things by its own. Today we will take our first small step in discovering machine learning with the help of libraries like Tens
    5 min read
    How to Log Keras Loss Output to a File
    Monitoring the training process of a machine learning model is crucial for understanding its performance over time. When working with Keras, one of the most effective ways to track this is by logging the loss output to a file. This not only allows you to keep a record of the training progress but al
    5 min read
    Custom Loss Function in R Keras
    In deep learning, loss functions guides the training process by quantifying how far the predicted values are from the actual target values. While Keras provides several standard loss functions like mean_squared_error or categorical_crossentropy, sometimes the problem you're working on requires a cus
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences