Develop your first Deep Learning Model in Python with Keras
This article was published as a part of the Data Science Blogathon
An artificial Neural Network is a sub-field of Artificial Intelligence compiled under Deep Learning Neural Networks which attempt to mimic the network of neurons that makes the human brain which allows them to understand and respond like a human.
Table of Contents
- Overview of Neural Network
- Introduction to Keras
- Step by Step Implementation of your First Keras Model
- Combining all the code
Brief Overview of Neural Network
Neural Network consists of a larger set of neurons, which are termed units arranged in layers. In simple words, Neural Network is designed to perform a more complex task where Machine Learning algorithms do not find their use and fail to achieve the required performance.
Neural Networks are used to perform many complex tasks including Image Classification, Object Detection, Face Identification, Text Summarization, speech recognition, and the list is endless.
How neural networks learn complex features? A neural network has many layers and each layer performs a specific function and complex the network. The more the layers are more performance is received. That’s why the neural network is also called a multi-layer perceptron.
Introduction to Kears Library
Keras is a fast, open-source, and easy-to-use Neural Network Library written in Python that runs at top of Theano or Tensorflow. Tensorflow provides low-level as well as high-level API, indeed Keras only provide High-level API.
As a beginner, it is recommended to work with Keras first and then move to TensorFlow. The reason is using Tensorflow functions as a beginner is a little bit complex to understand and interpret but Keras functionality is simple.
Build your first Neural Network model using Keras
We will build a simple Artificial Neural network using Keras step by step that will help you to create your own model in the future.
Step-1) Load Data
We are going to use Pima Indians Diabetes Data which you can download from here. It is a simple dataset provided by the UCI Machine Learning dataset, which contains a medical record of Indian patients. We have to predict whether the patient has an onset of diabetes within 5 years.
import pandas as pd data = pd.read_csv('diabetes.csv') x = data.drop("Outcome", axis=1) y = data["Outcome"]
It is a binary classification problem where we have to say if their onset of diabetes is 1 or not as 0. All the columns are numerical, which makes it easy to directly create a neural network over it. Thus we have separated the independent and dependent data.
Step-2) Define Keras Model
Model in Keras always defines as a sequence of layers. It means that we initialize the sequence model and add the layers one after the other which is executed as the sequence of the list. Practically we have to try experimenting with the process of adding and removing the layers until we are happy with our architecture.
The thing which you need to take care of is the first layer has the right number of input features which is specified using the input_dim parameter. we can specify the number of neurons as the first argument in a layer. to define activation function use activation argument.
In this example, We will define a fully connected network with three layers. To define the fully connected layer use the Dense class of Keras.
- The first layer has 12 neurons and activation function as relu
- The second hidden layer has 8 neurons and activation function as relu
- Finally, at the output layer, we use 1 unit and activation as sigmoid because it is a binary classification problem.
from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(12, input_dim=8, activation="relu")) model.add(Dense(12, activation="relu")) model.add(Dense(1, activation="sigmoid"))
Remember to specify the right shape of data in the first layer known as the Input layer.
Step-3) Compile The Keras Model
When we compile the Keras model, it uses the backend numerical libraries such as TensorFlow or Theano. Whatever backend you are using automatically chooses the best way to represent the network on your hardware such as CPU, GPU, or TPU.
When we are compiling the model we must specify some additional parameters to better evaluate the model and to find the best set of weights to map inputs to outputs.
- Loss Function – one must specify the loss function to evaluate the set of weights on which model will be mapped. we will use cross-entropy as a loss function which is actually known as binary cross-entropy used for binary classification.
- Optimizer – second is the optimizer to optimize the loss. we will use adam which is a popular version of gradient descent and gives the best result in most problems.
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
Step-4) Start Training (Fit the Model)
After successful compilation of the model, we are ready to fit data to the model and start training the neural network. Along with providing data to model, we need to define a number of epochs and batch size over which training occurs.
- Epoch – one single pass through all the rows in the training dataset
- Batch size – number of samples considered by the model before updating the weights.
model.fit(x,y, epochs=150, batch_size=10)
One epoch can be comprised of more than one batch. These parameters are finally decided after the heat and trial method.
Step-5) Evaluate the Model
After training the model let’s know the performance of a neural network. Model is always evaluated on a test set, In this example for sake of simplicity we have trained on a complete dataset but while working on any project you basically split the data and train the network.
_, accuracy = model.evaluate(x, y) print("Model accuracy: %.2f"% (accuracy*100))
To evaluate the model use the evaluate method and pass the input and output to the model and check the performance.
Step-6) Making Predictions
predict the output of new data by simply using predict method. we have a binary classification problem statement so the output will simply be 0 or 1.
predictions = model.predict(x) print([round(x) for x in predictions])
Alternatively, you can also use the predict_classes function to directly predict the classes.
That’s solved, we have easily made a neural network with 3 layers using only a few lines of code with Keras.
Compiling all code together
model = Sequential() #define model model.add(Dense(12, input_dim=8, activation="relu")) model.add(Dense(8, activation="relu")) model.add(Dense(1, activation="sigmoid")) model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]) #compile model model.fit(x,y, epochs=150, batch_size=10) #training _, accuracy = model.evaluate(x,y) #testing print("Model accuracy: %.2f"% (accuracy*100)) predictions = model.predict(x) #make predictions #round the prediction rounded = [round(x) for x in predictions]
A neural network builds a network of connected layers with multiple neurons in each layer. As we increase the number of layers the network is capable to learn more complex features.
You have easily build your first Neural Network model using Keras. I hope it was easy to catch all the things, If you have any queries please comment it down. I will happy to help you out.
If you like the article, Please have a look at my other articles. link