Lab Manual-ANN
Lab Manual-ANN
Lab Manual
CL461-Artificial Intelligence Lab
Introduction
There are many deep learning libraries out there, but the most popular ones
are TensorFlow, Keras, and PyTorch. We will be focusing on Keras in this
guide.
Keras is a high-level neural networks API, written in Python, and can run on
top of TensorFlow, CNTK, or Theano. It was developed with a focus on
enabling fast experimentation. The advantages of using Keras emanates from
the fact that it focuses on being user-friendly, modular, and extensible.
In this guide, we will focus on how to use the Keras library to build
classification models.
Problem Statement
Diabetes is a serious health issue which causes an increase in blood sugar.
Many complications occur if diabetes remains untreated and unidentified.
The aim of this guide is to build a classification model to detect diabetes. We
will be using the diabetes dataset which contains 768 observations and 9
variables, as described below:
Steps
Step 1 - Loading the required libraries and modules
Step 2 - Loading the data and performing basic data checks
Step 3 - Creating arrays for the features and the response variable
Step 4 - Creating the Training and Test datasets
Step 5 - Define, compile, and fit the Keras classification model
Step 6 - Predict on the test data and compute evaluation metrics
The following sections will cover these steps.
target_column = ['Outcome']
predictors = list(set(list(df.columns))-set(target_column))
list(set(list(df.columns))-set(target_column))
df[predictors] = df[predictors]/df[predictors].max()
df.describe()
The first couple of lines creates arrays of independent (X) and dependent (y)
variables, respectively. The third line splits the data into training and test
datasets, with 30% of the observations in the test set. The fourth line of code
prints the shape of the training set (537 observations of 8 variables) and test
set (231 observations of 8 variables).
X = df[predictors].values
y = df[target_column].values
Since our target variable represents a binary category which has been coded
as numbers 0 and 1, we will have to encode it. We can easily achieve that
using the "to_categorical" function from the Keras utilities package. The two
lines of code below accomplishes that in both training and test datasets.
count_classes = y_test.shape[1]
print(count_classes)
The fifth line of code creates the output layer with two nodes because there
are two output classes, 0 and 1. We use 'softmax' as the activation function
for the output layer, so that the sum of the predicted values from all the
neurons in the output layer adds up to one.
In the above lines of codes, we have defined our deep learning model
architecture. But before we can start training the model, we will configure the
learning process. This is done in the last line of code using the
model.compile() function.
In defining our compiler, we will use 'categorical cross-entropy' as our loss
measure, 'adam' as the optimizer algorithm, and 'accuracy' as the evaluation
metric. The main advantage of the "adam" optimizer is that we don't need to
specify the learning rate, as is the case with gradient descent. Using “adam”
will, thereby, save us the task of optimizing the learning rate for our model.
model = Sequential()
model.add(Dense(500, activation='relu', input_dim=8))
model.add(Dense(100, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(2, activation='softmax'))
Now we are ready to build the model which is done in the code below. We
also provide the argument, epochs, which represents the number of training
iterations. We have taken 20 epochs.
The same is repeated in the fourth, fifth and sixth lines of code which is
performed on the test data.
pred_train= model.predict(X_train)
scores = model.evaluate(X_train, y_train, verbose=0)
print('Accuracy on training data: {}% \n Error on
training data: {}'.format(scores[1], 1 - scores[1]))
pred_test= model.predict(X_test)
scores2 = model.evaluate(X_test, y_test, verbose=0)
print('Accuracy on test data: {}% \n Error on test data:
{}'.format(scores2[1], 1 - scores2[1]))