Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1891

Introduction to Artificial Neural Network in Python

$
0
0

Artificial Neural Networks are at the very core of Deep Learning. They are powerful, versatile, and scalable, making them ideal to tackle large and highly complex Machine Learning tasks powering speech recognition services (e.g., Apple’s Siri), classifying billions of images (e.g., Google Images), recommending the best videos to watch (e.g., YouTube).

In this blog, we are going to cover everything about Artificial Neural Network and how to build an Artificial Neural Network in Python.

What are Artificial Neurons?

  • Artificial Neurons were first popularized in 1943 by the neurophysiologist Warren McCulloch and the mathematician Walter Pitts.
  • Pitts and McCulloch presented a simplified computational model of how biological neurons might work together in animal brains to perform complex computations using propositional logic.
  • At that time due to a lack of resources, the progress of ANN’s technique was slow, and by the 1990s other machine learning techniques offers better results.
  • Due to the huge quantity of data available to train neural networks, a tremendous increase in computing power, ANNs seems to have entered a virtuous circle of funding and progress.
  • Before we deep dive into artificial neurons, let’s take a quick look at a biological neuron.

What are Biological Neurons?

  • It’s composed of a cell body containing the nucleus and most of the cell’s complex components, one very long extension called the axon. plus many branching extensions called dendrites.
  • Near its extremity, the axon divided into many branches called telodendria, and at the tip of these branches are minuscule structures called synaptic terminals.
  • Biological neurons generate short electrical which travel along the axons and make the synapses discharge chemical signals called neurotransmitters.
  • Each neuron commonly connected to thousands of other neurons. Highly complex computations can be performed by a network of fairly simple neurons.

Biological-neuron

How Does Artificial Neural Network Work?

  • To understand Artificial neural networks, we need to understand the most basic unit of an Artificial Neural Network, i.e. a Perceptron.
  • The artificial neuron also has inputs and outputs so we can attempt to mimic the biological neuron.

What Is A Perceptron?

  • The Perceptron is one of the simplest ANN architectures, invented in 1957 by Frank Rosenblatt.
  • The inputs and output are numbers (instead of binary on/off values), and each input connection is associated with a weight.
  • The inputs of the Perceptron are fed to special passthrough neurons called input neurons: they output whatever input they are fed.
  • It has 4 important components:
    1. Inputs
    2. Weights and Bias
    3. Summation Function
    4. Activation or transformation Function

Perceptron-model

The basic logic behind a Perceptron is as follows:

  • X represents the matrix of input features. It has one row per instance and one column per feature.
  • The weight matrix W contains all the connection weights except for the ones from the bias neuron.
  • The bias vector b contains all the connection weights between the bias neuron and the artificial neurons. It has one bias term per artificial neuron.
  • The function ϕ is called the activation function.

How is Perceptron Trained?

  • The Perceptron is supplied one training instance at a time, and for each instance, it makes its predictions.
  • For every output neuron that produced a wrong prediction, it adjusts the connection weights from the inputs that would have contributed to the correct prediction.

perceptron-weight-updates

The Multilayer Perceptron and Backpropagation

  • An MLP is composed of one (passthrough) input layer, one or more layers of threshold logic units called hidden layers, and one final layer of threshold logic unit called the output layer.
  • The backpropagation algorithm finds out how each connection weight and each bias term should be tweaked in order to reduce the error.

backpropagation-training-algorithm

Artificial Neural Network in Python

  • Deep Learning with first the classic artificial neural network, meaning the fully connected neural network with only fully connected layers, with no convolutional layers or other types of layers.
  • Here we will just have an input vector containing different features and we will predict an outcome that will be a binary variable because you have to know that actually artificial neural networks can be both used for regression or classification. And here we’re going to do it for classification.

Implementation First Artificial Brain

we have a data set of a bank which collected some information about their 10000 customers. Bank actually observed their customers for a certain period of time, let’s say, six months. They observed it during the six months they left the bank or stayed in the bank.

dataset

Data Preprocessing

1) The first thing we’re gonna do here is to import the libraries.

ann-libraries

2) We import the dataset. For ‘x ‘we will specify index three so that we can take all the columns starting from the column of index three up to the one before last and taking all the rows, all the values of the dataset. For ‘y’ will just take the last column of this dataset, which is exactly what we want for our dependent.

data-import

3) In our dataset there are two categorical variables. This first one, giving the country of residence of the customers. And the second one giving the gender of the customers. So, we’ll have to do some encoding work here to encode these categorical data in either simple label.

encoding-columns

4) Now, we split the dataset into the training set and test it. Feature scaling is absolutely compulsory for deep learning. Whenever you build an artificial neural network, you have to apply feature scaling. That’s absolutely fundamental.

spliting-data-in-train-test

Note: Data preprocessing face counts for 70 percent of the work of a data scientist.

Building the ANN

  • We will create our new variable, and which will be nothing else than the artificial neural network we’re going to build.
  • We will create that variable as an object of the sequential class. Tensor Flow, which has a short T.F. from which we’re going to call a Keras Library. And from which we’re going to call the models module.
  • The next step, which is to add the input layer and the first hidden layers. we’re gonna call one of the methods of the sequential class and that method is ‘add’. A fully-connected neural network must be the rectifier activation function.
  • Output layer to be fully connected to that second hidden layer and therefore we’re using again here the dense class but two parameters have to be changed. The value of that unit parameter here that we have to replace right now is actually one and activation function but a sigmoid activation function.

building-ann

Training the ANN

  • Compiling the ANN, we first need to start from our ann object, and then from this object, we’re going to call a new method, the method to compile an artificial neural network is simply the compile method.
  • We have to enter three parameters. i.e. an optimizer, loss function, and matrix.
  • Training the ANN on the Training set, the method to train whatever machinery model is the fit method, which will take always the same parameters.
  • The first one is X train for, you know, the matrix of features of the training set, then Y train for the dependent variable vector of the training set. And then first batch size and final parameter is a number of epochs.

training-ann

Making the predictions and evaluating the model

  • we need to call the predict method, the predicted probability is larger than 0.5 so that if Y pred is between 0 and 0.5, then this new Y pred will become 0 because this won’t be true.
  • And if Y pred is larger than 0.5, then this will be true. we’ll get therefore the final predicted binary outcomes one.
  • We get an accuracy of over 86 percent. it means that out of 100 customers 86 were predicted correctly to either stay in or leave the bank.

predict-test-result

Next Task For You

Begin your journey towards Introduction To Data Science and Machine Learning by joining our FREE Informative Class on Introduction To Data Science and Machine Learning by clicking on the below image.DS_ML_ContentUpgrade

The post Introduction to Artificial Neural Network in Python appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1891

Trending Articles