Assignment - 5 (ANN)

Neural network using Keras

Author: Jimut Bahan Pal

Question


Import the Iris dataset and divide the data into training and test datasets.

a) Train a 2-layered neural network (1 hidden layer) model on the data. Employ Batch normalization and initialize the weights using the Xaviers method with normal distribution. Make predictions on the test data and compute the accuracy.

b) Vary the number of neurons in the hidden layer and show the impact on accuracy.

c) Take (one of) the case which has best accuracy and employ dropout with different values of dropout rate (say from 0.1 to 0.7). Plot the variation of accuracy versus dropout rate.

d) Now increase the number of hidden layers (with same no. of neurons) and show the variation of accuracy with respect to the number of hidden layers. You can select the number of neurons as determined best in part b)

e) Using the setting as determined best in part b), train the model with a few different optimization algorithms (with default settings) and print the accuracy from each case. You may try adam, adagrad, adadelta.

Hint (python): from keras.models import Sequential from keras.layers import Dense

model = Sequential() model.add(Dense(5, input_dim=2, activation='relu')) model.add(Dense(4, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Loading the necessary libraries

In [1]:
import pandas as pd
import numpy as np
from sklearn import datasets
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.datasets import load_iris
from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.optimizers import SGD,Adam
from keras import optimizers
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.python.keras.utils import np_utils
from keras.layers.normalization import BatchNormalization
from keras.utils.vis_utils import plot_model  #plotting the model structure
Using TensorFlow backend.

Loading the iris data

In [2]:
iris = datasets.load_iris()
X_data = iris.data[:,0:4].astype(float)  # 4-features
Y_data = iris.target # labels (3 classes)
In [3]:
encoder = LabelEncoder()
In [4]:
encoder.fit(Y_data)
Out[4]:
LabelEncoder()
In [5]:
encoded_Y = encoder.transform(Y_data)
In [6]:
dummy_y = np_utils.to_categorical(encoded_Y)

Dividing the data into training and test dataset

In [7]:
#dummy_y
# set aside 20% of train and test data for evaluation
X_train, X_test, y_train, y_test = train_test_split(X_data, dummy_y,
    test_size=0.2, shuffle = True, random_state = 123)
print("X_train shape: {}".format(X_train.shape))
print("X_test shape: {}".format(X_test.shape))
print("y_train shape: {}".format(y_train.shape))
print("y_test shape: {}".format(y_test.shape))
X_train shape: (120, 4)
X_test shape: (30, 4)
y_train shape: (120, 3)
y_test shape: (30, 3)
In [8]:
print(X_train[:10])
[[7.4 2.8 6.1 1.9]
 [6.  2.2 5.  1.5]
 [4.7 3.2 1.6 0.2]
 [5.1 3.5 1.4 0.2]
 [6.  2.2 4.  1. ]
 [5.  2.3 3.3 1. ]
 [7.9 3.8 6.4 2. ]
 [5.4 3.9 1.7 0.4]
 [5.4 3.9 1.3 0.4]
 [5.8 2.7 3.9 1.2]]
In [9]:
# get reproducible results:: 99.17%
# Seed value
# Apparently you may use different seed values at each stage
seed_value= 0
import tensorflow as tf
# 1. Set the `PYTHONHASHSEED` environment variable at a fixed value
import os
os.environ['PYTHONHASHSEED']=str(seed_value)

# 2. Set the `python` built-in pseudo-random generator at a fixed value
import random
random.seed(seed_value)

# 3. Set the `numpy` pseudo-random generator at a fixed value
import numpy as np
np.random.seed(seed_value)

# 4. Set the `tensorflow` pseudo-random generator at a fixed value
import tensorflow as tf
tf.random.set_seed(seed_value)
# for later versions: 
# tf.compat.v1.set_random_seed(seed_value)


# for later versions:
# session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
# sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
# tf.compat.v1.keras.backend.set_session(sess)

a)

Train a 2-layered neural network (1 hidden layer) model on the data.

Employ Batch normalization and initialize the weights using the Xaviers method with normal distribution.

Make predictions on the test data and compute the accuracy.

In [10]:
#Defining the model 

# Seq approach
model = Sequential()
# input dimension is 4 since there are 4 features (input layer)
model.add(Dense(4, input_dim=4, activation='softmax'))
# 1st hidden layer
# initialising the weights using Xavier's Method
model.add(Dense(10, activation='softmax',kernel_initializer = 'glorot_normal'))
model.add(BatchNormalization())
# 2nd layer is output layer
model.add(Dense(3, activation='softmax'))
# output layer
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 4)                 20        
_________________________________________________________________
dense_2 (Dense)              (None, 10)                50        
_________________________________________________________________
batch_normalization_1 (Batch (None, 10)                40        
_________________________________________________________________
dense_3 (Dense)              (None, 3)                 33        
=================================================================
Total params: 143
Trainable params: 123
Non-trainable params: 20
_________________________________________________________________
In [11]:
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True,dpi=60)
Out[11]:
In [12]:
#fitting the model and predicting 
hist = model.fit(X_train,y_train,epochs=100)
Epoch 1/100
120/120 [==============================] - 3s 29ms/step - loss: 1.1122 - accuracy: 0.2583
Epoch 2/100
120/120 [==============================] - 0s 843us/step - loss: 1.1089 - accuracy: 0.2583
Epoch 3/100
120/120 [==============================] - 0s 224us/step - loss: 1.1058 - accuracy: 0.2917
Epoch 4/100
120/120 [==============================] - 0s 306us/step - loss: 1.1036 - accuracy: 0.3250
Epoch 5/100
120/120 [==============================] - 0s 839us/step - loss: 1.1018 - accuracy: 0.3333
Epoch 6/100
120/120 [==============================] - 0s 542us/step - loss: 1.0998 - accuracy: 0.3583
Epoch 7/100
120/120 [==============================] - 0s 317us/step - loss: 1.0984 - accuracy: 0.3583
Epoch 8/100
120/120 [==============================] - 0s 193us/step - loss: 1.0973 - accuracy: 0.3583
Epoch 9/100
120/120 [==============================] - 0s 425us/step - loss: 1.0962 - accuracy: 0.3583
Epoch 10/100
120/120 [==============================] - ETA: 0s - loss: 1.0947 - accuracy: 0.34 - 0s 489us/step - loss: 1.0941 - accuracy: 0.3667
Epoch 11/100
120/120 [==============================] - 0s 1ms/step - loss: 1.0927 - accuracy: 0.3667
Epoch 12/100
120/120 [==============================] - 0s 213us/step - loss: 1.0925 - accuracy: 0.3667
Epoch 13/100
120/120 [==============================] - 0s 498us/step - loss: 1.0904 - accuracy: 0.3667
Epoch 14/100
120/120 [==============================] - 0s 186us/step - loss: 1.0888 - accuracy: 0.3667
Epoch 15/100
120/120 [==============================] - 0s 197us/step - loss: 1.0863 - accuracy: 0.3667
Epoch 16/100
120/120 [==============================] - 0s 180us/step - loss: 1.0843 - accuracy: 0.3667
Epoch 17/100
120/120 [==============================] - 0s 436us/step - loss: 1.0809 - accuracy: 0.3750
Epoch 18/100
120/120 [==============================] - 0s 329us/step - loss: 1.0765 - accuracy: 0.4083
Epoch 19/100
120/120 [==============================] - 0s 910us/step - loss: 1.0725 - accuracy: 0.4667
Epoch 20/100
120/120 [==============================] - 0s 281us/step - loss: 1.0652 - accuracy: 0.5250
Epoch 21/100
120/120 [==============================] - 0s 331us/step - loss: 1.0582 - accuracy: 0.5750
Epoch 22/100
120/120 [==============================] - 0s 357us/step - loss: 1.0480 - accuracy: 0.6000
Epoch 23/100
120/120 [==============================] - 0s 307us/step - loss: 1.0372 - accuracy: 0.6250
Epoch 24/100
120/120 [==============================] - 0s 378us/step - loss: 1.0228 - accuracy: 0.6333
Epoch 25/100
120/120 [==============================] - 0s 442us/step - loss: 1.0029 - accuracy: 0.6500
Epoch 26/100
120/120 [==============================] - 0s 618us/step - loss: 0.9815 - accuracy: 0.6583
Epoch 27/100
120/120 [==============================] - 0s 514us/step - loss: 0.9555 - accuracy: 0.6667
Epoch 28/100
120/120 [==============================] - 0s 586us/step - loss: 0.9226 - accuracy: 0.6667
Epoch 29/100
120/120 [==============================] - 0s 531us/step - loss: 0.8930 - accuracy: 0.6667
Epoch 30/100
120/120 [==============================] - 0s 853us/step - loss: 0.8506 - accuracy: 0.6750
Epoch 31/100
120/120 [==============================] - 0s 351us/step - loss: 0.8196 - accuracy: 0.6500
Epoch 32/100
120/120 [==============================] - 0s 504us/step - loss: 0.7747 - accuracy: 0.6750
Epoch 33/100
120/120 [==============================] - 0s 478us/step - loss: 0.7457 - accuracy: 0.6667
Epoch 34/100
120/120 [==============================] - 0s 712us/step - loss: 0.6936 - accuracy: 0.6750
Epoch 35/100
120/120 [==============================] - 0s 413us/step - loss: 0.6686 - accuracy: 0.6667
Epoch 36/100
120/120 [==============================] - 0s 538us/step - loss: 0.6367 - accuracy: 0.6667
Epoch 37/100
120/120 [==============================] - 0s 467us/step - loss: 0.6148 - accuracy: 0.6750
Epoch 38/100
120/120 [==============================] - 0s 744us/step - loss: 0.6099 - accuracy: 0.6500
Epoch 39/100
120/120 [==============================] - 0s 571us/step - loss: 0.5804 - accuracy: 0.6500
Epoch 40/100
120/120 [==============================] - 0s 528us/step - loss: 0.5587 - accuracy: 0.6250
Epoch 41/100
120/120 [==============================] - 0s 931us/step - loss: 0.5601 - accuracy: 0.5667
Epoch 42/100
120/120 [==============================] - 0s 557us/step - loss: 0.5554 - accuracy: 0.6250
Epoch 43/100
120/120 [==============================] - 0s 592us/step - loss: 0.5350 - accuracy: 0.6417
Epoch 44/100
120/120 [==============================] - 0s 326us/step - loss: 0.5303 - accuracy: 0.6417
Epoch 45/100
120/120 [==============================] - 0s 572us/step - loss: 0.5248 - accuracy: 0.6500
Epoch 46/100
120/120 [==============================] - 0s 502us/step - loss: 0.5172 - accuracy: 0.6583
Epoch 47/100
120/120 [==============================] - 0s 492us/step - loss: 0.5188 - accuracy: 0.6417
Epoch 48/100
120/120 [==============================] - 0s 593us/step - loss: 0.5093 - accuracy: 0.6750
Epoch 49/100
120/120 [==============================] - 0s 196us/step - loss: 0.5017 - accuracy: 0.6833
Epoch 50/100
120/120 [==============================] - 0s 237us/step - loss: 0.5065 - accuracy: 0.6583
Epoch 51/100
120/120 [==============================] - 0s 427us/step - loss: 0.4991 - accuracy: 0.6833
Epoch 52/100
120/120 [==============================] - 0s 433us/step - loss: 0.5003 - accuracy: 0.6917
Epoch 53/100
120/120 [==============================] - 0s 705us/step - loss: 0.5223 - accuracy: 0.7167
Epoch 54/100
120/120 [==============================] - 0s 324us/step - loss: 0.4876 - accuracy: 0.6917
Epoch 55/100
120/120 [==============================] - 0s 249us/step - loss: 0.4910 - accuracy: 0.7250
Epoch 56/100
120/120 [==============================] - 0s 192us/step - loss: 0.4825 - accuracy: 0.7333
Epoch 57/100
120/120 [==============================] - 0s 340us/step - loss: 0.4894 - accuracy: 0.7583
Epoch 58/100
120/120 [==============================] - 0s 276us/step - loss: 0.4765 - accuracy: 0.7667
Epoch 59/100
120/120 [==============================] - 0s 288us/step - loss: 0.4711 - accuracy: 0.7417
Epoch 60/100
120/120 [==============================] - ETA: 0s - loss: 0.3996 - accuracy: 0.71 - 0s 267us/step - loss: 0.4983 - accuracy: 0.7750
Epoch 61/100
120/120 [==============================] - 0s 344us/step - loss: 0.4762 - accuracy: 0.7583
Epoch 62/100
120/120 [==============================] - 0s 367us/step - loss: 0.4611 - accuracy: 0.7583
Epoch 63/100
120/120 [==============================] - 0s 307us/step - loss: 0.4596 - accuracy: 0.7333
Epoch 64/100
120/120 [==============================] - 0s 250us/step - loss: 0.4695 - accuracy: 0.7667
Epoch 65/100
120/120 [==============================] - 0s 363us/step - loss: 0.4520 - accuracy: 0.7917
Epoch 66/100
120/120 [==============================] - 0s 249us/step - loss: 0.4442 - accuracy: 0.8167
Epoch 67/100
120/120 [==============================] - 0s 272us/step - loss: 0.4462 - accuracy: 0.7917
Epoch 68/100
120/120 [==============================] - 0s 254us/step - loss: 0.4406 - accuracy: 0.8000
Epoch 69/100
120/120 [==============================] - 0s 252us/step - loss: 0.4350 - accuracy: 0.8083
Epoch 70/100
120/120 [==============================] - 0s 271us/step - loss: 0.4329 - accuracy: 0.8167
Epoch 71/100
120/120 [==============================] - 0s 214us/step - loss: 0.4302 - accuracy: 0.8167
Epoch 72/100
120/120 [==============================] - 0s 287us/step - loss: 0.4324 - accuracy: 0.8500
Epoch 73/100
120/120 [==============================] - 0s 264us/step - loss: 0.4181 - accuracy: 0.8250
Epoch 74/100
120/120 [==============================] - 0s 253us/step - loss: 0.4215 - accuracy: 0.8250
Epoch 75/100
120/120 [==============================] - 0s 256us/step - loss: 0.4431 - accuracy: 0.8000
Epoch 76/100
120/120 [==============================] - 0s 287us/step - loss: 0.4352 - accuracy: 0.8167
Epoch 77/100
120/120 [==============================] - 0s 419us/step - loss: 0.4462 - accuracy: 0.8000
Epoch 78/100
120/120 [==============================] - 0s 657us/step - loss: 0.4013 - accuracy: 0.8417
Epoch 79/100
120/120 [==============================] - 0s 434us/step - loss: 0.4048 - accuracy: 0.8583
Epoch 80/100
120/120 [==============================] - 0s 339us/step - loss: 0.4012 - accuracy: 0.8667
Epoch 81/100
120/120 [==============================] - 0s 269us/step - loss: 0.3903 - accuracy: 0.8667
Epoch 82/100
120/120 [==============================] - 0s 292us/step - loss: 0.4045 - accuracy: 0.8583
Epoch 83/100
120/120 [==============================] - 0s 279us/step - loss: 0.3863 - accuracy: 0.8833
Epoch 84/100
120/120 [==============================] - 0s 254us/step - loss: 0.4151 - accuracy: 0.8417
Epoch 85/100
120/120 [==============================] - 0s 271us/step - loss: 0.3861 - accuracy: 0.8667
Epoch 86/100
120/120 [==============================] - 0s 268us/step - loss: 0.3746 - accuracy: 0.9167
Epoch 87/100
120/120 [==============================] - 0s 480us/step - loss: 0.3541 - accuracy: 0.8667
Epoch 88/100
120/120 [==============================] - 0s 359us/step - loss: 0.3508 - accuracy: 0.9000
Epoch 89/100
120/120 [==============================] - 0s 285us/step - loss: 0.3501 - accuracy: 0.9000
Epoch 90/100
120/120 [==============================] - 0s 278us/step - loss: 0.3393 - accuracy: 0.9250
Epoch 91/100
120/120 [==============================] - 0s 343us/step - loss: 0.3547 - accuracy: 0.9167
Epoch 92/100
120/120 [==============================] - 0s 270us/step - loss: 0.3374 - accuracy: 0.9167
Epoch 93/100
120/120 [==============================] - 0s 328us/step - loss: 0.3332 - accuracy: 0.8917
Epoch 94/100
120/120 [==============================] - 0s 145us/step - loss: 0.3166 - accuracy: 0.9083
Epoch 95/100
120/120 [==============================] - 0s 308us/step - loss: 0.3280 - accuracy: 0.9167
Epoch 96/100
120/120 [==============================] - 0s 250us/step - loss: 0.3246 - accuracy: 0.8750
Epoch 97/100
120/120 [==============================] - 0s 367us/step - loss: 0.3043 - accuracy: 0.9167
Epoch 98/100
120/120 [==============================] - 0s 275us/step - loss: 0.3020 - accuracy: 0.9250
Epoch 99/100
120/120 [==============================] - 0s 339us/step - loss: 0.3248 - accuracy: 0.8833
Epoch 100/100
120/120 [==============================] - 0s 290us/step - loss: 0.3547 - accuracy: 0.8333
In [17]:
print(hist.history.keys())
dict_keys(['loss', 'accuracy'])
In [18]:
plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['accuracy']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Accuracy ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper left')
plt.show()

plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['loss']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Loss ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper right')
plt.show()
In [19]:
y_pred = model.predict(X_test)

y_test_class = np.argmax(y_test,axis=1)
y_pred_class = np.argmax(y_pred,axis=1)
In [20]:
print(y_pred)
print(y_test_class)
print(y_pred_class)
[[0.09890788 0.64731497 0.2537772 ]
 [0.04914845 0.58929026 0.36156127]
 [0.03867501 0.5637737  0.3975513 ]
 [0.11557894 0.6496424  0.23477863]
 [0.6503627  0.31497464 0.03466271]
 [0.06067329 0.61122406 0.32810265]
 [0.19335772 0.6398732  0.16676906]
 [0.62054306 0.34025338 0.03920352]
 [0.6223265  0.33880046 0.03887304]
 [0.1037725  0.6507972  0.24543034]
 [0.06039342 0.60723007 0.33237645]
 [0.6732332  0.29544082 0.03132604]
 [0.10907686 0.6496358  0.24128729]
 [0.04907482 0.59123677 0.35968846]
 [0.04779161 0.5852524  0.36695597]
 [0.03474224 0.5498999  0.41535786]
 [0.7077717  0.26564255 0.02658576]
 [0.656733   0.3095983  0.03366867]
 [0.09372076 0.64337194 0.26290736]
 [0.651472   0.31406128 0.03446671]
 [0.6790975  0.2904024  0.03050014]
 [0.07452545 0.630643   0.29483148]
 [0.55858576 0.3916591  0.04975515]
 [0.05973415 0.60986656 0.33039925]
 [0.62442726 0.33689493 0.03867782]
 [0.66258955 0.3045409  0.03286951]
 [0.67073905 0.29757932 0.03168161]
 [0.04395211 0.57989144 0.37615648]
 [0.03018782 0.5287199  0.44109222]
 [0.65192384 0.31364352 0.03443266]]
