Neural Network
Neural Network
Ms. Qurat-ul-Ain
Outline
Neural Networks
Structure Of ANN
Activation Functions
ANN Learning Techniques
ANN Working
Perceptron
Back Propagation
Forward Propagation
Feed-forward Network
ANN Advantages & Disadvantages
ANN Applications
What Is It?
An artificial neural network is a crude way of trying to simulate the human
brain (digitally)
Human brain – Approx 10 billion neurons
Parts of neuron
Cell body
between them
Large number of neurons: 1010
Parallel processing
Distributed computation/memory
1. Computational theory
2. Representation and algorithm
3. Hardware implementation
Reverse engineering: From hardware to theory
Each input into the neuron has its own weight associated with it
As each input enters the nucleus (blue circle) it's multiplied by its weight.
Introduction
The nucleus sums all these new input values which gives us
the activation
For n inputs and n weights – weights multiplied by input and summed
a = x1w1+x2w2+x3w3... +xnwn
Introduction
If the activation is greater than a threshold value - the neuron outputs
Let w0 = -T and x0 = 1
Output is 1 if D> 0;
Output is 0 otherwise
of many layers.
The multiple layers that are interconnected are often called “Multilayer
Perceptron”.
The neurons in one layer are called “Nodes”.
layer.
Hidden Layers: There can be one or more hidden layers. The processing that
takes place in the inner layers is called “hidden layers”. The hidden layers
calculate the output based on the “weights” which is the “sum of weighted
synapse connections”. The hidden layers refine the input by removing
redundant information and send the information to the next hidden layer for
further processing.
Output Layer: This hidden layer connects to the “output layer” where the
output is shown.
Comparison Between ML &ANN
Machine Learning Artificial Neural Network
Machine Learning learns from input data ANN are used in machine learning
and discovers output data patterns of algorithms to train the system using
interest. synapses, nodes and connection links.
ML programs can predict the outcome for ANN can learn and make intelligent
learned set of data and adjust itself for new decision on their own for new data but it is
data. deeper than machine learning.
Some examples of ML are Google search Some examples of ANN are face
results etc. recognition, image recognition, etc.
Neural Networks & Deep Learning
Deep Learning networks contain several hidden layers between the input and
the output.
These networks are distinguished by the depth of the hidden layers in them.
The input data passes through multiple steps before the output is shown.
These networks differ from the earlier NN such as perceptron which had a
The ability of the network to learn from unlabeled data is an advantage over the
recognized as the next layer will perform aggregation of features from the
previous layers.
Multiple hidden layers in the network increase complexity and
dimensional data.
Some examples of deep learning networks include clustering of millions
of data. For the unlabeled set of data, the networks such as Boltzmann
selection machines perform automatic feature extraction.
The network learns automatically by analyzing the input through sampling
unstructured data.
The more the training data fed to the network, the more accurate it will
become.
SVM Vs. Neural Network
SVM utilizes nonlinear mapping to make the data linear separable, hence
The more data that is fed into the network, it will better generalize better
and accurately make predictions with fewer errors. On the other hand, SVM
and Random Forest require much fewer input data.
They Are Both Parametric
In comparing the overall correctacies of the CNN and SVM classifier, CNN
Leaky ReLU
Softmax
Choice of the Activation Function
For classification tasks;
We prefer to use sigmoid, tanh functions and their combinations.
sometimes avoided.
As indicated earlier, ReLU function is mostly used (computationally
fast).
ReLu variants are used to resolve a dead neuron issue (e.g., Leaky ReLu).
It must be noted that ReLU function is only used in the hidden layers.
Start with ReLuor leaky/randomized Relu and if the results are not
Y Y Y Y
+1 +1 1 1
0 X 0 X 0 X 0 X
-1 -1 -1 -1
Applies activation
function to the sum of
results
Outputs result
ANN Learning Techniques
1: Supervised Learning
In this learning, the user trains the model using labeled data. It means
some data is already marked with the correct answers. Supervised learning
can be compared to the learning which is held in the presence of a
supervisor.
2: Unsupervised learning
In this learning ,the model does not need supervision. It usually deals with
the unlabeled data. User permits the model to work on its own to classify
the data. It sorts the data according to the similarities and patterns
without any prior training to the data.
ANN Working
The input node takes the information in numerical form.
The information represents an activation value where each node has given a
number.
The higher the number, the greater the activation.
Based on weights and activation function, the activation value passes to the next
node.
Each node calculates the weighted sum and updates that sum based on the transfer
function(activation function).
After that, it applies an activation function.
This function applies to this particular neuron. From that, the neuron concludes if
node.
The output layer shares the information in an understanding way.
The network uses the cost function to compare the output and
Back Propagation
Forward Propagation
Perceptron
In a neural network, we have the same basic principle, except the inputs are
They adjust themselves to minimize the loss function until the model is
groups.
Let’s say you want your neural network to be able to return outputs
If you replace the “true”s by 1 and the “false”s by 0 and put the 4 possibilities as
points with coordinates on a plan, then you realize the two final groups “false” and
“true” may be separated by a single line. This is what a Perceptron can do.
On the other hand, if we check the case of the “exclusive or” (in which the case
“true or true” (the point (1,1)) is false), then we can see that a simple line cannot
separate the two groups, and a Perceptron isn’t able to deal with this problem.
So, the Perceptron is indeed not a very efficient neural network, but it is simple to
=1+0*-1+2*1 =2
y = f(yin) = 1
Check y=t, No, so weight change is required.
w1(new) = w1(old) + atx1
= 0+1*-1*-1=1
b(new)= b(old)+ at
yin= b+ wlxl+w2x2
= -1+1*-1+1*-1= -3
y=f(yin)=-1
are calculated?
The weights are updated with the help of optimizers. Optimizers are the
backward.
This permits the user to train the neural network and modernize the
weights.
Due to the structured algorithm, the user can adjust weights simultaneously.
It will help the user to see which weight of the neural network is responsible
for error.
Feed-forward Network
This network contains an input, hidden, and output layer.
Signals can move in only one direction. Input data passes to the hidden
inputs.
The output of the previous layer becomes the input of the following
layer.
This continues through all the layers and determines the output.
It means signals can travel in both the direction using loops. Neurons can
Output layer
Hidden layer
wij
Input layer
Input vector: X
Nets Without Hidden Layers
Input layer
The cashier only tells you the total price of the meal
After several days, you should be able to figure out the price of each
portion.
Each meal price gives a linear constraint on the prices of the portions:
price x fishw fish xchipswchips xdrink wdrink
Solving The Problem
The prices of the portions are like the weights in of a linear
neuron.
We will start with guesses for the weights and then adjust the
Linear
Neuron
150 50 100
2 5 3
portions portions portions
of fish of chips of drink
A Model Of The Cashier’s Brain
With Arbitrary Initial Weights
Residual error = 350
Price of meal = 500 The learning rule is:
wi xi ( y yˆ )
With a learning rate of 1/35,
the weight changes are +20, +50,
+30
This gives new weights of 70,
50 50 50
100, 80
Notice that the weight for chips
2 5 3 got worse!
Portions Of Portions Of Portions Of
Fish Chips Drink
ANN Advantages & Disadvantages
Advantages
It has a parallel processing ability. It has the numerical strength that performs more than one
Disadvantages of ANN
Its black-box nature is the most prominent disadvantage of ANN.
The neural network does not give the proper explanation of determining the output. It reduces
There is no assurance of proper network structure. There is no proper rule to determine the
structure.
Limitations Of Neural Networks
These networks are black boxes for the user as the user does not have any
These algorithms are rather slow and require many iterations (also called
below.
Pattern Recognition: ANN is used in pattern recognition, image recognition,
import numpy as np
import pandas as pd
import tensorflow as tf
The basic principle while creating a machine learning model is to generate X also
called as Matrix of Features. This X basically contains all our independent variables.
Let’s create the same here.
#Generating Matrix of Features
X = data.iloc[:,3:-1].values
Implementing ANN in Python
#Generating Matrix of Features
X = data.iloc[:,3:-1].values
#Generating Dependent Variable Vectors
Y = data.iloc[:,-1].values
#Encoding Categorical Variable Gender
from sklearn.preprocessing
import LabelEncoder LE1 = LabelEncoder()
X[:,2] = np.array(LE1.fit_t
#Encoding Categorical variable Geography
from sklearn.compose
import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
Ct=ColumnTransformer(transformers=[('encoder',OneHotEncoder(),
[1])],remainder="passthrough")
X = np.array(ct.fit_transform(X))ransform(X[:,2]))
Implementing ANN in Python
#Splitting dataset into training and testing dataset
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test =
train_test_split(X,Y,test_size=0.2,random_state=0)
#Performing Feature Scaling
from sklearn.preprocessing
import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#Initialising ANN
ann = tf.keras.models.Sequential()
#Adding First Hidden Layer
ann.add(tf.keras.layers.Dense(units=6,activation="relu"))
Implementing ANN in Python
#Adding Second Hidden Layer
ann.add(tf.keras.layers.Dense(units=6,activation="relu"))
ann.add(tf.keras.layers.Dense(units=1,activation="sigmoid"))
#Compiling ANN
ann.compile(optimizer="adam",loss="binary_crossentropy",metrics=['
accuracy'])
#Fitting ANN
ann.fit(X_train,Y_train,batch_size=32,epochs = 100)
Implementing ANN in Python
#Predicting result for Single Observation
Output:
[[False]]
You can save your created neural network by writing the following
command
#Saving created neural network
ann.save("ANN.h5")
References
Jost Schatzmann (2003) Final Year Individual Project Report Using Self-
Analysis, 3:111-26
Wong and Bergeron (1997) Pak Chung Wong and R. Daniel Bergeron. 30 Years of
Multidimensional Multivariate Visualization. In Gregory M.
Overviews, Methodologies and Techniques, pages 3-33, Los Alamitos, CA, 1997.
IEEE Computer Society Press.