Professional Documents
Culture Documents
learning problem
May 1, 2022
To apply a three layer neural network, we first need to preprocess the train and test data.
1 Importing libraries
Necessary liabraries which will help us preprocess the train and test data are imported bellow.
[49]: import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn import linear_model
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import time
We can see that both train and test data are four dimensional and contains 15000 examples each.
1
[52]: train_data.shape
[52]: (15000, 4)
[53]: test_data.shape
[53]: (15000, 4)
According to The training dataset matlab file, the first 7500 rows are class 1 and the second 7500
rows are class 2. We need to create a new column ’class_name’ that labels first 7500 data as 0
(for class 1) and second 7500 data as 1 (for class 2).
/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py:1732:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
Here are the first 4 rows of the labeled data, all of which belong to class_1.
[55]: train_data.head()
[55]: 0 1 2 3 class_name
0 0.351074 3.635034 6.047802 -3.335702 0
1 6.391923 4.069761 5.844878 -7.343990 0
2 -10.094422 -5.324229 0.806393 -4.402726 0
3 -9.151437 -1.194381 6.352514 -0.786227 0
4 3.057876 -9.318271 -2.736060 -6.155479 0
2
6 Randomize the Dataset
We need to randomized the train data to make sure the data is random, so that the deep neural
network can learn properly.
[56]: from collections import Counter
Counter(train_data["class_name"])
train_data_updated = train_data.sample(frac=1).reset_index(drop=True)
train_data_updated.head()
[56]: 0 1 2 3 class_name
0 7.775054 7.431022 -2.588864 -0.022134 0
1 -5.181603 14.139670 -0.160220 2.571873 0
2 -4.486467 4.666326 -0.147838 7.182762 0
3 11.431682 10.108164 8.937301 1.930146 0
4 8.966712 -12.707098 -0.733887 7.864671 0
Before implementing the neural network we first have to seperate the first three columns of the
training data as input ( x_train ) and the fourth column ( class_name ) as output( y_train ).
[58]: x_train.shape
[58]: (15000, 4)
[59]: y_train.shape
[59]: (15000, 1)
It is better for the neural network to learn, if the data is scaled between 0 to 1. Therefore, I
normalized the input data but I did not touch the output data as it contains either 0 or 1.
[60]: x_train = np.array(x_train)
y_train = np.array(y_train)
[62]: x_train
3
[62]: array([[ 0.70284308, 0.67174356, -0.23402605, -0.00200085],
[-0.33915294, 0.92548786, -0.01048693, 0.16833754],
[-0.46393742, 0.48253631, -0.01528766, 0.7427564 ],
…,
[ 0.30323523, -0.23685057, 0.02507769, 0.92267075],
[ 0.6515841 , 0.5779838 , 0.29274954, -0.39455113],
[ 0.85834407, -0.08024698, -0.11541187, 0.49344299]])
[63]: x_train.shape
[63]: (15000, 4)
[64]: y_train
[64]: array([[0],
[0],
[0],
…,
[1],
[0],
[0]])
I than split the data to validate the training of the network. I took 85% of the data for training
the network and 15% of the data to validate the accuracy of training.
[65]: X_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size=0.
,→15, random_state=42)
[66]: X_train.shape
[66]: (12750, 4)
Here, I created a class named DeepNeuralNetwork. I set the number of epochs to 10000 and
learning rate to 0.01. I used Sigmoid activision function for the output layer as it is a binary
classification problem and Relu activision function for all the other layers.
[67]: class DeepNeuralNetwork:
def __init__(self, sizes, epochs=10000, learning_rate=.01):
self.sizes = sizes
self.epochs = epochs
self.learning_rate = learning_rate
4
self.b = [None] * (len(self.sizes) - 1)
# set random value to weights and biases
for i in range(len(self.sizes) - 1):
self.W[i] = np.random.randn(self.sizes[i], self.sizes[i + 1])
self.b[i] = np.zeros((1, self.sizes[i + 1]))
cost = np.squeeze(cost)
return round(float(cost), 2)
return Z, cache
5
dW = np.dot(A_prev.T, dZ) / m
db = np.sum(dZ, axis=0, keepdims=True) / m
dA_prev = np.dot(dZ, W.T)
return dA_prev, dW, db
caches.append(cache)
AL, cache = self.linear_activation_forward(A, self.W[L - 1], self.b[L -␣
,→1], 'sigmoid')
caches.append(cache)
return AL, caches
6
def update_parameters(self, grads, learning_rate):
L = len(self.sizes) - 1
for l in range(L):
self.W[l] = self.W[l] - learning_rate * grads["dW" + str(l + 1)]
self.b[l] = self.b[l] - learning_rate * grads["db" + str(l + 1)]
I created an object dnn using the DeepNeuralNetwork class. Here I set the number of neurons
for the first and second hidden layers to 32 and 24 respectively. And finally I trained the object
using the train method.
[ ]: sizes = (4,32,24,1)
dnn = DeepNeuralNetwork(sizes, epochs=10000)
7
train_losses, train_accuracies, validation_losses, validation_accuracies = dnn.
,→train(X_train, Y_train, X_val, Y_val)
Epoch: 1,
Train: Accuracy: 64.97, cost: 1.5
Validation: Accuracy: 66.13, cost: 1.48
Epoch: 501,
Train: Accuracy: 81.63, cost: 0.43
Validation: Accuracy: 80.76, cost: 0.48
Epoch: 1001,
Train: Accuracy: 82.26, cost: 0.4
Validation: Accuracy: 81.47, cost: 0.44
Epoch: 1501,
Train: Accuracy: 82.72, cost: 0.39
Validation: Accuracy: 81.78, cost: 0.42
Epoch: 2001,
Train: Accuracy: 82.99, cost: 0.38
Validation: Accuracy: 81.91, cost: 0.41
Epoch: 2501,
Train: Accuracy: 83.06, cost: 0.38
Validation: Accuracy: 82.09, cost: 0.41
Epoch: 3001,
Train: Accuracy: 83.16, cost: 0.37
Validation: Accuracy: 82.31, cost: 0.4
Epoch: 3501,
Train: Accuracy: 83.22, cost: 0.37
Validation: Accuracy: 82.27, cost: 0.4
Epoch: 4001,
Train: Accuracy: 83.18, cost: 0.37
Validation: Accuracy: 82.4, cost: 0.4
Epoch: 4501,
Train: Accuracy: 83.22, cost: 0.37
Validation: Accuracy: 82.49, cost: 0.4
Epoch: 5001,
Train: Accuracy: 83.26, cost: 0.37
Validation: Accuracy: 82.58, cost: 0.4
Epoch: 5501,
Train: Accuracy: 83.2, cost: 0.37
Validation: Accuracy: 82.58, cost: 0.39
Epoch: 6001,
Train: Accuracy: 83.3, cost: 0.37
Validation: Accuracy: 82.8, cost: 0.39
Epoch: 6501,
Train: Accuracy: 83.44, cost: 0.36
Validation: Accuracy: 82.93, cost: 0.39
Epoch: 7001,
Train: Accuracy: 83.41, cost: 0.36
8
Validation: Accuracy: 82.84, cost: 0.39
Epoch: 7501,
Train: Accuracy: 83.43, cost: 0.36
Validation: Accuracy: 82.76, cost: 0.39
Epoch: 8001,
Train: Accuracy: 83.47, cost: 0.36
Validation: Accuracy: 82.62, cost: 0.39
Epoch: 8501,
Train: Accuracy: 83.44, cost: 0.36
Validation: Accuracy: 82.62, cost: 0.39
Epoch: 9001,
Train: Accuracy: 83.45, cost: 0.36
Validation: Accuracy: 82.67, cost: 0.39
Epoch: 9501,
Train: Accuracy: 83.5, cost: 0.36
Validation: Accuracy: 82.67, cost: 0.39
We can see that the final train and validation accuracies are 83.5 % and 82.67 % respectively.
10 Plotting the loss and accuracy graph for the train and valida-
tion data
We plotted loss and accuracy graphs for train and validation data in order to see if there is over-
fitting. The plots clearly shows there is no overfitting. We will further verify it by checking the
prediction accuracy of Test data. If the accuracy of test data is consistant or close enough with the
training accuracy then we can say the data fitting is generalized.
[ ]: plt.plot(train_losses, label="Train loss")
plt.plot(validation_losses, label="Validation loss")
plt.legend()
plt.title("Losses")
9
Losses
Train loss
1.4 Validation loss
1.2
1.0
0.8
0.6
0.4
0 2000 4000 6000 8000 10000
10
accuracy
82.5
80.0
77.5
75.0
72.5
70.0
67.5 Train accuracy
65.0 Validation accuracy
0 2000 4000 6000 8000 10000
As instructed in the test data matlab file , the expected class labels are 2,2,1,1,2,2,1,1,.....This
would be in our case 1,1,0,0,1,1,0,0,...Now, we will preprocess the test data the simillar way that
we did in case of training data.
[ ]: # labeling the data in the described order 2,2,1,1,2,2,1,1,.....('0' for class␣
,→1 and '1' for class 2)
We checked the accuracy of test data by applying the get_test_accuracy method which we
created inside the DeepNeuralNertwork class. The test accuracy is 83.39 %.
[ ]: dnn.get_test_accuracy(x_test, y_test)
[ ]: 83.39
We can improve the accuracy slightly by tuning some hyperparameter. For example, we can increase
the number of hidden layer. As it is not in the scope of the assignment, I kept the three layer neural
11
network as my final model.
12 The End
[ ]: %%capture
!wget -nc https://raw.githubusercontent.com/brpy/colab-pdf/master/colab_pdf.py
from colab_pdf import colab_pdf
colab_pdf('Using a three layer deep neural network to solve an unsupervised␣
,→learning problem.ipynb')
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
12
[NbConvertApp] Writing 98768 bytes to /content/drive/My Drive/Using a three
layer deep neural network to solve an unsupervised learning problem.pdf
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
[ ]:
13