You are on page 1of 3

Pandit Deendayal Petroleum University

School of Technology
Department of Computer Science and Engineering
19CP402P Machine Learning
BTech ICT/CE Semester VII, Autumn Semester 2019-20

Lab 1: Creating First ANN using Keras and Tensor flow

TENSOR FLOW AND KERAS


Tensor Flow [1 - 2]

Tensor Flow is an open source library for fast numerical computing. It was created and is
maintained by Google and released under the Apache 2.0 open source license. The API is
nominally for the Python programming language, although there is access to the underlying
C++ API. Unlike other numerical libraries intended for use in Deep Learning like Theano,
TensorFlow was designed for use both in research and development and in production systems,
not least Rank Brain in Google search1 and the fun Deep Dream project2. It can run on single
CPU systems, GPUs as well as mobile devices and large scale distributed systems of hundreds
of machines.
1https://en.wikipedia.org/wiki/RankBrain
2https://en.wikipedia.org/wiki/DeepDream

Keras

Keras is a minimalist Python library for deep learning that can run on top of Theano or
TensorFlow. It was developed to make developing deep learning models as fast and easy as
possible for research and development. It runs on Python 2.7 or 3.5 and can seamlessly execute
on GPUs and CPUs given the underlying frameworks. It is released under the permissive MIT
license. Keras was developed and maintained by Francois Chollet, a Google engineer using
four guiding principles:

1. Modularity: A model can be understood as a sequence or a graph alone. All the concerns
of a deep learning model are discrete components that can be combined in arbitrary
ways
2. Minimalism: The library provides just enough to achieve an outcome, no frills and
maximizing readability.
3. Extensibility: New components are intentionally easy to add and use within the
framework, intended for developers to trial and explore new ideas.
4. Python: No separate model files with custom file formats. Everything is native Python.

Building Deep Learning Models with Keras

The focus of Keras is the idea of a model. The main type of model is a sequence of layers called
a Sequential which is a linear stack of layers. You create a Sequential and add layers to it in
the order that you wish for the computation to be performed. Once defined, you compile the
model which makes use of the underlying framework to optimize the computation to be
performed by your model. In this you can specify the loss function and the optimizer to be
used. Once compiled, the model must be fit to data. This can be done one batch of data at a
time or by firing off the entire model training regime. This is where all the compute happens.

1
Once trained, you can use your model to make predictions on new data. We can summarize the
construction of deep learning models in Keras as follows:
1. Define your model. Create a Sequential model and add configured layers.
2. Compile your model. Specify loss function and optimizers and call the compile ()
function on the model.
3. Fit your model. Train the model on a sample of data by calling the fit () function on
the model.
4. Make predictions. Use the model to generate predictions on new data by calling
functions such as evaluate () or predict () on the model.

Exercise 1: Run the following Script in single step and write output after each command

# Example of TensorFlow library


import tensorflow as tf

# declare two symbolic floating-point scalars


a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

# create a simple symbolic expression using the add function


add = tf.add(a, b)

# bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'


sess = tf.Session()
binding = {a: 1.5, b: 2.5}
c = sess.run(add, feed_dict=binding)
print(c)

Exercise 2 [3]

Visit http://playground.tensorflow.org and create your own ANN for classifying Non-linear
pattern

Exercise 3: Develop your First ANN with Keras

The steps you are going to cover are as follows:


1. Load Data.
2. Define Model.
3. Compile Model.
4. Fit Model.
5. Evaluate Model.

Pima Indians Onset of Diabetes Dataset [4]

In this lab we are going to use the Pima Indians onset of diabetes dataset. It describes patient
medical record data for Pima Indians and whether they had an onset of diabetes within five
years. It is a binary classification problem (onset of diabetes as 1 or not as 0). The input
variables that describe each patient are numerical and have varying scales. Below lists the
eight attributes for the dataset:
1. Number of times pregnant.

2
2. Plasma glucose concentration 2 hours in an oral glucose tolerance test.
3. Diastolic blood pressure (mm Hg).
4. Triceps skin fold thickness (mm).
5. 2-Hour serum insulin (mu U/ml).
6. Body mass index.
7. Diabetes pedigree function.
8. Age (years).
9. Class, onset of diabetes within five years.

Exercise 4: Download and Study the PIMA.CSV file from the LMS.

# Initialise Random Number Generator for numpy


from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load pima indians dataset


dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[: ,0:8]
Y = dataset[: ,8]

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

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

# Fit the model


model.fit(X, Y, nb_epoch=150, batch_size=10)

# evaluate the model


scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

Exercise 5:
1. Study each command and its syntax.
2. Draw the structure of ANN created.
3. Change the parameters and observe their impact on execution.
References
[1] https://www.tensorflow.org/guide/
[2] https://www.tensorflow.org/tutorials/
[3] http://playground.tensorflow.org
[4] https://www.kaggle.com/uciml/pima-indians-diabetes-database

You might also like