Unit 5

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

ADITYA COLLEGE OF ENGINEERING

Machine Learning
UNIT-5
Neural Networks and Deep Learning
Aditya College of Engineering

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
Aditya College of Engineering

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.

Machine Learning
Aditya College of Engineering

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
Aditya College of Engineering

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.

Machine Learning
Aditya College of Engineering

Introduction to Artificial Neural Networks with Keras

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
Introduction to Artificial Neural Networks with Aditya College of Engineering

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
Aditya College of Engineering
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
Aditya College of Engineering
Multi-layer perception

• 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
Aditya College of Engineering

• 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
Aditya College of Engineering

Implementation
• To construct our first multi-layer perception first we import sequential
model API from Keras.
• We are using Dense and dropout layers so we have to import them
from our Keras.
• To split our dataset we will use the train tests split function which is
available in scikit-learn model selection and plot_model will help us to
display our model.
• Finally, we import the load txt function from numpy which will be
used to load our data set let’s import them here we work with the
titanic data set.

Machine Learning
Aditya College of Engineering

• Now we construct a sequential model with dense and dropout layers


first we construct a dense layer with 32 neurons as this is the first
layer we have to specify the input dimension which is 4.
• So in the first hidden layer there will be 4 inputs and 32 outputs we
use RELU as our activation function the next one is another dense
layer with 16 neurons then dropout layer with 0.2 dropout is a
technique used to prevent a model from overfitting this dropout will
use 20% inputs at the time of model.

Machine Learning

You might also like