Neural Network Model using Keras Python Library

Reading Time: 3 minutes

Keras runs on top of open source machine libraries like TensorFlow, Theano or Cognitive Toolkit (CNTK). Theano is a python library use for fast numerical computation tasks.

TensorFlow is the most famous symbolic math library use for creating neural networks and deep learning models. TensorFlow is very flexible and the primary benefit is distribute computing.

CNTK is deep learning framework developed by Microsoft. It uses libraries such as Python, C#, C++ or standalone machine learning toolkits. Theano and TensorFlow are very powerful libraries but difficult to understand for creating neural networks.

Coding Part

1. Understanding and Loading the datasets

First Import Libraries like NumPy, pandas, and also import classes named sequential and dense from Keras library.

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense

Now here I am using the Pima Indians onset of diabetes dataset which is a standard machine learning dataset from the UCI Machine Learning repository and the link can be found under reference section. This dataset tells about the patient medical record and whether they had an onset of diabetes within five years also it is a binary classification problem.

Now import the dataset using pandas and then let us understand more about the datasets and then split the datasets into dependent and independent variables.

dataset = pd.read_csv('pima-indians-diabetes.csv')
X = dataset[:,0:8]
y = dataset[:,8]

2. Defining the Keras Model

Models in Keras are defined as a sequence of layers in which each layer is add one after another. The input should contain input features and is specified when creating the first layer with the input_dims argument. Here inputs_dims will be 8.

It is quite difficult to know how many layers we should use. Generally we use Keras Tuner for this, which takes a range of layers, a range of neurons, and some activation functions. and then by permutation and combination, it tries to find which is best suited. But one disadvantage of this is it takes lots of time. You can refer to the documentation of it Keras Tuner for more details. 

In this example, a fully connected network with a three-layer is used which is defined using the Dense Class. The first argument takes the number of neurons in that layer and, and the activation argument takes the activation function as an input. Here relu is used as an activation function. In the first two layers and sigmoid in the last layer as it is a binary classification problem.

model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

3. Compile Keras Model

While compiling we must specify the loss function to calculate the errors, the optimizer for updating the weights and any metrics.

In this case, we will use “binary_crossentropy“ as the loss argument as it is a binary classification problem.

Here we will take optimizer as “adam“ as it automatically tunes itself and gives good results in a wide range of problems and finally we will collect and report the classification accuracy through metrics argument.

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

4. Fitting The Keras Model

Now we will fit our model on the loaded data by calling the fit() function on the model.

The training process will run for a fixed number of iterations through the dataset which is specified using the epochs argument. The number of dataset rows should be and are updated within each epoch, and set using the batch_size argument.

Here, We will run for 150 epochs and a batch size of 10., y, epochs=150, batch_size=10)

5. Evaluate Keras Model

The evaluation of the model on the dataset can be done using the evaluate() function. It takes two arguments i.e, input and output. It will generate a prediction for each input and output pair and collect scores, including the average loss and any metrics such as accuracy.

The evaluate() function will return a list with two values first one is the loss of the model and the second will be the accuracy of the model on the dataset. We only have interest in reporting the accuracy and hence we ignore the loss value.

_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

6. Make Predictions

Prediction can be done by calling the predict() function on the model. Here sigmoid activation function is use on the output layer, so the predictions will be a probability in the range between 0 and 1.

predictions = model.predict(X)
rounded = [round(x[0]) for x in predictions]


Here we have learned how to create your first neural network model using the powerful Keras Python library for deep learning.

There are six main steps in using Keras to create a neural network or deep learning model that are loading the data, defining the neural network in Keras after that compiling, evaluating, and finally making the predictions with the model.


Dataset used

Thankyou and Keep Learning!!!

Written by 

Shubham Gupta is software trainee at knoldus. He is practising Devops - Docker, Jenkins, Ansible, Kubernetes. He is passionate about DevOps technology and cloud computing. He like to watch movies and playing online games.

Leave a Reply