Keras Deep Learning Tutorial: Build A Good Model in 5 Steps

Keras Deep Learning Tutorial

Deep Learning is the advanced feature of Machine Learning. There are many use cases of deep learning you will see in your daily life. Some of them are Face recognition, language translation, and speech recognition. In addition, there are also various Python Packages for building your deep learning model like Tensorflow, Keras e.t.c. It’s great. But In this entire intuition, you will know how to build your own deep learning model using the popular framework Keras.

A Brief Introduction about Keras

It allows you to write a deep learning code with just only in a few lines. As it is a High Framework, Built on the top of TensorFlow and Theano framework. It means Keras act as a front end and TensorFlow or Theano as a Backend.  Whenever you build a Deep Learning model using Keras, then in background the neural network is built on the TensorFlow or Theano. Thus you can use your built model with any tools that support TensorFlow and Keras. In this tutorial, I will use Tensorflow for the model building.

Step By Step to creating A Neural Network in Keras

In this section, you will know how to build a supervised deep learning model using Keras. If you don’t have an idea of Supervised Machine Learning model then you can read the post.

Step 1: Import the necessary Libraries

The first step is to import all libraries that are required in the tutorial.
I have used pandas for reading the CSV file, numpy for testing the model, Keras for modeling the deep learning neural network.

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import *

Step 2: Load the dataset

The next step is to load the dataset for creating the model. For this, you will use the panda’s library. For the x value, I am taking values for all the column from “mpg” to “qsec” . The target is the “vs ” column.

read the csv file deep learning

cars = pd.read_csv("mtcars.csv")
cars.head()
X = cars.iloc[:32, 1:8].values
y = cars.iloc[:32, [8]].values

Step 3: Define the model

After the pre-processing of the dataset, the next task is to create a model for the prediction. To build the model in keras you have to decide how many layers should be, how many nodes should be in each and how each layer is connected to each other. If you try to build the bigger model then it can lead to overfitting of the data. Thus the easiest way is to build the model is to to create the model using the Keras sequential API. Use the following codes

model = Sequential()
model.add(Dense(50, input_dim=7, activation="relu"))
model.add(Dense(100, activation="relu"))
model.add(Dense(50, activation="relu"))
model.add(Dense(1, activation="linear"))
model.compile(loss="mean_squared_error", optimizer="adam")

The method model.add() create the layer ad Dense() method accepts the various arguments like how many nodes , activation function type like linear,relu et.c . In this example, I am using the rectifier linear unit (relu) for the input and the mid-layers but for the output using the Linear activation function. Input_dim is input dimensions that is the number of input. Here it is 7.

Step 4: Compile and Train the Model

After defining the number of layers and activation function the next step is to compile and train the model, The model.compile() method compile the model with the loss (mean_squared_error) and the optimizer(adam) as the arguments.
Then you will fit the compiled model to the X and y. To do so use the model.fit() method with the epochs and verbose as an additional parameter. Use the following code.

model.compile(loss="mean_squared_error", optimizer="adam")

# Train the model
model.fit(x=X
          , y=y
          , epochs=50
          , shuffle=True
          , verbose=2)

Step 5: Test the Results

The last step is to test the model. Please note that if the dataset size is small then you should not split the data rather than the first train the entire dataset and then test on some input. If the root means squared value and the output is close to the real output then you can consider the model is good. Like in this example I am taking input manually and predicting the output and the output is negative that is predicting correctly. And also the root means the squared error is low.

test_x = np.array([[ 15,8,301,335,3.54,3.57,14.6]])
predict = model.predict(test_x)
print(predict)

End Notes

Deep Learning is the advancement of Machine Learning. The Predictions accuracy is more than that of the machine learning algorithms. But it requires resources for complex computation. You can easily train and build a model using your CPU for a basic problem. But for the complex problem, you have to use the GPU for training.

We hope you have understood this intuition. If you have any query about it you can contact us or message on the Data Science Learner Page. We are always ready to help you.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner