Build your First Deep Learning Neural Network Model using Keras, TensorFlow in Python

Our product collects user’s behaviour (Page views, Events) on our sites. We contact every customer whether he is interested to buy a car or not? We thought what if we contact the user who is going to buy instead of everyone by this we create great customer experience and we will save some bucks so that we can invest that money and efforts on some other.

We can solve this problem using Artificial Intelligence right. So our journey towards AI(Artificial Intelligence)/ML(Machine Learning) started. We want to find out whether user buys a car or not. So, Its a classification problem. We thought it’s not a complicated problem so we can solve by using Logistic Regression or something else. Then we thought why not Deep Learning (Using TensorFlow). For a while, I thought I am doing over-engineering but this article encouraged me to go in this direction. That’s how its all started.

I am not an expert in this area. I have some idea about Machine Learning area which helped me a lot. I am going to concentrate a lot on coding part
rather machine learning & deep learning part.

First, you should have python installed in your system. If not follow this link.

Next, install TensorFlow in your system. You can follow this link. I am using Mac so the steps mentioned here are related to MacOS. I chose Virtualenv option because I feel more comfortable with that. I feel Its easy to on/off/uninstall anytime. I used python3 rather than python2.

# Open Terminal
# Navigate to a folder where you want to install TensorFlow
sudo easy_install pip
pip install --upgrade virtualenv
virtualenv -p python3 .
# Activate the Virtualenv environment
source ./bin/activate
# Install TensorFlow and all the packages
pip3 install --upgrade tensorflow
#Install Keras
pip3 install keras

You might be wondered why Keras. When I started writing code in TensorFlow I feel clumsy. I heard a lot about Keras so when I saw examples related to Keras felt each step explaining what it actually does.

Let’s start coding. I love GOT(Game Of Thrones). I thought why not predict the characters death 😉. Then I found this beautiful DataSet. Let’s start step by step.

Step1: You will find 3 files I used character-predictions.csv. Let’s load this. For this I used pandas.

# load GOT characters death dataset
import pandas as pds
dataframeX = pds.read_csv('data/character-predictions.csv', usecols= [7, 16, 17, 18, 19, 20, 25, 26, 28, 29, 30, 31])
dataframeY = pds.read_csv('data/character-predictions.csv', usecols=[32])

I am storing Features data in dataframeX & label data in dataframeY. I am using some of the features because for now I am concentrating on the fields where I can get this job done without any big data manipulations.

Step2: Splitting train & test data for the model.
So that we can evaluate model properly after training.

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(dataframeX.values, dataframeY.values, test_size = 0.2)

Step3: We don’t want to give more preference to any particular feature so we need to scale them so that every feature will have equal preference.

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

Step4: This step is most important one. Here we are going to create the Neural Network. We will import required modules then we will use Sequential module for initializing NN and Dense module to add Input, Hidden & Output Layers.

# create model
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(15, input_dim=12, activation='relu'))
model.add(Dense(15, activation='relu'))
model.add(Dense(15, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

In above step, We are building NN(Neural Network). In this first one is input layer then 2 hidden layers & at last output layer. Here we are using rectifier(relu) function in our hidden layer and Sigmoid function in our output layer as we want binary result from output layer but if the number of categories in the output layer is more than 2 then use SoftMax function. We have 12 features that’s why we gave input_dim as 12 in the input layer.

Activation Function: Very important to understand. Neuron applies activation function to weighted sum(summation of Wi * Xi where w is weight, X is input variable and i is suffix of W and X). I tried different ones(like sigmoid, relu) got good results with relu here that’s why proceeded further with this. It doesn’t mean that every time with relu activation function we get good results. We should try with different ones so that we can pick best one.

Neural Network Diagram

Step5: We built model now its time to compile the model

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Our output is a binary value so used binary_crossentropy as loss function. If our output has more than 2 categories I might have used categorical_crossentropy. I used Adam as Optimizer to make sure our weights optimized properly. I used accuracy as metrics to improve the performance of our neural network.

Step6: logging our model process for TensorBoard graph purpose

# log for tensorboard graph purpose
import keras
tbCallBack = keras.callbacks.TensorBoard(log_dir='/tmp/keras_logs', write_graph=True)

Step7: Its time to the fit our model. We use our features & label data to the fit our model.

# Fit the model, Y_train, epochs=100, batch_size=50,  verbose=1, callbacks=[tbCallBack])

In above step, epochs are nothing but the number of iterations. I used 100 here because got optimum results with 100. You need to try different numbers to get best one. batch_size is used to specify the number of observation after which you want to update weight. I used 1 for verbose for printing the model fitting process on screen. If you put 0 then you won’t get any output at the time of model fitting.

Step8: Predicting the results with test data.

# Predicting the Test set results
Y_pred = model.predict(X_test)
Y_pred = (Y_pred > 0.5)

The prediction values range from 0 to 1. I am considering the value 1 if we get prediction value more than 0.5 or else 0.

Step9: Its time to calculate the accuracy of our model.

# Calculating Model Accuracy
from sklearn.metrics import accuracy_score
acs = accuracy_score(Y_test, Y_pred)
print("\nAccuracy Score: %.2f%%" % (acs * 100))

Step10: Save our model for further use.

# Save the model'models/gotCharactersDeathPredictions.h5')

Congrats!!! You built your first model. I got around 81.54% accuracy. You will find this example & couple of others at my GitHub repository.

Peace. Happy Coding.

Related Post

Leave a Reply

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