ADITYA COLLEGE OF ENGINEERING & TECHNOLOGY
Machine Learning
UNIT-5
Neural Networks and Deep Learning
By
B Manikyala Rao M.Tech(Ph.d)
Sr Assistant Professor
Dept of Computer Science & Engineering
Aditya College of Engineering & Technology
Surampalem
Aditya College of Engineering & Technology
Introduction to Artificial Neural Networks with Keras
➢An Artificial Neural Network in the field of Artificial intelligence where it attempts to mimic
the network of neurons makes up a human brain so that computers will have an option to
understand things and make decisions in a human-like manner. The artificial neural network is
designed by programming computers to behave simply like interconnected brain cells.
Input Layer:
• As the name suggests, it accepts inputs in several different formats provided by the
programmer.
Hidden Layer:
• The hidden layer presents in-between input
and output layers. It performs all the calculations
to find hidden features and patterns.
Output Layer:
• The input goes through a series of transformations
using the hidden layer, which finally results in output
that is conveyed using this layer.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
➢ Keras model represents the actual neural network model. Keras provides a two mode to create the model, simple and easy to
use Sequential API as well as more flexible and advanced Functional API. Let us learn now to create model using
both Sequential and Functional API in this chapter.
Sequential:
➢ The core idea of Sequential API is simply arranging the Keras layers in a sequential order and so, it is called Sequential API. Most of
the ANN also has layers in sequential order and the data flows from one layer to another layer in the given order until the data
finally reaches the output layer.
➢ A ANN model can be created by simply calling Sequential() API as specified below −
from keras.models import Sequential
model = Sequential()
Add layers:To add a layer, simply create a layer using Keras layer API and then pass the layer through add() function as specified below
−
from keras.models import Sequential
model = Sequential()
input_layer = Dense(32, input_shape=(8,)) model.add(input_layer)
hidden_layer = Dense(64, activation='relu'); model.add(hidden_layer)
output_layer = Dense(8) model.add(output_layer)
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Introduction to Artificial Neural Networks with Keras
Access the model:
Keras provides few methods to get the model information like layers, input data and output data. They are as
follows −
model.layers − Returns all the layers of the model as list.
model.inputs − Returns all the input tensors of the model as list.
model.outputs − Returns all the output tensors of the model as list.
• model.get_weights − Returns all the weights as NumPy arrays.
• model.set_weights(weight_numpy_array) − Set the weights of the model.
Serialize the model:
• Keras provides methods to serialize the model into object as well as json and load it again later. They are as
follows −
get_config() − IReturns the model as an object.
from_config() − It accept the model configuration object as argument and create the model accordingly.
to_json() − Returns the model as an json object.
model_from_json() − Accepts json representation of the model and create a new model.
to_yaml() − Returns the model as a yaml string.
model_from_yaml() − Accepts yaml representation of the model and create a new model.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Introduction to Artificial Neural Networks with Keras
Summarise the model:
• Understanding the model is very important phase to properly use it for training
and prediction purposes. Keras provides a simple method, summary to get the
full information about the model and its layers.
Train and Predict the model:
Model provides function for training, evaluation and prediction process. They are
as follows −
• compile − Configure the learning process of the model
• fit − Train the model using the training data
• evaluate − Evaluate the model using the test data
• predict − Predict the results for new input.
Functional API
• Sequential API is used to create models layer-by-layer. Functional API is an
alternative approach of creating more complex models. Functional model, you
can define multiple input or output that share layers. First, we create an instance
for model and connecting to the layers to access input and output to the model.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Introduction to Artificial Neural Networks with Keras
Create a model:Import an input layer using the below module −
from keras.layers import Input
Now, create an input layer specifying input dimension shape for the model
using the below code −
data = Input(shape=(2,3))
Define layer for the input using the below module −
from keras.layers import Dense
Add Dense layer for the input using the below line of code −
layer = Dense(2)(data)
Define model using the below module −
from keras.models import Model
Create a model in functional way by specifying both input and output layer −
model = Model(inputs = data, outputs = layer)
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Multi-layer perception Implementation
• Multi-layer perception is the basic type of algorithm used in deep
learning it is also known as an artificial neural network and they are
the most useful type of neural network.
• The goal is to create robust algorithms and data structures that can
be used to model difficult problems MLP utilizes a supervised learning
technique called backpropagation.
• MLP is used to solve regression and classification problems we use
MLP in speech and image processing computer vision time series
prediction and machine translation
• In MLP neurons are arranged into networks a row of neurons is called
a layer and one network can have multiple layers any network model
starts with an input layer that takes input from the data set layers
after the input layer are called hidden layers.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
• In the above example, we have two hidden layers that are not directly
exposed to the input the planning can refer to have many hidden
layers in our network model usually if we increase the number of
hidden layers the model will work more efficiently.
• The final layer is called the output layer and it is responsible for the
final outcome the choice of activation function in the output layer
strongly depends on the type of problem.
• A multi-class classification problem may have multiple neurons in the
final output layer one for each class in this scenario softmax
activation function may be used to output our probability of the
network.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Implementation of Multi-layer Perceptron in Python using Keras
• The basic components of the perceptron include Inputs, Weights and
Biases, Linear combination, and Activation function. Following is the
basic terminology of each of the components.
1.Inputs of a perceptron are real values input.
2.Weights are parameters within the neural network to transform input
data.
3.Bias is an additional parameter used to adjust output along with a
weighted sum.
4.Linear combination is the merging of input values.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
import tensorflow as tf
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train=keras.utils.to_categorical(y_train,10)
y_test=keras.utils.to_categorical(y_test,10)
model = Sequential()
model.add(Dense(512, input_shape=(784, ), activation='relu'))
model.add(Dense(768, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x, y, epochs=100)
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
• Tensorflow is a free and open-source software library used to do
computational mathematics to build machine learning models more
profoundly deep learning models.
Features of Tensorflow:
• Computational Framework – It is a platform that provides huge
computational framework at one place. In this, computation is
approached as a dataflow graph.
• Mobile Deployment – It is an easy way to build projects with mobile
deployment
• High Performance – Due to its ease and varying toolkits, it gives high-
performance user experience.
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Downloading and Installation
• Tensorflow can be downloaded from its official Website tensorflow.org
Step 1: Click on Install on top navigation bar of Tensorflow Website.
Step 2: Before proceeding we need to get python environment. Choose pip
in the left side and go to python section and install python environment to
work on it.
Step 3: Python environment can be downloaded from python.org.
Step 4: To install Tensorflow, we need pip for python. Usually, pip comes
already installed if the python version is 3 or above.
Step 5: Creating a Tensorflow virtual environment
Can be done with the use of the following command:
pip install --user virtualenv
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Step 6: After setting up the virtual environment, use the following
command to install Tensorflow pip package:
pip install --upgrade tensorflow
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Machine Learning B Manikyala Rao
Aditya College of Engineering & Technology
Machine Learning B Manikyala Rao