-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.py
141 lines (110 loc) · 4.51 KB
/
NeuralNetwork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
heart_df = pd.read_csv('heart.csv')
X = heart_df.drop(columns=['target'])
y_label = heart_df['target'].values.reshape(X.shape[0], 1)
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y_label, test_size=0.2, random_state=2)
sc = StandardScaler()
sc.fit(Xtrain)
Xtrain = sc.transform(Xtrain)
Xtest = sc.transform(Xtest)
print(f"Shape of train set is {Xtrain.shape}")
print(f"Shape of test set is {Xtest.shape}")
print(f"Shape of train label is {ytrain.shape}")
print(f"Shape of test labels is {ytest.shape}")
class NeuralNetwork():
def __init__(self, layers=[13, 8, 1], learning_rate=0.001, iterations=100):
self.params = {}
self.learning_rate = learning_rate
self.iterations = iterations
self.loss = []
self.sample_size = None
self.layers = layers
self.X = None
self.y = None
def init_weights(self):
np.random.seed(1)
self.params["W1"] = np.random.randn(self.layers[0], self.layers[1])
self.params['b1'] = np.random.randn(self.layers[1], )
self.params['W2'] = np.random.randn(self.layers[1], self.layers[2])
self.params['b2'] = np.random.randn(self.layers[2], )
def sigmoid(self, Z):
return 1 / (1 + np.exp(-Z))
def relu(self, Z):
return np.maximum(0, Z)
def dRelu(self, x):
x[x <= 0] = 0
x[x > 0] = 1
return x
def eta(self, x):
ETA = 0.0000000001
return np.maximum(x, ETA)
def entropy_loss(self, y, yhat):
nsample = len(y)
yhat_inv = 1.0 - yhat
y_inv = 1.0 - y
yhat = self.eta(yhat)
yhat_inv = self.eta(yhat_inv)
loss = -1 / nsample * (np.sum(np.multiply(np.log(yhat), y) + np.multiply((y_inv), np.log(yhat_inv))))
return loss
def forward_propagation(self):
Z1 = self.X.dot(self.params['W1']) + self.params['b1']
A1 = self.relu(Z1)
Z2 = A1.dot(self.params['W2']) + self.params['b2']
yhat = self.sigmoid(Z2)
loss = self.entropy_loss(self.y, yhat)
self.params['Z1'] = Z1
self.params['Z2'] = Z2
self.params['A1'] = A1
return yhat, loss
def backword_propogation(self, yhat):
y_inv = 1 - self.y
yhat_inv = 1 - yhat
dl_wrt_yhat = np.divide(y_inv, self.eta(yhat_inv)) - np.divide(self.y, self.eta(yhat))
dl_wrt_sig = yhat * (yhat_inv)
dl_wrt_z2 = dl_wrt_yhat * dl_wrt_sig
dl_wrt_A1 = dl_wrt_z2.dot(self.params['W2'].T)
dl_wrt_w2 = self.params['A1'].T.dot(dl_wrt_z2)
dl_wrt_b2 = np.sum(dl_wrt_z2, axis=0, keepdims=True)
dl_wrt_z1 = dl_wrt_A1 * self.dRelu(self.params['Z1'])
dl_wrt_w1 = self.X.T.dot(dl_wrt_z1)
dl_wrt_b1 = np.sum(dl_wrt_z1, axis=0, keepdims=True)
self.params['W1'] = self.params['W1'] - self.learning_rate * dl_wrt_w1
self.params['W2'] = self.params['W2'] - self.learning_rate * dl_wrt_w2
self.params['b1'] = self.params['b1'] - self.learning_rate * dl_wrt_b1
self.params['b2'] = self.params['b2'] - self.learning_rate * dl_wrt_b2
def fit(self, X, y):
self.X = X
self.y = y
self.init_weights()
for i in range(self.iterations):
yhat, loss = self.forward_propagation()
self.backword_propogation(yhat)
self.loss.append(loss)
def predict(self, X):
Z1 = X.dot(self.params['W1']) + self.params['b1']
A1 = self.relu(Z1)
Z2 = A1.dot(self.params['W2']) + self.params['b2']
pred = self.sigmoid(Z2)
return np.round(pred)
def accuracy(self, y, yhat):
accuracy = int(sum(y == yhat) / len(y) * 100)
return accuracy
def plot_loss(self):
plt.plot(self.loss)
plt.xlabel("Iteration")
plt.ylabel("logloss")
plt.title("Loss curve for training")
plt.show()
model = NeuralNetwork(layers=[13, 10, 1], learning_rate=0.002, iterations=1000)
model.fit(Xtrain, ytrain)
model.plot_loss()
train_pred = model.predict(Xtrain)
test_pred = model.predict(Xtest)
print("\nTraining accuracy is : {}".format(model.accuracy(ytrain, train_pred)))
print("\nTesting accuracy is : {}".format(model.accuracy(ytest, test_pred)))