In this tutorial, we will focus on Keras basics and learn neural network implementation using Keras.
Keras is a widely used open-source deep-learning library for building neural network models. Keras offers a modular, easy to learn, easy to use, and faster prototype development framework. It is a higher-level wrapper of Tensorflow, CTNK, and Theano libraries. In the previous tutorial, we have seen Introduction to Artificial Neural Network and Multi-Layer Perceptron Neural Network using Python (using Scikit-learn). It’s time to jump on one of the best deep learning library Keras.
In this tutorial, we are going to cover the following topics:
Keras is a high-level deep learning python library for developing neural network models. Keras is a high-level API wrapper. It can run on top of the Tensorflow, CTNK, and Theano library. Keras is developed for the easy and fast development of neural network models.
Keras offers the following benefits:
Keras offers the following limitations:
import numpy as np
import pandas as pd
# Import scikit-learn modules
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
# Import keras modules
import keras
from keras.models import Sequential
from keras.layers import Dense
Let’s first load the required HR dataset using pandas’ read CSV function. You can download data from the following link:
import numpy as np
import pandas as pd
# Load data
data=pd.read_csv('HR_comma_sep.csv')
data.head()
Output:
Lots of machine learning algorithms require numerical input data, so you need to represent categorical columns in a numerical column. In order to encode this data, you could map each value to a number. e.g. Salary column’s value can be represented as low:0, medium:1, and high:2. This process is known as label encoding. In sklearn, we can do this using LabelEncoder.
# Import LabelEncoder
from sklearn import preprocessing
# Creating labelEncoder
le = preprocessing.LabelEncoder()
# Converting string labels into numbers.
data['salary']=le.fit_transform(data['salary'])
data['Departments ']=le.fit_transform(data['Departments '])
# Convert dataframes into numpy array
X = X.values
y = y.values
# Scaling features
sc = StandardScaler()
X = sc.fit_transform(X)
Here, we imported the preprocessing module and created the Label Encoder object. Using this LabelEncoder object you fit and transform the “salary” and “Departments “ column into the numeric column.
In order to assess the model performance, we need to divide the dataset into a training set and a test set. Let’s split dataset by using function train_test_split(). you need to pass basically 3 parameters features, target, and test_set size.
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 70% training and 30% test
# Neural network
model = Sequential()
# Input layers
model.add(Dense(6, input_dim=9, activation='relu'))
# hidden layers
model.add(Dense(4, activation='relu'))
# Output Layer
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, batch_size=64)
Epoch 1/100 188/188 [==============================] - 0s 1ms/step - loss: 0.6331 - accuracy: 0.6910 Epoch 2/100 188/188 [==============================] - 0s 900us/step - loss: 0.4697 - accuracy: 0.8192 Epoch 3/100 188/188 [==============================] - 0s 982us/step - loss: 0.3503 - accuracy: 0.8661 Epoch 4/100 188/188 [==============================] - 0s 814us/step - loss: 0.2707 - accuracy: 0.8983 Epoch 5/100 188/188 [==============================] - 0s 761us/step - loss: 0.2256 - accuracy: 0.9233 Epoch 6/100 188/188 [==============================] - 0s 651us/step - loss: 0.2011 - accuracy: 0.9370 Epoch 7/100 188/188 [==============================] - 0s 661us/step - loss: 0.1882 - accuracy: 0.9456 Epoch 8/100 188/188 [==============================] - 0s 766us/step - loss: 0.1808 - accuracy: 0.9487 Epoch 9/100 188/188 [==============================] - 0s 994us/step - loss: 0.1762 - accuracy: 0.9493 Epoch 10/100 188/188 [==============================] - 0s 923us/step - loss: 0.1731 - accuracy: 0.9498 Epoch 11/100 188/188 [==============================] - 0s 936us/step - loss: 0.1706 - accuracy: 0.9516 Epoch 12/100 188/188 [==============================] - 0s 821us/step - loss: 0.1690 - accuracy: 0.9519 Epoch 13/100 188/188 [==============================] - 0s 679us/step - loss: 0.1675 - accuracy: 0.9523 Epoch 14/100 188/188 [==============================] - 0s 844us/step - loss: 0.1662 - accuracy: 0.9524 Epoch 15/100 188/188 [==============================] - 0s 679us/step - loss: 0.1647 - accuracy: 0.9527 Epoch 16/100 188/188 [==============================] - 0s 969us/step - loss: 0.1640 - accuracy: 0.9524 Epoch 17/100 188/188 [==============================] - 0s 728us/step - loss: 0.1630 - accuracy: 0.9527 Epoch 18/100 188/188 [==============================] - 0s 546us/step - loss: 0.1624 - accuracy: 0.9533 Epoch 19/100 188/188 [==============================] - 0s 567us/step - loss: 0.1615 - accuracy: 0.9536 Epoch 20/100 188/188 [==============================] - 0s 530us/step - loss: 0.1610 - accuracy: 0.9544 Epoch 21/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1604 - accuracy: 0.9535 Epoch 22/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1597 - accuracy: 0.9544 Epoch 23/100 188/188 [==============================] - 0s 762us/step - loss: 0.1591 - accuracy: 0.9546 Epoch 24/100 188/188 [==============================] - 0s 776us/step - loss: 0.1584 - accuracy: 0.9546 Epoch 25/100 188/188 [==============================] - 0s 790us/step - loss: 0.1579 - accuracy: 0.9542 Epoch 26/100 188/188 [==============================] - 0s 568us/step - loss: 0.1575 - accuracy: 0.9552 Epoch 27/100 188/188 [==============================] - 0s 485us/step - loss: 0.1570 - accuracy: 0.9543 Epoch 28/100 188/188 [==============================] - 0s 445us/step - loss: 0.1563 - accuracy: 0.9557 Epoch 29/100 188/188 [==============================] - 0s 484us/step - loss: 0.1560 - accuracy: 0.9560 Epoch 30/100 188/188 [==============================] - 0s 487us/step - loss: 0.1552 - accuracy: 0.9561 Epoch 31/100 188/188 [==============================] - 0s 567us/step - loss: 0.1549 - accuracy: 0.9565 Epoch 32/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1543 - accuracy: 0.9557 Epoch 33/100 188/188 [==============================] - 0s 779us/step - loss: 0.1541 - accuracy: 0.9561 Epoch 34/100 188/188 [==============================] - 0s 706us/step - loss: 0.1538 - accuracy: 0.9567 Epoch 35/100 188/188 [==============================] - 0s 682us/step - loss: 0.1532 - accuracy: 0.9568 Epoch 36/100 188/188 [==============================] - 0s 629us/step - loss: 0.1528 - accuracy: 0.9566 Epoch 37/100 188/188 [==============================] - 0s 607us/step - loss: 0.1523 - accuracy: 0.9572 Epoch 38/100 188/188 [==============================] - 0s 640us/step - loss: 0.1520 - accuracy: 0.9575 Epoch 39/100 188/188 [==============================] - 0s 561us/step - loss: 0.1516 - accuracy: 0.9575 Epoch 40/100 188/188 [==============================] - 0s 774us/step - loss: 0.1512 - accuracy: 0.9576 Epoch 41/100 188/188 [==============================] - 0s 897us/step - loss: 0.1512 - accuracy: 0.9575 Epoch 42/100 188/188 [==============================] - 0s 2ms/step - loss: 0.1506 - accuracy: 0.9574 Epoch 43/100 188/188 [==============================] - 0s 848us/step - loss: 0.1503 - accuracy: 0.9580 Epoch 44/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1501 - accuracy: 0.9569 Epoch 45/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1495 - accuracy: 0.9577 Epoch 46/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1494 - accuracy: 0.9576 Epoch 47/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1492 - accuracy: 0.9576 Epoch 48/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1487 - accuracy: 0.9572 Epoch 49/100 188/188 [==============================] - 0s 958us/step - loss: 0.1484 - accuracy: 0.9580 Epoch 50/100 188/188 [==============================] - 0s 936us/step - loss: 0.1480 - accuracy: 0.9579 Epoch 51/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1479 - accuracy: 0.9577 Epoch 52/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1476 - accuracy: 0.9579 Epoch 53/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1473 - accuracy: 0.9584 Epoch 54/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1471 - accuracy: 0.9586 Epoch 55/100 188/188 [==============================] - 0s 757us/step - loss: 0.1470 - accuracy: 0.9581 Epoch 56/100 188/188 [==============================] - 0s 752us/step - loss: 0.1468 - accuracy: 0.9584 Epoch 57/100 188/188 [==============================] - 0s 875us/step - loss: 0.1464 - accuracy: 0.9587 Epoch 58/100 188/188 [==============================] - 0s 797us/step - loss: 0.1459 - accuracy: 0.9578 Epoch 59/100 188/188 [==============================] - 0s 774us/step - loss: 0.1461 - accuracy: 0.9578 Epoch 60/100 188/188 [==============================] - 0s 784us/step - loss: 0.1456 - accuracy: 0.9592 Epoch 61/100 188/188 [==============================] - 0s 955us/step - loss: 0.1453 - accuracy: 0.9582 Epoch 62/100 188/188 [==============================] - 0s 979us/step - loss: 0.1450 - accuracy: 0.9587 Epoch 63/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1450 - accuracy: 0.9581 Epoch 64/100 188/188 [==============================] - 0s 818us/step - loss: 0.1449 - accuracy: 0.9585 Epoch 65/100 188/188 [==============================] - 0s 796us/step - loss: 0.1446 - accuracy: 0.9583 Epoch 66/100 188/188 [==============================] - 0s 799us/step - loss: 0.1444 - accuracy: 0.9589 Epoch 67/100 188/188 [==============================] - 0s 783us/step - loss: 0.1444 - accuracy: 0.9590 Epoch 68/100 188/188 [==============================] - 0s 795us/step - loss: 0.1441 - accuracy: 0.9592 Epoch 69/100 188/188 [==============================] - 0s 893us/step - loss: 0.1438 - accuracy: 0.9585 Epoch 70/100 188/188 [==============================] - 0s 799us/step - loss: 0.1437 - accuracy: 0.9588 Epoch 71/100 188/188 [==============================] - 0s 742us/step - loss: 0.1434 - accuracy: 0.9585 Epoch 72/100 188/188 [==============================] - 0s 843us/step - loss: 0.1433 - accuracy: 0.9590 Epoch 73/100 188/188 [==============================] - 0s 799us/step - loss: 0.1433 - accuracy: 0.9586 Epoch 74/100 188/188 [==============================] - 0s 811us/step - loss: 0.1429 - accuracy: 0.9584 Epoch 75/100 188/188 [==============================] - 0s 791us/step - loss: 0.1429 - accuracy: 0.9589 Epoch 76/100 188/188 [==============================] - 0s 765us/step - loss: 0.1426 - accuracy: 0.9587 Epoch 77/100 188/188 [==============================] - 0s 737us/step - loss: 0.1426 - accuracy: 0.9587 Epoch 78/100 188/188 [==============================] - 0s 827us/step - loss: 0.1423 - accuracy: 0.9585 Epoch 79/100 188/188 [==============================] - 0s 811us/step - loss: 0.1421 - accuracy: 0.9591 Epoch 80/100 188/188 [==============================] - 0s 845us/step - loss: 0.1419 - accuracy: 0.9588 Epoch 81/100 188/188 [==============================] - 0s 938us/step - loss: 0.1420 - accuracy: 0.9596 Epoch 82/100 188/188 [==============================] - 0s 747us/step - loss: 0.1418 - accuracy: 0.9594 Epoch 83/100 188/188 [==============================] - 0s 556us/step - loss: 0.1418 - accuracy: 0.9583 Epoch 84/100 188/188 [==============================] - 0s 479us/step - loss: 0.1418 - accuracy: 0.9592 Epoch 85/100 188/188 [==============================] - 0s 469us/step - loss: 0.1415 - accuracy: 0.9589 Epoch 86/100 188/188 [==============================] - 0s 702us/step - loss: 0.1415 - accuracy: 0.9590 Epoch 87/100 188/188 [==============================] - 0s 907us/step - loss: 0.1411 - accuracy: 0.9587 Epoch 88/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1410 - accuracy: 0.9589 Epoch 89/100 188/188 [==============================] - 0s 951us/step - loss: 0.1413 - accuracy: 0.9580 Epoch 90/100 188/188 [==============================] - 0s 768us/step - loss: 0.1411 - accuracy: 0.9592 Epoch 91/100 188/188 [==============================] - 0s 967us/step - loss: 0.1406 - accuracy: 0.9587 Epoch 92/100 188/188 [==============================] - 0s 615us/step - loss: 0.1412 - accuracy: 0.9593 Epoch 93/100 188/188 [==============================] - 0s 1ms/step - loss: 0.1405 - accuracy: 0.9595 Epoch 94/100 188/188 [==============================] - 0s 965us/step - loss: 0.1404 - accuracy: 0.9592 Epoch 95/100 188/188 [==============================] - 0s 617us/step - loss: 0.1404 - accuracy: 0.9587 Epoch 96/100 188/188 [==============================] - 0s 615us/step - loss: 0.1403 - accuracy: 0.9586 Epoch 97/100 188/188 [==============================] - 0s 623us/step - loss: 0.1403 - accuracy: 0.9593 Epoch 98/100 188/188 [==============================] - 0s 605us/step - loss: 0.1401 - accuracy: 0.9591 Epoch 99/100 188/188 [==============================] - 0s 602us/step - loss: 0.1401 - accuracy: 0.9588 Epoch 100/100 188/188 [==============================] - 0s 714us/step - loss: 0.1401 - accuracy: 0.9591
Out[10]:
score = model.evaluate(X_test, y_test,verbose=1)
print(score) #loss and accuracy
94/94 [==============================] - 0s 423us/step - loss: 0.1316 - accuracy: 0.9620 [0.1315860152244568, 0.9620000123977661]
In this section, we are covering the parameters that need to be hyper tune at the time of model building. We will learn the importance of each parameter in this section but we will see experimentation using all these parameters in upcoming articles.
Here is the list of parameters that need to tune during model training:
Congratulations, you have made it to the end of this tutorial!
In this tutorial, we have discussed Keras library, workflow, components, benefits, and limitations. Also, we have built the classifier model for employee churn using the Neural Network classification model with Keras library in python.
In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…
Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…
In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…
Analyze employee churn, Why employees are leaving the company, and How to predict, who will…
Airflow operators are core components of any workflow defined in airflow. The operator represents a…
Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…