CS 611 Slides 5
CS 611 Slides 5
INTELLIGENCE
• Deep learning has empowered great projects like the driverless car, making it possible for the car to distinguish
between lampposts and pedestrians, recognize stop signs, etc.
• Deep learning is inspired by the structure of the human brain and how it operates.
• With deep learning, machines are able to perform tasks that require human intelligence to be performed.
• The machines learn from experience and acquire skills with the intervention by humans.
• Deep learning involves the use of artificial neural networks to learn patterns, trends and relations from large
datasets.
• Deep learning algorithms perform tasks repeatedly, while tweaking it in a bid to improve the outcome.
• With deep learning, computer models are able to perform classification tasks from text, images or sound.
• The deep learning models can provide a high level of accuracy, sometimes outperforming humans.
• Training of models is done using large sets of labeled data and neural networks with several layers.
DL Cont.
• The word deep refers to the number of hidden layers that a neural network has.
• Most neural networks have 2-3 hidden layers, but a deep neural network may have over 100 hidden layers. The training of
such models involves the use of large sets of data.
• Neural network architectures with no need for manual intervention are then used to extract patterns from the data.
• A good example of a deep neural network is the convolutional neural network (CNN). CNN uses 2D convolutional layers,
which makes it good for the processing of 2D data like images.
• With CNN, there is no need for extracting features manually as you are not required to identify the features to be used for
classifying images.
• CNNs directly extract features from images. The ability of deep learning models to extract features automatically makes
them suitable for use in computer vision problems like object classification.
• CNNs rely on numerous layers to detect the features of an image. The complexity of the image features increases at every
layer.
• In the first hidden layer for example, the edges of the mages may be detected while the last layer may learn complex
features in the image.
• A good example of the application of deep learning is in fraud detection systems. Once it learns the normal procedures,
any anomaly will be easily detected and classified as a potential for fraud.
Artificial Neural Networks (ANN)
• Neural networks form the core of deep learning. A neural network is a kind of
network in which the nodes are seen as artificial neurons.
• The concept of neural networks began in the 1980s. The neural network of the
human being is made up of a network of interconnected neurons for maintaining a
high level of coordination to receive and then transmit messages to the spinal cord
and the brain.
• Artificial Neural Networks are made up of neurons that have been created artificially.
These are then taught so that they can adapt to the cognitive skills of human beings.
• Some of the applications of ANNs are image recognition, soft sensors, voice
recognition, time series predictions, and anomaly detection.
ANN Cont.
• Neural networks are represented in the form of a mathematical model and they are mostly applied in machine
learning. They are made up of neurons which are connected to each other, sending each other signals.
• A neuron receives signals until they exceed its threshold, and this is the time it fires, meaning that it forwards
the signal to the next connected neuron in the network.
• The connections between the neurons can be done in the way we want, even to the same neuron, but the
problem comes in when we need to train the network. This explains why restrictions have to be imposed on
the creation of neural networks.
• For the case of a multi-layer perceptron, the neurons have been arranged into layers, and each neuron is
allowed to pass signals only to the next neuron in the layer. The first layer in this is the input layer, while the last
one is the output layer, and this will have the predicted values.
• In artificial neural networks, learning refers to the process of modifying the bias and the weights that are fed to
the network. Learning in neural networks is facilitated by training it, whereby a certain set of inputs are fed to
the network while expecting a particular output, which is the target output.
• The process of learning in artificial neural networks is similar to the concept of learning in human beings. The
training is done repeatedly until we get the value of weights that give us the targeted outputs. After each
training, normally referred to as an epoch, the weights are adjusted so that the error of the neural network can
be reduced. This is done until the error is minimized completely.
A Single Neuron Neural Network
in Python
A Single NN in Python Cont.
• A single neuron neural network will transform a given input into a particular
output.
• Based on the given inputs and the assigned weights, we can determine
whether the neuron fired or not.
• The neuron will have 3 input connections and 1 output. We will be using the
tanh activation function.
Why Activation Function?
• In Neural Network every neuron will do two computations:
sum = (w1*x1+w2*x2)+ b
• Below is the actual formula for the tanh function along with
the formula for calculating its derivative.
• We then created the NeuralNetwork class that will help us implement a neural network.
• To ensure that similar weights are generated after every run, we created a seed.
• The weights to be fed into the network have been stored in a 3 x 1 matrix.
• As we stated earlier, we have used tanh as the activation function of the network.
• The network has been trained by calculating the error in the output and adjusting the weights so as to
reduce this error.
• Before making any adjustments, it is good for us to know the amount of adjustment that needs to be
done.
• This has been calculated by multiplying the error with the input and then with the gradient of the tanh
function.
• At every step, the error and backpropagate the gradients will be calculated.
• The step for calculating the output of the neuron is known as forwarding
propagation
• However, a number of problems arise when we try to add more layers to a neural network.
• However, this problem can be solved to some extent using some sensible activation functions,
such as the ReLU family of activations.
• Another problem associated with a deep fully connected network is that the number of parameters
that are trainable in the network, that is, the weights, can grow rapidly.
• This is an indication that the training may become practically impossible or slow down.
• Convolutional neural networks can help us solve the second problem by relying on the correlations
between the adjacent inputs in images or the time series.
CNN Cont.
• Convolutional neural networks are used for the classification of images,
clustering them based on similarity and in object recognition by scenes.
• These types of networks can identify faces, street signs, individuals, platypuses,
eggplants, and other aspects regarding visual data.
• They are used together with text analysis through the Optical Character
Recognition (OCR) in which the images are seen as symbols that are to be
transcribed and sound can be applied once they have been represented visually.
• The use of neural networks in image recognition marks one of the reasons as to
why deep learning has become so popular in the world.
• They are widely applied in fields such as machine visions which are highly used
in robotics, self-driving cars, and treatments for visually impaired.
Example
• Consider an example where we are dealing with images of cats and dogs.
• The pixels that are close to the eyes of the cat are more likely to be the same
as the ones that are close to the cat’s nose rather than those close to the
dog’s nose.
• This means that not every node in a layer needs to be connected to all other
nodes in the next layer.
• The number of weight parameters that need to be trained in the model will be
cut.
• It wraps the efficient numerical computation libraries Theano and TensorFlow and allows you to
define and train neural network models in just a few lines of code.
• Keras contains numerous implementations of commonly used neural-network building blocks such
as layers, objectives, activation functions, optimizers, and a host of tools to make working with
image and text data easier to simplify the coding necessary for writing deep neural network code.
• The Keras library will be used to build a convolutional neural network that will recognize hand-
written digits.
• The MNIST which comes with 70,000 handwritten digits from 0 to 9 will be used.
• The MNIST database (Modified National Institute of Standards and Technology database) is a large
database of handwritten digits that is commonly used for training various image processing
systems. The database is also widely used for training and testing in the field of machine learning.
Loading the Dataset
• We will load the dataset into two variables, X_train and X_test, and y_train and
y_test will be used to hold the matching digits. Here is the code for this:
✴Optimizer- we will use the adam optimizer which will adjust the learning rate
throughout the period of training the model.
✴Metrics- we will use the accuracy metric to return an accuracy score once the
model has been run on the validation data set.
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
Train the Model
• To train the model, we will use the fit() function provided by Keras.
• We will provide the function with the training data, the target data and the
number of epochs for which we need to run the model.
• The function will return an array of 10 numbers which are probabilities that an image
has every possible digit from 0 to 9.
• We will run a prediction for the first four images in our test set, and display the first
four values in y_test to compare to the actual results:
model.predict(X_test[:4])
y_test[:4]
• The model is corrected since it has predicted 7, 2, 1 and 0 for the first four images,
the correct values for the y_test.
Code
from keras.datasets import mnist
model = Sequential()
mnist.load_data()
model.compile(optimizer='adam',
X_train = X_train.reshape(60000,28,28,1)
loss='categorical_crossentropy',
metrics=['accuracy'])
X_test = X_test.reshape(10000,28,28,1)
model.fit(X_train, y_train,
y_train = to_categorical(y_train)
validation_data=(X_test, y_test), epochs=3)
y_test = to_categorical(y_test)
model.predict(X_test[:4]) y_test[:4]
y_train[0]