[1 2 2 1 0 2 1 0 0 1 2 0 1 2 2 2 0 0 1 0 0 2 0 2 0 0 0 2 2 0]
[1 1 1 1 0 1 1 0 0 1 1 0 1 1 1 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0]

b)

Vary the number of neurons in the hidden layer and show the impact on accuracy.

Lets brute force crazily, starting from Number of Neurons in hidden layer = 4

In [22]:
deep_neurons = 50
accuracy_neuron_list = []

for item in range(4,deep_neurons):
    model = Sequential()
    # input dimension is 4 since there are 4 features (input layer)
    model.add(Dense(4, input_dim=4, activation='softmax'))
    # 1st hidden layer
    # initialising the weights using Xavier's Method
    model.add(Dense(item, activation='softmax',kernel_initializer = 'glorot_normal'))
    model.add(BatchNormalization())
    # 2nd layer is output layer
    model.add(Dense(3, activation='softmax'))
    # output layer
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    hist = model.fit(X_train,y_train,epochs=100,verbose=0)
    #model.summary()
    
    print("Hidden Neurons => ",item)
    print("Accuracy => ",hist.history.get('accuracy')[-1])
    accuracy_neuron_list.append([item,hist.history.get('accuracy')[-1]])
    plt.figure(figsize=(7,5))
    legend_acc = []
    for item in hist.history.keys():
        if item.split('_')[-1:] == ['accuracy']:
            #print("Accuracy = ",item)
            legend_acc.append(item)
            plt.plot(hist.history[item])
    plt.ylabel('Accuracy ->')
    plt.xlabel('Epoch ->')
    plt.legend(legend_acc, loc='upper left')
    plt.show()

    plt.figure(figsize=(7,5))
    legend_acc = []
    for item in hist.history.keys():
        if item.split('_')[-1:] == ['loss']:
            #print("Accuracy = ",item)
            legend_acc.append(item)
            plt.plot(hist.history[item])
    plt.ylabel('Loss ->')
    plt.xlabel('Epoch ->')
    plt.legend(legend_acc, loc='upper right')
    plt.show()
Hidden Neurons =>  4
Accuracy =>  0.94166666
Hidden Neurons =>  5
Accuracy =>  0.98333335
Hidden Neurons =>  6
Accuracy =>  0.9583333
Hidden Neurons =>  7
Accuracy =>  0.90833336
Hidden Neurons =>  8
Accuracy =>  0.94166666
Hidden Neurons =>  9
Accuracy =>  0.9583333
Hidden Neurons =>  10
Accuracy =>  0.925
Hidden Neurons =>  11
Accuracy =>  0.98333335
Hidden Neurons =>  12
Accuracy =>  0.9583333
Hidden Neurons =>  13
Accuracy =>  0.9583333
Hidden Neurons =>  14
Accuracy =>  0.9583333
Hidden Neurons =>  15
Accuracy =>  0.975
Hidden Neurons =>  16
Accuracy =>  0.95
Hidden Neurons =>  17
Accuracy =>  0.9166667
Hidden Neurons =>  18
Accuracy =>  0.9916667
Hidden Neurons =>  19
Accuracy =>  0.94166666
Hidden Neurons =>  20
Accuracy =>  0.975
Hidden Neurons =>  21
Accuracy =>  0.975
Hidden Neurons =>  22
Accuracy =>  0.98333335
Hidden Neurons =>  23
Accuracy =>  0.9583333
Hidden Neurons =>  24
Accuracy =>  0.975
Hidden Neurons =>  25
Accuracy =>  0.9
Hidden Neurons =>  26
Accuracy =>  0.975
Hidden Neurons =>  27
Accuracy =>  0.9583333
Hidden Neurons =>  28
Accuracy =>  0.96666664
Hidden Neurons =>  29
Accuracy =>  0.9166667
Hidden Neurons =>  30
Accuracy =>  0.95
Hidden Neurons =>  31
Accuracy =>  0.975
Hidden Neurons =>  32
Accuracy =>  0.95
Hidden Neurons =>  33
Accuracy =>  0.90833336
Hidden Neurons =>  34
Accuracy =>  0.9583333
Hidden Neurons =>  35
Accuracy =>  0.95
Hidden Neurons =>  36
Accuracy =>  0.98333335
Hidden Neurons =>  37
Accuracy =>  0.94166666
Hidden Neurons =>  38
Accuracy =>  0.95
Hidden Neurons =>  39
Accuracy =>  0.9583333
Hidden Neurons =>  40
Accuracy =>  0.9583333
Hidden Neurons =>  41
Accuracy =>  0.94166666
Hidden Neurons =>  42
Accuracy =>  0.925
Hidden Neurons =>  43
Accuracy =>  0.975
Hidden Neurons =>  44
Accuracy =>  0.94166666
Hidden Neurons =>  45
Accuracy =>  0.975
Hidden Neurons =>  46
Accuracy =>  0.98333335
Hidden Neurons =>  47
Accuracy =>  0.925
Hidden Neurons =>  48
Accuracy =>  0.95
Hidden Neurons =>  49
Accuracy =>  0.975
In [27]:
print(accuracy_neuron_list)
[[4, 0.94166666], [5, 0.98333335], [6, 0.9583333], [7, 0.90833336], [8, 0.94166666], [9, 0.9583333], [10, 0.925], [11, 0.98333335], [12, 0.9583333], [13, 0.9583333], [14, 0.9583333], [15, 0.975], [16, 0.95], [17, 0.9166667], [18, 0.9916667], [19, 0.94166666], [20, 0.975], [21, 0.975], [22, 0.98333335], [23, 0.9583333], [24, 0.975], [25, 0.9], [26, 0.975], [27, 0.9583333], [28, 0.96666664], [29, 0.9166667], [30, 0.95], [31, 0.975], [32, 0.95], [33, 0.90833336], [34, 0.9583333], [35, 0.95], [36, 0.98333335], [37, 0.94166666], [38, 0.95], [39, 0.9583333], [40, 0.9583333], [41, 0.94166666], [42, 0.925], [43, 0.975], [44, 0.94166666], [45, 0.975], [46, 0.98333335], [47, 0.925], [48, 0.95], [49, 0.975]]
In [28]:
#accuracy_neuron_list
print("No. of Hidden Neurons selected => ", 18)
No. of Hidden Neurons selected =>  18
In [29]:
print("Max accuracy got =>",max(second))
Max accuracy got => 0.9916667
In [30]:
first = [i[0] for i in accuracy_neuron_list]
second = [i[1] for i in accuracy_neuron_list]
fig, ax = plt.subplots()
ax.plot(first, second,color='red')

ax.set(xlabel='No. of Hidden Neurons', ylabel='Accuracy',
       title='Variation of Accuracy with No. of Hidden Neurons')
ax.grid()
plt.show()

So we have brute forced and got number of neuron = 18

c)

Take (one of) the case which has best accuracy and employ dropout with different values of dropout rate (say from 0.1 to 0.7). Plot the variation of accuracy versus dropout rate.

In [31]:
dropout_acc_list = []
for dropout_ in range(1,8):
    dropout = dropout_/10
    #print(dropout)
    #Defining the model 

    # Seq approach
    model = Sequential()
    # input dimension is 4 since there are 4 features (input layer)
    model.add(Dense(4, input_dim=4, activation='softmax'))
    # 1st hidden layer
    # initialising the weights using Xavier's Method
    model.add(Dense(18, activation='softmax',kernel_initializer = 'glorot_normal'))
    model.add(Dropout(dropout))
    model.add(BatchNormalization())
    # 2nd layer is output layer
    model.add(Dense(3, activation='softmax'))
    # output layer
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    #model.summary()
    hist = model.fit(X_train,y_train,epochs=100,verbose=0)
    print("Dropout => ",dropout)
    print("Accuracy => ",hist.history.get('accuracy')[-1])
    dropout_acc_list.append([dropout,hist.history.get('accuracy')[-1]])
    plt.figure(figsize=(7,5))
    legend_acc = []
    for item in hist.history.keys():
        if item.split('_')[-1:] == ['accuracy']:
            #print("Accuracy = ",item)
            legend_acc.append(item)
            plt.plot(hist.history[item])
    plt.ylabel('Accuracy ->')
    plt.xlabel('Epoch ->')
    plt.legend(legend_acc, loc='upper left')
    plt.show()

    plt.figure(figsize=(7,5))
    legend_acc = []
    for item in hist.history.keys():
        if item.split('_')[-1:] == ['loss']:
            #print("Accuracy = ",item)
            legend_acc.append(item)
            plt.plot(hist.history[item])
    plt.ylabel('Loss ->')
    plt.xlabel('Epoch ->')
    plt.legend(legend_acc, loc='upper right')
    plt.show()
