RnewGrab Deal : Flat 20% off on live classes + 2 free self-paced courses! - SCHEDULE CALL Rnew

- Data Science Blogs -

How to work with Deep Learning on Keras?


Deep learning is an artificial intelligence work that mimics the functions of the human brain in preparing information and making designs for use in dynamic or can say decision making. Deep learning is a subpart of machine learning in artificial intelligence that has systems fit for taking in unaided from information that is unstructured or unlabeled. 

Working of Deep Learning

Deep Learning has progressed with the digital era, which has achieved a blast of information in all structures and from each region of the world. A large amount of data simply known as Big Data, are all drawn from sources like businesses, social platforms, media, search engines and many others. This huge measure of information is promptly available and can be shared through applications like distributed computing.  

An enormous amount of unstructured data will take a lot of time to get analyzed so for that type of data we need to increase potential from the company by adapting AI for automated support so that we can increase the potential of the data  

Keras consists of high-level neural networks API which is capable of running on Theano, Tensorflow and CNTK. It empowers quick experimentation through an elevated level, easy to use, measured and extensible API. Keras can run on CPU and GPU both.

Keras gives seven distinctive datasets, which can be stacked in utilizing Keras legitimately. These incorporate image datasets. The most straightforward method for making a model in Keras is by utilizing the successive API, which lets you stack one layer after the other. The issue with the consecutive API is that it doesn't permit models to have different sources of info or yields, which are required for certain issues.

Data Science Training - Using R and Python

  • Detailed Coverage
  • Best-in-class Content
  • Prepared by Industry leaders
  • Latest Technology Covered


Deep learning using Keras is performed by the following series of steps, which are:

  • Installation of Keras

For Keras installation, we can use either pip or conda command.

Command: pip install Keras

  • Understanding and splitting the dataset into training and testing set.

For this article, we are using a dataset that contains 80000 grayscale images with different classes.

Keras splits the dataset into two parts training and testing dataset. The training dataset consists of 70000 instances whereas the testing dataset contains 10000 instances.  

                  Screenshot (1).png

Since it is an image dataset; we will process this dataset CNN (Convolutional Neural Network). Reshape method is used for the transformation of the dataset into four dimensions.                   


x_train = x_train.astype('float32')

x_test = x_test.astype('float32')

x_train /= 255

x_test /= 255

x_train = X_train.reshape(X_train.shape[0], 28, 28, 1)

x_test = X_test.reshape(X_test.shape[0], 28, 28, 1)

Now, labels are transformed using the to_categorical method of Keras.               


from Keras.utils import to_categorical

y_train = to_categorical(y_train, 10)

y_test = to_categorical(y_test, 10)

  • Design a model

There are two approaches for designing a deep learning model, one with sequential API and another with Functional API.

For this article, Functional API is used; It is simple, flexible and easily understandable. Functional API eases the building of complex network structures.


from Keras.models import Model

from Keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout, Input

inputs = Input(shape=x_train.shape[1:])

x = Conv2D(filters=32, kernel_size=(5,5), activation='relu')(inputs)

x = Conv2D(filters=32, kernel_size=(5,5), activation='relu')(x)

x = MaxPool2D(pool_size=(2, 2))(x)

x = Dropout(rate=0.25)(x)

x = Conv2D(filters=64, kernel_size=(3,3), activation='relu')(x)

x = Conv2D(filters=64, kernel_size=(3,3), activation='relu')(x)

x = MaxPool2D(pool_size=(2, 2))(x)

x = Dropout(rate=0.25)(x)

x = Flatten()(x)

x = Dense(256, activation='relu')(x)

x = Dropout(rate=0.5)(x)

predictions = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs=predictions)

Data Science Training - Using R and Python

  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available

In Functional API, the previous layer acts as an input for the current layer which makes complex computation a bit easy compared to sequential API.

  • Compilation of model

Model is compiled before training because it gives an idea of how efficiently the model is working which is defined by loss function and optimizer is used to minimize the loss by updating weights using gradients.                  







Since, image dataset is used to illustrate the concept of deep learning. Now, image data is augmented to create more data from the existing data. Image augmentation involves rotation of images, noise addition etc.

  • Image Augmentation

Since image dataset is used to illustrate the concept of deep learning. Now, image data is augmented to create more data from the existing data. Image augmentation involves rotation of images, noise addition etc.

Image augmentation makes the model more robust and makes it efficient when there is a huge amount of data. ImageDataGenerator method from Keras is used for image augmentation. It will generate new images of rotated or edited images.


from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(






  • Fitting the designed model and Visualizing the training and test dataset.

After the compilation of the model, the dataset is ready to train. For training of dataset, fit_generator was used instead of the fit method from Keras because we are implementing DataGenerator. A number of epochs, X any Y dimension were passed to generator.

To monitor the loss and accuracy, a validation set is also passed to the generator.


epochs = 3

batch_size = 32

history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs,

validation_data=(x_test, y_test), steps_per_epoch=x_train.shape[0]//batch_size)

The output appears as:

Screenshot (8).png

Now, for visualization of training and testing dataset and mapping its accuracy is performed as follows:

Matplotlib.pyplot is used for visualization of training, testing accuracy and loss for each epoch to make a better understanding of the model. 


import matplotlib.pyplot as plt

plt.plot(history.history['acc'], label='training accuracy')

plt.plot(history.history['val_acc'], label='testing accuracy')





The output appears as:


For understanding the loss pattern during training and testing are as follows:


plt.plot(history.history['loss'], label='training loss')

plt.plot(history.history['val_loss'], label='testing loss')





The output appears as:


From the above graph, we can easily observe that the model is not overfitting. In that case, we need to train more epochs because of decrement in validation loss.


Keras is an elevated level neural systems API, fit for running on TensorFlow, Theano and CNTK. It empowers quick experimentation because it is quite easy to understand and scalable. In today era, everything needs to be robust and performs complex computation with ease so that the use of deep learning with Keras becomes popular. It is used in various medical applications and researches.

fbicons FaceBook twitterTwitter google+Google+ lingedinLinkedIn pinterest Pinterest emailEmail


    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.

  • fb-15
  • twitter-15
  • linkedin-15


Trending Courses

AWS Course


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53
AWS Course

Upcoming Class

0 day 25 Sep 2023

DevOps Course


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

8 days 03 Oct 2023

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

4 days 29 Sep 2023

Hadoop Course


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

4 days 29 Sep 2023

Salesforce Course


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

1 day 26 Sep 2023

QA Course


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

2 days 27 Sep 2023

Business Analyst  Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst  Course

Upcoming Class

5 days 30 Sep 2023

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

11 days 06 Oct 2023

Python Course


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

4 days 29 Sep 2023

Artificial Intelligence  Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence  Course

Upcoming Class

4 days 29 Sep 2023

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

39 days 03 Nov 2023

Tableau Course


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
Tableau Course

Upcoming Class

4 days 29 Sep 2023

Search Posts


Receive Latest Materials and Offers on Data Science Course