artificial neural network using keras

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have been learning about neural networks for a last few articles and now we have come to final step. We will create a simple artificial neural network using keras.

Artificial Neural Network using Keras:

We need theano, tensorflow and keras libraries to create artifical neural networks.

Installing Theano:

Theano is capable of evaluating complicated mathematical expressions and running on graphical processing units. Install theano by running below command on terminal.

pip install –upgrade –no-deps git+git://github.com/Theano/Theano.git

It will download theano from git and install it.

Installing TensorFlow:

Tensorflow facilitates machine learning and deep learning with its extensive support for numerical computations. Installation instructions are given on tensorflow official page. Please install it in a way most suitable for you.

Installing Keras:

Keras implements deep learning concepts using python and uses theano and tensorflow under the hood. We can directly create artificial neural networks using keras because it wraps all the complicated things. Run below command on below terminal to install.

pip install –upgrade keras

The Problem Statement:

We have a dataset of customers of a bank. It consists of customerId, surname, creditScore, geography, gender,age, tenure, balance, numberOfProducts, hasCreditCard, isActiveMember, estimatedSalary and whether is exited the bank. Based on the data we want to predict whether a customer is going to leave the bank or not. This is a classification problem.

Please download dataset for artificial neural network using keras from https://superdatascience.com/machine-learning/.

Read dataset:

We are simply going to read the downloaded dataset churn_modelling.csv.

Note that we are picking only relevant independent variables from dataset and storing in X matrix. We omitted columns 0,1 and 2 from dataset as rowNumber, customerId and surname are not really important for deciding whether the customer will leave the bank or not.

y matrix stores whether customer left the bank or not.

Data Preprocessing:

Note that customer information contains categorical variables gender and geography. We need to encode them and convert into numbers as discussed in data preprocessing article .

Create Training and Test Set:

We have information of 10000 customers. Let us use 90% data as training set and 10% data as test set.

Perform Feature Scaling:

Artificial Neural Network Using Keras:

Import Keras Libraries:

  • Sequential model is a liner structure of layers of artificial neural network.
  • Dense is an actual neural network layer

Create Neural Network:

  • We initialized an artificial neural network using keras Sequential class and stored in classifier.
  • Then created a Dense  input layer with
    • output_dim as 12 ( The number 12 came from simply adding 11 input variables and 1 output variable. There is not strict rule for output_dim parameter ),
    • activation function as ‘relu’ (It is actually a rectifier function and generally applied to input and hidden network layers ) and
    • input_dim = 11 because we have 11 independent input variables in X matrix.
    • added this layer to neural network using classifier.add()
  • Then created another Dense hidden layer with similar parameters without input_dim (because input_dim parameter is provided to only input layer) and added this hidden layer using classifier.add()
  • Then created an output layer with output_dim as 1 and sigmoid activation function (because generally it is applied for output layers) and added it to network by classifier.add().
  • Then compiled this neural network using ‘adam’ optimizer, ‘binary_crossentropy’ loss function for accuracy metrics

That’s it. We have created a neural network, added an input layer, a hidden layer and an output layer to it and compiled it. Now its time for training and making predictions.

Train ANN model:

classifier.fit() method trains the model with 9000 rows in batch size of 10 rows each. Once the model is trained with all 9000 rows, one epoch is completed. As we have provided nb_epoch = 100 . Our model will be trained 100 times in batches of 10 rows on 9000 rows. As explained in previous articles, different weights will be assigned to neural networks using backpropagation so that optimal cost function value is found.

       Note that our model calculates probability of a customer leaving bank. We are deciding a customer will leave bank if probability is more than 0.5 or he stays with bank if probability is less than or equal to 0.5.

 Execute the Program:

Run program and you should see that 100 iterations are being performed as below.

Epoch 97/100
9000/9000 [==============================] – 2s 202us/step – loss: 0.3173 – acc: 0.8676
Epoch 98/100
9000/9000 [==============================] – 2s 216us/step – loss: 0.3171 – acc: 0.8692
Epoch 99/100
9000/9000 [==============================] – 2s 210us/step – loss: 0.3176 – acc: 0.8684
Epoch 100/100
9000/9000 [==============================] – 2s 205us/step – loss: 0.3172 – acc: 0.8694

Note that accuracy is changing for each epoch.

 Confusion Matrix:

Use confusion matrix to compare test set against predictions.

confusion matrix for ANN
confusion matrix for ANN

We ran predictions on 1000 rows and out of them (765 + 101) = 866 results are correct. So our accuracy for artificial neural network using keras is 86.6 %.

 

 

References:

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

One thought on “artificial neural network using keras

Leave a Reply

Your email address will not be published. Required fields are marked *