Dropout =>  0.1
Accuracy =>  0.69166666
Dropout =>  0.2
Accuracy =>  0.7083333
Dropout =>  0.3
Accuracy =>  0.65
Dropout =>  0.4
Accuracy =>  0.69166666
Dropout =>  0.5
Accuracy =>  0.7
Dropout =>  0.6
Accuracy =>  0.55833334
Dropout =>  0.7
Accuracy =>  0.425
In [32]:
dropout_acc_list
Out[32]:
[[0.1, 0.69166666],
 [0.2, 0.7083333],
 [0.3, 0.65],
 [0.4, 0.69166666],
 [0.5, 0.7],
 [0.6, 0.55833334],
 [0.7, 0.425]]
In [34]:
first_d = [i[0] for i in dropout_acc_list]
second_d = [i[1] for i in dropout_acc_list]
fig, ax = plt.subplots()
ax.plot(first_d, second_d,color='green')

ax.set(xlabel='No. of Hidden Neurons', ylabel='Accuracy',
       title='Variation of Accuracy with Dropout')
#ax.grid()
plt.show()

Conclusion: Don't add Dropout to such a simple model, it is degrading

d)

Now increase the number of hidden layers (with same no. of neurons) and show the variation of accuracy with respect to the number of hidden layers. You can select the number of neurons as determined best in part b)

In [35]:
hiddenlayer_acc_list = []
for hidden_layer in range(1,8):
    #dropout = dropout_/10
    #print(dropout)
    #Defining the model 

    # Seq approach
    model = Sequential()
    # input dimension is 4 since there are 4 features (input layer)
    model.add(Dense(4, input_dim=4, activation='softmax'))
    # add hidden layers
    for layer_s in range(1,hidden_layer):
        # 42 is the no. of neurons determined in part (b) 
        # -> the best (though the accuracy curve is not so smooth)
        model.add(Dense(18, activation='softmax',kernel_initializer = 'glorot_normal'))
        #model.add(Dropout(dropout))
        model.add(BatchNormalization())
    # 2nd layer is output layer
    model.add(Dense(3, activation='softmax'))
    # output layer
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    #model.summary()
    hist = model.fit(X_train,y_train,epochs=100,verbose=0)
    print("Number of hidden layers => ",hidden_layer)
    print("Accuracy => ",hist.history.get('accuracy')[-1])
    hiddenlayer_acc_list.append([hidden_layer,hist.history.get('accuracy')[-1]])
    plt.figure(figsize=(7,5))
    legend_acc = []
    for item in hist.history.keys():
        if item.split('_')[-1:] == ['accuracy']:
            #print("Accuracy = ",item)
            legend_acc.append(item)
            plt.plot(hist.history[item])
    plt.ylabel('Accuracy ->')
    plt.xlabel('Epoch ->')
    plt.legend(legend_acc, loc='upper left')
    plt.show()

    plt.figure(figsize=(7,5))
    legend_acc = []
    for item in hist.history.keys():
        if item.split('_')[-1:] == ['loss']:
            #print("Accuracy = ",item)
            legend_acc.append(item)
            plt.plot(hist.history[item])
    plt.ylabel('Loss ->')
    plt.xlabel('Epoch ->')
    plt.legend(legend_acc, loc='upper right')
    plt.show()
Number of hidden layers =>  1
Accuracy =>  0.73333335
Number of hidden layers =>  2
Accuracy =>  0.98333335
Number of hidden layers =>  3
Accuracy =>  0.975
Number of hidden layers =>  4
Accuracy =>  0.96666664
Number of hidden layers =>  5
Accuracy =>  0.9583333
Number of hidden layers =>  6
Accuracy =>  0.9583333
Number of hidden layers =>  7
Accuracy =>  0.98333335
In [39]:
hiddenlayer_acc_list
Out[39]:
[[1, 0.73333335],
 [2, 0.98333335],
 [3, 0.975],
 [4, 0.96666664],
 [5, 0.9583333],
 [6, 0.9583333],
 [7, 0.98333335]]
In [42]:
max([i[1] for i in hiddenlayer_acc_list])
Out[42]:
0.98333335
In [43]:
#accuracy_neuron_list
print("No. of hidden Layers selected => ", 2, " Since, we should select the simple one from Occam's Rasor ")
No. of hidden Layers selected =>  2  Since, we should select the simple one from Occam's Rasor 
In [44]:
first_d = [i[0] for i in hiddenlayer_acc_list]
second_d = [i[1] for i in hiddenlayer_acc_list]
fig, ax = plt.subplots()
ax.plot(first_d, second_d,color='blue')

ax.set(xlabel='No. of Hidden Layers with 42 Neurons', ylabel='Accuracy',
       title='Variation of Accuracy with No. of Hidden Neurons')
#ax.grid()
plt.show()

e)

Using the setting as determined best in part b), train the model with a few different optimization algorithms (with default settings) and print the accuracy from each case. You may try adam, adagrad, adadelta.

With Adam

