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.
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.
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.
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.
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.
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.
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.
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.
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
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.
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.
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.
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[0]) 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.
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[0]) 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
Hello Raghav Agrawal, Thank for your informative article on Developing first deep learning model in Python with Keras. I am working in the field of audio/music measurements and need to develop a deep learning model to estimate pitch profile of input music signal. Others have done it, but I too need to develop for my own signals and verify. Can you on your behalf or on behalf of your organization help me develop such a system. Will pay the required fees/consultancy charges to you or to you organization. Will be thankful for your immediate response. Ashok Dhingra. email: [email protected]