In [45]:
model = Sequential()
# input dimension is 4 since there are 4 features (input layer)
model.add(Dense(4, input_dim=4, activation='softmax'))
# 1st hidden layer
# initialising the weights using Xavier's Method
model.add(Dense(18, activation='softmax',kernel_initializer = 'glorot_normal'))
# model.add(Dropout(dropout))
model.add(BatchNormalization())
# 2nd layer is output layer
model.add(Dense(3, activation='softmax'))
# output layer
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#model.summary()
hist = model.fit(X_train,y_train,epochs=100,verbose=1)
#print("Dropout => ",dropout)
Epoch 1/100
120/120 [==============================] - 2s 13ms/step - loss: 1.1004 - accuracy: 0.3750
Epoch 2/100
120/120 [==============================] - 0s 342us/step - loss: 1.0739 - accuracy: 0.4000
Epoch 3/100
120/120 [==============================] - 0s 302us/step - loss: 1.0461 - accuracy: 0.4667
Epoch 4/100
120/120 [==============================] - 0s 387us/step - loss: 1.0245 - accuracy: 0.4333
Epoch 5/100
120/120 [==============================] - 0s 623us/step - loss: 0.9946 - accuracy: 0.5500
Epoch 6/100
120/120 [==============================] - 0s 363us/step - loss: 0.9751 - accuracy: 0.5750
Epoch 7/100
120/120 [==============================] - 0s 303us/step - loss: 0.9532 - accuracy: 0.6083
Epoch 8/100
120/120 [==============================] - 0s 341us/step - loss: 0.9318 - accuracy: 0.6333
Epoch 9/100
120/120 [==============================] - 0s 401us/step - loss: 0.9131 - accuracy: 0.6167
Epoch 10/100
120/120 [==============================] - 0s 146us/step - loss: 0.8931 - accuracy: 0.6417
Epoch 11/100
120/120 [==============================] - 0s 238us/step - loss: 0.8605 - accuracy: 0.6667
Epoch 12/100
120/120 [==============================] - 0s 415us/step - loss: 0.8463 - accuracy: 0.7167
Epoch 13/100
120/120 [==============================] - 0s 260us/step - loss: 0.8226 - accuracy: 0.6833
Epoch 14/100
120/120 [==============================] - 0s 329us/step - loss: 0.8018 - accuracy: 0.7083
Epoch 15/100
120/120 [==============================] - 0s 407us/step - loss: 0.7859 - accuracy: 0.7083
Epoch 16/100
120/120 [==============================] - 0s 370us/step - loss: 0.7632 - accuracy: 0.6917
Epoch 17/100
120/120 [==============================] - 0s 450us/step - loss: 0.7452 - accuracy: 0.6833
Epoch 18/100
120/120 [==============================] - 0s 295us/step - loss: 0.7280 - accuracy: 0.6917
Epoch 19/100
120/120 [==============================] - 0s 243us/step - loss: 0.7188 - accuracy: 0.7417
Epoch 20/100
120/120 [==============================] - 0s 268us/step - loss: 0.6953 - accuracy: 0.7000
Epoch 21/100
120/120 [==============================] - 0s 544us/step - loss: 0.6884 - accuracy: 0.7333
Epoch 22/100
120/120 [==============================] - 0s 302us/step - loss: 0.6677 - accuracy: 0.7250
Epoch 23/100
120/120 [==============================] - 0s 366us/step - loss: 0.6615 - accuracy: 0.8000
Epoch 24/100
120/120 [==============================] - 0s 660us/step - loss: 0.6488 - accuracy: 0.7333
Epoch 25/100
120/120 [==============================] - 0s 447us/step - loss: 0.6464 - accuracy: 0.7833
Epoch 26/100
120/120 [==============================] - 0s 497us/step - loss: 0.6253 - accuracy: 0.7500
Epoch 27/100
120/120 [==============================] - 0s 797us/step - loss: 0.6186 - accuracy: 0.7583
Epoch 28/100
120/120 [==============================] - 0s 543us/step - loss: 0.6098 - accuracy: 0.7583
Epoch 29/100
120/120 [==============================] - 0s 385us/step - loss: 0.5971 - accuracy: 0.7917
Epoch 30/100
120/120 [==============================] - 0s 436us/step - loss: 0.5972 - accuracy: 0.8000
Epoch 31/100
120/120 [==============================] - 0s 304us/step - loss: 0.5893 - accuracy: 0.8583
Epoch 32/100
120/120 [==============================] - 0s 357us/step - loss: 0.5669 - accuracy: 0.8083
Epoch 33/100
120/120 [==============================] - 0s 401us/step - loss: 0.5644 - accuracy: 0.8000
Epoch 34/100
120/120 [==============================] - 0s 234us/step - loss: 0.5566 - accuracy: 0.8083
Epoch 35/100
120/120 [==============================] - 0s 252us/step - loss: 0.5468 - accuracy: 0.8667
Epoch 36/100
120/120 [==============================] - 0s 378us/step - loss: 0.5646 - accuracy: 0.8000
Epoch 37/100
120/120 [==============================] - 0s 288us/step - loss: 0.5380 - accuracy: 0.8500
Epoch 38/100
120/120 [==============================] - 0s 569us/step - loss: 0.5224 - accuracy: 0.8750
Epoch 39/100
120/120 [==============================] - 0s 580us/step - loss: 0.5191 - accuracy: 0.8833
Epoch 40/100
120/120 [==============================] - 0s 433us/step - loss: 0.5101 - accuracy: 0.8917
Epoch 41/100
120/120 [==============================] - 0s 439us/step - loss: 0.5047 - accuracy: 0.9250
Epoch 42/100
120/120 [==============================] - 0s 556us/step - loss: 0.4867 - accuracy: 0.9417
Epoch 43/100
120/120 [==============================] - 0s 420us/step - loss: 0.4772 - accuracy: 0.9250
Epoch 44/100
120/120 [==============================] - 0s 592us/step - loss: 0.4686 - accuracy: 0.9500
Epoch 45/100
120/120 [==============================] - 0s 510us/step - loss: 0.4759 - accuracy: 0.9000
Epoch 46/100
120/120 [==============================] - 0s 410us/step - loss: 0.4571 - accuracy: 0.9417
Epoch 47/100
120/120 [==============================] - 0s 466us/step - loss: 0.4462 - accuracy: 0.9583
Epoch 48/100
120/120 [==============================] - 0s 529us/step - loss: 0.4397 - accuracy: 0.9417
Epoch 49/100
120/120 [==============================] - 0s 580us/step - loss: 0.4306 - accuracy: 0.9750
Epoch 50/100
120/120 [==============================] - 0s 302us/step - loss: 0.4135 - accuracy: 0.9667
Epoch 51/100
120/120 [==============================] - 0s 480us/step - loss: 0.4099 - accuracy: 0.9667
Epoch 52/100
120/120 [==============================] - 0s 393us/step - loss: 0.4071 - accuracy: 0.9500
Epoch 53/100
120/120 [==============================] - 0s 593us/step - loss: 0.3845 - accuracy: 0.9500
Epoch 54/100
120/120 [==============================] - 0s 460us/step - loss: 0.3949 - accuracy: 0.9583
Epoch 55/100
120/120 [==============================] - 0s 459us/step - loss: 0.3742 - accuracy: 0.9500
Epoch 56/100
120/120 [==============================] - 0s 385us/step - loss: 0.3634 - accuracy: 0.9750
Epoch 57/100
120/120 [==============================] - 0s 368us/step - loss: 0.3696 - accuracy: 0.9667
Epoch 58/100
120/120 [==============================] - 0s 341us/step - loss: 0.3526 - accuracy: 0.9667
Epoch 59/100
120/120 [==============================] - 0s 518us/step - loss: 0.3402 - accuracy: 0.9667
Epoch 60/100
120/120 [==============================] - 0s 565us/step - loss: 0.3285 - accuracy: 0.9750
Epoch 61/100
120/120 [==============================] - 0s 514us/step - loss: 0.3261 - accuracy: 0.9750
Epoch 62/100
120/120 [==============================] - 0s 503us/step - loss: 0.3112 - accuracy: 0.9667
Epoch 63/100
120/120 [==============================] - 0s 399us/step - loss: 0.3001 - accuracy: 0.9750
Epoch 64/100
120/120 [==============================] - 0s 291us/step - loss: 0.3036 - accuracy: 0.9750
Epoch 65/100
120/120 [==============================] - 0s 575us/step - loss: 0.3181 - accuracy: 0.9667
Epoch 66/100
120/120 [==============================] - 0s 333us/step - loss: 0.2781 - accuracy: 0.9833
Epoch 67/100
120/120 [==============================] - 0s 459us/step - loss: 0.2884 - accuracy: 0.9417
Epoch 68/100
120/120 [==============================] - 0s 442us/step - loss: 0.2860 - accuracy: 0.9667
Epoch 69/100
120/120 [==============================] - 0s 265us/step - loss: 0.2696 - accuracy: 0.9583
Epoch 70/100
120/120 [==============================] - 0s 419us/step - loss: 0.2631 - accuracy: 0.9500
Epoch 71/100
120/120 [==============================] - 0s 534us/step - loss: 0.2726 - accuracy: 0.9667
Epoch 72/100
120/120 [==============================] - 0s 416us/step - loss: 0.2385 - accuracy: 0.9750
Epoch 73/100
120/120 [==============================] - 0s 393us/step - loss: 0.2336 - accuracy: 0.9750
Epoch 74/100
120/120 [==============================] - 0s 409us/step - loss: 0.2230 - accuracy: 0.9750
Epoch 75/100
120/120 [==============================] - 0s 460us/step - loss: 0.2207 - accuracy: 0.9750
Epoch 76/100
120/120 [==============================] - 0s 379us/step - loss: 0.2564 - accuracy: 0.9833
Epoch 77/100
120/120 [==============================] - 0s 367us/step - loss: 0.2077 - accuracy: 0.9750
Epoch 78/100
120/120 [==============================] - 0s 291us/step - loss: 0.2215 - accuracy: 0.9667
Epoch 79/100
120/120 [==============================] - 0s 691us/step - loss: 0.2048 - accuracy: 0.9667
Epoch 80/100
120/120 [==============================] - 0s 309us/step - loss: 0.1889 - accuracy: 0.9833
Epoch 81/100
120/120 [==============================] - 0s 408us/step - loss: 0.1976 - accuracy: 0.9750
Epoch 82/100
120/120 [==============================] - 0s 296us/step - loss: 0.2052 - accuracy: 0.9750
Epoch 83/100
120/120 [==============================] - 0s 313us/step - loss: 0.1879 - accuracy: 0.9667
Epoch 84/100
120/120 [==============================] - 0s 292us/step - loss: 0.1834 - accuracy: 0.9500
Epoch 85/100
120/120 [==============================] - 0s 230us/step - loss: 0.1837 - accuracy: 0.9833
Epoch 86/100
120/120 [==============================] - 0s 240us/step - loss: 0.1877 - accuracy: 0.9583
Epoch 87/100
120/120 [==============================] - 0s 333us/step - loss: 0.1649 - accuracy: 0.9750
Epoch 88/100
120/120 [==============================] - 0s 314us/step - loss: 0.1730 - accuracy: 0.9500
Epoch 89/100
120/120 [==============================] - 0s 236us/step - loss: 0.1716 - accuracy: 0.9667
Epoch 90/100
120/120 [==============================] - 0s 233us/step - loss: 0.1514 - accuracy: 0.9750
Epoch 91/100
120/120 [==============================] - 0s 309us/step - loss: 0.1603 - accuracy: 0.9750
Epoch 92/100
120/120 [==============================] - 0s 240us/step - loss: 0.1460 - accuracy: 0.9750
Epoch 93/100
120/120 [==============================] - 0s 288us/step - loss: 0.1498 - accuracy: 0.9750
Epoch 94/100
120/120 [==============================] - 0s 313us/step - loss: 0.1595 - accuracy: 0.9667
Epoch 95/100
120/120 [==============================] - 0s 233us/step - loss: 0.1377 - accuracy: 0.9750
Epoch 96/100
120/120 [==============================] - 0s 236us/step - loss: 0.1330 - accuracy: 0.9750
Epoch 97/100
120/120 [==============================] - 0s 219us/step - loss: 0.1312 - accuracy: 0.9833
Epoch 98/100
120/120 [==============================] - 0s 325us/step - loss: 0.1342 - accuracy: 0.9833
Epoch 99/100
120/120 [==============================] - 0s 206us/step - loss: 0.1306 - accuracy: 0.9667
Epoch 100/100
120/120 [==============================] - 0s 241us/step - loss: 0.1429 - accuracy: 0.9667
In [46]:
print("Accuracy => ",hist.history.get('accuracy')[-1])
#dropout_acc_list.append([dropout,hist.history.get('accuracy')[-1]])
plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['accuracy']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Accuracy ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper left')
plt.show()

plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['loss']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Loss ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper right')
plt.show()
Accuracy =>  0.96666664

With Adagrad

In [47]:
model = Sequential()
# input dimension is 4 since there are 4 features (input layer)
model.add(Dense(4, input_dim=4, activation='softmax'))
# 1st hidden layer
# initialising the weights using Xavier's Method
model.add(Dense(18, activation='softmax',kernel_initializer = 'glorot_normal'))
# model.add(Dropout(dropout))
model.add(BatchNormalization())
# 2nd layer is output layer
model.add(Dense(3, activation='softmax'))
# output layer
model.compile(loss='categorical_crossentropy', optimizer='adagrad', metrics=['accuracy'])
#model.summary()
hist = model.fit(X_train,y_train,epochs=100,verbose=1)
#print("Dropout => ",dropout)
Epoch 1/100
120/120 [==============================] - 1s 9ms/step - loss: 1.0954 - accuracy: 0.4500
Epoch 2/100
120/120 [==============================] - 0s 272us/step - loss: 1.0861 - accuracy: 0.3667
Epoch 3/100
120/120 [==============================] - 0s 778us/step - loss: 1.0749 - accuracy: 0.3750
Epoch 4/100
120/120 [==============================] - 0s 831us/step - loss: 1.0535 - accuracy: 0.4500
Epoch 5/100
120/120 [==============================] - 0s 370us/step - loss: 1.0224 - accuracy: 0.6417
Epoch 6/100
120/120 [==============================] - 0s 425us/step - loss: 0.9774 - accuracy: 0.6667
Epoch 7/100
120/120 [==============================] - 0s 541us/step - loss: 0.9256 - accuracy: 0.6583
Epoch 8/100
120/120 [==============================] - 0s 716us/step - loss: 0.8592 - accuracy: 0.7083
Epoch 9/100
120/120 [==============================] - 0s 326us/step - loss: 0.7968 - accuracy: 0.8000
Epoch 10/100
120/120 [==============================] - 0s 252us/step - loss: 0.7670 - accuracy: 0.7417
Epoch 11/100
120/120 [==============================] - 0s 500us/step - loss: 0.7097 - accuracy: 0.6833
Epoch 12/100
120/120 [==============================] - 0s 561us/step - loss: 0.6477 - accuracy: 0.7250
Epoch 13/100
120/120 [==============================] - 0s 996us/step - loss: 0.6207 - accuracy: 0.7833
Epoch 14/100
120/120 [==============================] - 0s 455us/step - loss: 0.6056 - accuracy: 0.6500
Epoch 15/100
120/120 [==============================] - 0s 351us/step - loss: 0.5799 - accuracy: 0.7083
Epoch 16/100
120/120 [==============================] - 0s 444us/step - loss: 0.5578 - accuracy: 0.7333
Epoch 17/100
120/120 [==============================] - 0s 424us/step - loss: 0.5565 - accuracy: 0.7500
Epoch 18/100
120/120 [==============================] - 0s 338us/step - loss: 0.5344 - accuracy: 0.7667
Epoch 19/100
120/120 [==============================] - 0s 682us/step - loss: 0.5255 - accuracy: 0.7833
Epoch 20/100
120/120 [==============================] - 0s 353us/step - loss: 0.5169 - accuracy: 0.7667
Epoch 21/100
120/120 [==============================] - 0s 250us/step - loss: 0.5103 - accuracy: 0.7750
Epoch 22/100
120/120 [==============================] - 0s 780us/step - loss: 0.5050 - accuracy: 0.7500
Epoch 23/100
120/120 [==============================] - 0s 490us/step - loss: 0.5018 - accuracy: 0.7583
Epoch 24/100
120/120 [==============================] - 0s 844us/step - loss: 0.4928 - accuracy: 0.7833
Epoch 25/100
120/120 [==============================] - 0s 663us/step - loss: 0.4920 - accuracy: 0.7500
Epoch 26/100
120/120 [==============================] - 0s 534us/step - loss: 0.5255 - accuracy: 0.6833
Epoch 27/100
120/120 [==============================] - 0s 445us/step - loss: 0.5120 - accuracy: 0.7667
Epoch 28/100
120/120 [==============================] - 0s 400us/step - loss: 0.4758 - accuracy: 0.8167
Epoch 29/100
120/120 [==============================] - 0s 236us/step - loss: 0.4774 - accuracy: 0.8083
Epoch 30/100
120/120 [==============================] - 0s 297us/step - loss: 0.4738 - accuracy: 0.7833
Epoch 31/100
120/120 [==============================] - 0s 382us/step - loss: 0.4687 - accuracy: 0.8167
Epoch 32/100
120/120 [==============================] - 0s 419us/step - loss: 0.4658 - accuracy: 0.8000
Epoch 33/100
120/120 [==============================] - 0s 475us/step - loss: 0.4727 - accuracy: 0.8083
Epoch 34/100
120/120 [==============================] - 0s 570us/step - loss: 0.4589 - accuracy: 0.8333
Epoch 35/100
120/120 [==============================] - 0s 540us/step - loss: 0.4723 - accuracy: 0.7750
Epoch 36/100
120/120 [==============================] - 0s 758us/step - loss: 0.4628 - accuracy: 0.8167
Epoch 37/100
120/120 [==============================] - 0s 335us/step - loss: 0.4460 - accuracy: 0.8500
Epoch 38/100
120/120 [==============================] - 0s 572us/step - loss: 0.4540 - accuracy: 0.8083
Epoch 39/100
120/120 [==============================] - 0s 426us/step - loss: 0.4499 - accuracy: 0.8000
Epoch 40/100
120/120 [==============================] - 0s 900us/step - loss: 0.4504 - accuracy: 0.8333
Epoch 41/100
120/120 [==============================] - 0s 708us/step - loss: 0.4384 - accuracy: 0.8417
Epoch 42/100
120/120 [==============================] - 0s 790us/step - loss: 0.4405 - accuracy: 0.8333
Epoch 43/100
120/120 [==============================] - 0s 639us/step - loss: 0.4331 - accuracy: 0.8583
Epoch 44/100
120/120 [==============================] - 0s 568us/step - loss: 0.4791 - accuracy: 0.7417
Epoch 45/100
120/120 [==============================] - 0s 510us/step - loss: 0.4597 - accuracy: 0.7667
Epoch 46/100
120/120 [==============================] - 0s 747us/step - loss: 0.4263 - accuracy: 0.8500
Epoch 47/100
120/120 [==============================] - 0s 931us/step - loss: 0.4294 - accuracy: 0.8417
Epoch 48/100
120/120 [==============================] - 0s 350us/step - loss: 0.4152 - accuracy: 0.8833
Epoch 49/100
120/120 [==============================] - 0s 261us/step - loss: 0.4318 - accuracy: 0.8167
Epoch 50/100
120/120 [==============================] - 0s 271us/step - loss: 0.4205 - accuracy: 0.8333
Epoch 51/100
120/120 [==============================] - 0s 287us/step - loss: 0.4100 - accuracy: 0.8500
Epoch 52/100
120/120 [==============================] - 0s 425us/step - loss: 0.4394 - accuracy: 0.7917
Epoch 53/100
120/120 [==============================] - 0s 429us/step - loss: 0.3972 - accuracy: 0.8917
Epoch 54/100
120/120 [==============================] - 0s 346us/step - loss: 0.4121 - accuracy: 0.8667
Epoch 55/100
120/120 [==============================] - 0s 306us/step - loss: 0.3977 - accuracy: 0.9000
Epoch 56/100
120/120 [==============================] - 0s 212us/step - loss: 0.3863 - accuracy: 0.8917
Epoch 57/100
120/120 [==============================] - 0s 126us/step - loss: 0.3848 - accuracy: 0.8917
Epoch 58/100
120/120 [==============================] - 0s 231us/step - loss: 0.3841 - accuracy: 0.8750
Epoch 59/100
120/120 [==============================] - 0s 220us/step - loss: 0.3708 - accuracy: 0.9250
Epoch 60/100
120/120 [==============================] - 0s 195us/step - loss: 0.3710 - accuracy: 0.9000
Epoch 61/100
120/120 [==============================] - 0s 196us/step - loss: 0.3808 - accuracy: 0.9083
Epoch 62/100
120/120 [==============================] - 0s 292us/step - loss: 0.3614 - accuracy: 0.9000
Epoch 63/100
120/120 [==============================] - 0s 364us/step - loss: 0.3557 - accuracy: 0.9000
Epoch 64/100
120/120 [==============================] - 0s 427us/step - loss: 0.3601 - accuracy: 0.8833
Epoch 65/100
120/120 [==============================] - 0s 358us/step - loss: 0.3546 - accuracy: 0.9083
Epoch 66/100
120/120 [==============================] - 0s 304us/step - loss: 0.3773 - accuracy: 0.8417
Epoch 67/100
120/120 [==============================] - 0s 284us/step - loss: 0.3725 - accuracy: 0.8583
Epoch 68/100
120/120 [==============================] - 0s 376us/step - loss: 0.3377 - accuracy: 0.9333
Epoch 69/100
120/120 [==============================] - 0s 317us/step - loss: 0.3356 - accuracy: 0.9250
Epoch 70/100
120/120 [==============================] - 0s 289us/step - loss: 0.3314 - accuracy: 0.9250
Epoch 71/100
120/120 [==============================] - 0s 170us/step - loss: 0.3369 - accuracy: 0.8917
Epoch 72/100
120/120 [==============================] - 0s 279us/step - loss: 0.3311 - accuracy: 0.9250
Epoch 73/100
120/120 [==============================] - 0s 274us/step - loss: 0.3802 - accuracy: 0.8333
Epoch 74/100
120/120 [==============================] - 0s 203us/step - loss: 0.3289 - accuracy: 0.9167
Epoch 75/100
120/120 [==============================] - 0s 464us/step - loss: 0.3457 - accuracy: 0.9083
Epoch 76/100
120/120 [==============================] - 0s 235us/step - loss: 0.3283 - accuracy: 0.9083
Epoch 77/100
120/120 [==============================] - 0s 203us/step - loss: 0.3069 - accuracy: 0.9417
Epoch 78/100
120/120 [==============================] - 0s 190us/step - loss: 0.3359 - accuracy: 0.8750
Epoch 79/100
120/120 [==============================] - 0s 231us/step - loss: 0.3007 - accuracy: 0.9583
Epoch 80/100
120/120 [==============================] - 0s 359us/step - loss: 0.2920 - accuracy: 0.9500
Epoch 81/100
120/120 [==============================] - 0s 178us/step - loss: 0.2897 - accuracy: 0.9583
Epoch 82/100
120/120 [==============================] - 0s 243us/step - loss: 0.3002 - accuracy: 0.9417
Epoch 83/100
120/120 [==============================] - 0s 225us/step - loss: 0.2816 - accuracy: 0.9583
Epoch 84/100
120/120 [==============================] - 0s 261us/step - loss: 0.2810 - accuracy: 0.9500
Epoch 85/100
120/120 [==============================] - 0s 312us/step - loss: 0.2996 - accuracy: 0.9167
Epoch 86/100
120/120 [==============================] - 0s 322us/step - loss: 0.2943 - accuracy: 0.9250
Epoch 87/100
120/120 [==============================] - 0s 239us/step - loss: 0.2759 - accuracy: 0.9667
Epoch 88/100
120/120 [==============================] - 0s 221us/step - loss: 0.2703 - accuracy: 0.9667
Epoch 89/100
120/120 [==============================] - 0s 217us/step - loss: 0.2651 - accuracy: 0.9417
Epoch 90/100
120/120 [==============================] - 0s 207us/step - loss: 0.2731 - accuracy: 0.9417
Epoch 91/100
120/120 [==============================] - 0s 213us/step - loss: 0.2486 - accuracy: 0.9667
Epoch 92/100
120/120 [==============================] - 0s 198us/step - loss: 0.2541 - accuracy: 0.9583
Epoch 93/100
120/120 [==============================] - 0s 211us/step - loss: 0.3026 - accuracy: 0.9000
Epoch 94/100
120/120 [==============================] - 0s 261us/step - loss: 0.2727 - accuracy: 0.9167
Epoch 95/100
120/120 [==============================] - 0s 278us/step - loss: 0.2738 - accuracy: 0.9167
Epoch 96/100
120/120 [==============================] - ETA: 0s - loss: 0.2506 - accuracy: 0.93 - 0s 118us/step - loss: 0.2466 - accuracy: 0.9500
Epoch 97/100
120/120 [==============================] - 0s 221us/step - loss: 0.2728 - accuracy: 0.9333
Epoch 98/100
120/120 [==============================] - 0s 254us/step - loss: 0.2419 - accuracy: 0.9750
Epoch 99/100
120/120 [==============================] - 0s 258us/step - loss: 0.3151 - accuracy: 0.9000
Epoch 100/100
120/120 [==============================] - 0s 245us/step - loss: 0.2453 - accuracy: 0.9583
In [48]:
print("Accuracy => ",hist.history.get('accuracy')[-1])
#dropout_acc_list.append([dropout,hist.history.get('accuracy')[-1]])
plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['accuracy']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Accuracy ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper left')
plt.show()

plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['loss']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Loss ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper right')
plt.show()
Accuracy =>  0.9583333

With Adadelta

In [50]:
model = Sequential()
# input dimension is 4 since there are 4 features (input layer)
model.add(Dense(4, input_dim=4, activation='softmax'))
# 1st hidden layer
# initialising the weights using Xavier's Method
model.add(Dense(18, activation='softmax',kernel_initializer = 'glorot_normal'))
# model.add(Dropout(dropout))
model.add(BatchNormalization())
# 2nd layer is output layer
model.add(Dense(3, activation='softmax'))
# output layer
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
#model.summary()
hist = model.fit(X_train,y_train,epochs=100,verbose=1)
#print("Dropout => ",dropout)
Epoch 1/100
120/120 [==============================] - 1s 11ms/step - loss: 1.1975 - accuracy: 0.0500
Epoch 2/100
120/120 [==============================] - 0s 662us/step - loss: 1.1703 - accuracy: 0.0667
Epoch 3/100
120/120 [==============================] - 0s 505us/step - loss: 1.1354 - accuracy: 0.1500
Epoch 4/100
120/120 [==============================] - 0s 636us/step - loss: 1.1108 - accuracy: 0.4500
Epoch 5/100
120/120 [==============================] - 0s 474us/step - loss: 1.0912 - accuracy: 0.4583
Epoch 6/100
120/120 [==============================] - 0s 669us/step - loss: 1.0737 - accuracy: 0.5000
Epoch 7/100
120/120 [==============================] - 0s 329us/step - loss: 1.0595 - accuracy: 0.5083
Epoch 8/100
120/120 [==============================] - 0s 398us/step - loss: 1.0355 - accuracy: 0.5167
Epoch 9/100
120/120 [==============================] - 0s 693us/step - loss: 1.0238 - accuracy: 0.5000
Epoch 10/100
120/120 [==============================] - 0s 977us/step - loss: 1.0006 - accuracy: 0.5250
Epoch 11/100
120/120 [==============================] - 0s 469us/step - loss: 0.9786 - accuracy: 0.5083
Epoch 12/100
120/120 [==============================] - 0s 340us/step - loss: 0.9529 - accuracy: 0.5000
Epoch 13/100
120/120 [==============================] - 0s 550us/step - loss: 0.9289 - accuracy: 0.6000
Epoch 14/100
120/120 [==============================] - 0s 628us/step - loss: 0.8855 - accuracy: 0.7667
Epoch 15/100
120/120 [==============================] - 0s 747us/step - loss: 0.8482 - accuracy: 0.8167
Epoch 16/100
120/120 [==============================] - 0s 488us/step - loss: 0.7999 - accuracy: 0.8000
Epoch 17/100
120/120 [==============================] - 0s 550us/step - loss: 0.7575 - accuracy: 0.8250
Epoch 18/100
120/120 [==============================] - 0s 534us/step - loss: 0.6874 - accuracy: 0.8250
Epoch 19/100
120/120 [==============================] - 0s 613us/step - loss: 0.6663 - accuracy: 0.7833
Epoch 20/100
120/120 [==============================] - 0s 550us/step - loss: 0.5989 - accuracy: 0.8500
Epoch 21/100
120/120 [==============================] - 0s 561us/step - loss: 0.5631 - accuracy: 0.8250
Epoch 22/100
120/120 [==============================] - 0s 504us/step - loss: 0.5461 - accuracy: 0.8667
Epoch 23/100
120/120 [==============================] - 0s 572us/step - loss: 0.5172 - accuracy: 0.8250
Epoch 24/100
120/120 [==============================] - 0s 356us/step - loss: 0.4893 - accuracy: 0.8583
Epoch 25/100
120/120 [==============================] - 0s 365us/step - loss: 0.4817 - accuracy: 0.8667
Epoch 26/100
120/120 [==============================] - 0s 246us/step - loss: 0.4705 - accuracy: 0.8500
Epoch 27/100
120/120 [==============================] - 0s 328us/step - loss: 0.4570 - accuracy: 0.8833
Epoch 28/100
120/120 [==============================] - 0s 459us/step - loss: 0.4528 - accuracy: 0.8417
Epoch 29/100
120/120 [==============================] - 0s 165us/step - loss: 0.4459 - accuracy: 0.8500
Epoch 30/100
120/120 [==============================] - 0s 241us/step - loss: 0.4415 - accuracy: 0.9000
Epoch 31/100
120/120 [==============================] - 0s 338us/step - loss: 0.4289 - accuracy: 0.9167
Epoch 32/100
120/120 [==============================] - 0s 481us/step - loss: 0.4141 - accuracy: 0.9000
Epoch 33/100
120/120 [==============================] - 0s 456us/step - loss: 0.4463 - accuracy: 0.8417
Epoch 34/100
120/120 [==============================] - 0s 316us/step - loss: 0.4196 - accuracy: 0.8917
Epoch 35/100
120/120 [==============================] - 0s 331us/step - loss: 0.3908 - accuracy: 0.9167
Epoch 36/100
120/120 [==============================] - 0s 317us/step - loss: 0.3796 - accuracy: 0.9167
Epoch 37/100
120/120 [==============================] - 0s 199us/step - loss: 0.3723 - accuracy: 0.9083
Epoch 38/100
120/120 [==============================] - 0s 426us/step - loss: 0.3896 - accuracy: 0.8833
Epoch 39/100
120/120 [==============================] - 0s 355us/step - loss: 0.3806 - accuracy: 0.9083
Epoch 40/100
120/120 [==============================] - 0s 342us/step - loss: 0.3576 - accuracy: 0.9083
Epoch 41/100
120/120 [==============================] - 0s 368us/step - loss: 0.3470 - accuracy: 0.9250
Epoch 42/100
120/120 [==============================] - 0s 284us/step - loss: 0.3420 - accuracy: 0.9333
Epoch 43/100
120/120 [==============================] - 0s 294us/step - loss: 0.3474 - accuracy: 0.9250
Epoch 44/100
120/120 [==============================] - 0s 281us/step - loss: 0.3350 - accuracy: 0.9083
Epoch 45/100
120/120 [==============================] - ETA: 0s - loss: 0.2904 - accuracy: 0.87 - 0s 323us/step - loss: 0.3339 - accuracy: 0.9250
Epoch 46/100
120/120 [==============================] - 0s 448us/step - loss: 0.3222 - accuracy: 0.9333
Epoch 47/100
120/120 [==============================] - 0s 345us/step - loss: 0.3191 - accuracy: 0.9083
Epoch 48/100
120/120 [==============================] - ETA: 0s - loss: 0.2970 - accuracy: 0.93 - 0s 476us/step - loss: 0.2875 - accuracy: 0.9667
Epoch 49/100
120/120 [==============================] - 0s 364us/step - loss: 0.2915 - accuracy: 0.9500
Epoch 50/100
120/120 [==============================] - 0s 441us/step - loss: 0.2775 - accuracy: 0.9750
Epoch 51/100
120/120 [==============================] - 0s 306us/step - loss: 0.2717 - accuracy: 0.9667
Epoch 52/100
120/120 [==============================] - 0s 307us/step - loss: 0.2684 - accuracy: 0.9583
Epoch 53/100
120/120 [==============================] - 0s 131us/step - loss: 0.2557 - accuracy: 0.9750
Epoch 54/100
120/120 [==============================] - 0s 154us/step - loss: 0.2889 - accuracy: 0.9167
Epoch 55/100
120/120 [==============================] - 0s 250us/step - loss: 0.2407 - accuracy: 0.9583
Epoch 56/100
120/120 [==============================] - 0s 235us/step - loss: 0.2611 - accuracy: 0.9417
Epoch 57/100
120/120 [==============================] - 0s 352us/step - loss: 0.2415 - accuracy: 0.9417
Epoch 58/100
120/120 [==============================] - 0s 441us/step - loss: 0.2367 - accuracy: 0.9250
Epoch 59/100
120/120 [==============================] - 0s 385us/step - loss: 0.2116 - accuracy: 0.9667
Epoch 60/100
120/120 [==============================] - 0s 267us/step - loss: 0.2038 - accuracy: 0.9750
Epoch 61/100
120/120 [==============================] - 0s 336us/step - loss: 0.2027 - accuracy: 0.9667
Epoch 62/100
120/120 [==============================] - 0s 304us/step - loss: 0.2023 - accuracy: 0.9583
Epoch 63/100
120/120 [==============================] - 0s 290us/step - loss: 0.2115 - accuracy: 0.9583
Epoch 64/100
120/120 [==============================] - 0s 306us/step - loss: 0.1761 - accuracy: 0.9667
Epoch 65/100
120/120 [==============================] - 0s 400us/step - loss: 0.1884 - accuracy: 0.9667
Epoch 66/100
120/120 [==============================] - 0s 521us/step - loss: 0.1804 - accuracy: 0.9750
Epoch 67/100
120/120 [==============================] - 0s 283us/step - loss: 0.1952 - accuracy: 0.9417
Epoch 68/100
120/120 [==============================] - 0s 368us/step - loss: 0.1817 - accuracy: 0.9583
Epoch 69/100
120/120 [==============================] - 0s 446us/step - loss: 0.1565 - accuracy: 0.9833
Epoch 70/100
120/120 [==============================] - 0s 336us/step - loss: 0.1765 - accuracy: 0.9583
Epoch 71/100
120/120 [==============================] - 0s 308us/step - loss: 0.1418 - accuracy: 0.9750
Epoch 72/100
120/120 [==============================] - 0s 460us/step - loss: 0.1895 - accuracy: 0.9417
Epoch 73/100
120/120 [==============================] - 0s 362us/step - loss: 0.1812 - accuracy: 0.9667
Epoch 74/100
120/120 [==============================] - 0s 322us/step - loss: 0.1593 - accuracy: 0.9583
Epoch 75/100
120/120 [==============================] - 0s 336us/step - loss: 0.1260 - accuracy: 0.9833
Epoch 76/100
120/120 [==============================] - 0s 247us/step - loss: 0.1264 - accuracy: 0.9750
Epoch 77/100
120/120 [==============================] - 0s 435us/step - loss: 0.1449 - accuracy: 0.9583
Epoch 78/100
120/120 [==============================] - 0s 301us/step - loss: 0.1504 - accuracy: 0.9667
Epoch 79/100
120/120 [==============================] - 0s 421us/step - loss: 0.1393 - accuracy: 0.9583
Epoch 80/100
120/120 [==============================] - 0s 399us/step - loss: 0.1187 - accuracy: 0.9833
Epoch 81/100
120/120 [==============================] - 0s 261us/step - loss: 0.1545 - accuracy: 0.9500
Epoch 82/100
120/120 [==============================] - 0s 397us/step - loss: 0.1238 - accuracy: 0.9667
Epoch 83/100
120/120 [==============================] - 0s 340us/step - loss: 0.1225 - accuracy: 0.9667
Epoch 84/100
120/120 [==============================] - 0s 317us/step - loss: 0.1211 - accuracy: 0.9750
Epoch 85/100
120/120 [==============================] - 0s 313us/step - loss: 0.1250 - accuracy: 0.9583
Epoch 86/100
120/120 [==============================] - 0s 249us/step - loss: 0.1131 - accuracy: 0.9833
Epoch 87/100
120/120 [==============================] - 0s 286us/step - loss: 0.0965 - accuracy: 0.9833
Epoch 88/100
120/120 [==============================] - 0s 319us/step - loss: 0.1068 - accuracy: 0.9750
Epoch 89/100
120/120 [==============================] - 0s 335us/step - loss: 0.0976 - accuracy: 0.9833
Epoch 90/100
120/120 [==============================] - 0s 274us/step - loss: 0.1005 - accuracy: 0.9833
Epoch 91/100
120/120 [==============================] - 0s 288us/step - loss: 0.1197 - accuracy: 0.9750
Epoch 92/100
120/120 [==============================] - 0s 306us/step - loss: 0.0904 - accuracy: 0.9833
Epoch 93/100
120/120 [==============================] - 0s 285us/step - loss: 0.0939 - accuracy: 0.9750
Epoch 94/100
120/120 [==============================] - 0s 328us/step - loss: 0.0979 - accuracy: 0.9667
Epoch 95/100
120/120 [==============================] - 0s 307us/step - loss: 0.1159 - accuracy: 0.9750
Epoch 96/100
120/120 [==============================] - 0s 296us/step - loss: 0.0954 - accuracy: 0.9750
Epoch 97/100
120/120 [==============================] - 0s 346us/step - loss: 0.1336 - accuracy: 0.9417
Epoch 98/100
120/120 [==============================] - 0s 343us/step - loss: 0.0938 - accuracy: 0.9833
Epoch 99/100
120/120 [==============================] - 0s 434us/step - loss: 0.1759 - accuracy: 0.9250
Epoch 100/100
120/120 [==============================] - 0s 227us/step - loss: 0.1084 - accuracy: 0.9750
In [51]:
print("Accuracy => ",hist.history.get('accuracy')[-1])
#dropout_acc_list.append([dropout,hist.history.get('accuracy')[-1]])
plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['accuracy']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Accuracy ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper left')
plt.show()

plt.figure(figsize=(7,5))
legend_acc = []
for item in hist.history.keys():
    if item.split('_')[-1:] == ['loss']:
        #print("Accuracy = ",item)
        legend_acc.append(item)
        plt.plot(hist.history[item])
plt.ylabel('Loss ->')
plt.xlabel('Epoch ->')
plt.legend(legend_acc, loc='upper right')
plt.show()
Accuracy =>  0.975

References