-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.py
189 lines (160 loc) · 5.78 KB
/
neural_network.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import h5py
import numpy as np
class NeuralNetwork:
def __init__(self):
self.m = 6
self.cost = list()
self.parameters = dict()
@staticmethod
def _sigmoid(Z):
A = 1 / (1 + np.exp(-Z))
return A
def _sigmoid_derivative(self, dA, Z):
A = self._sigmoid(Z)
return dA * A * (1 - A)
@staticmethod
def _relu(Z):
A = np.maximum(0, Z)
return A
@staticmethod
def _relu_derivative(dA, Z):
dZ = np.array(dA, copy=True)
dZ[Z <= 0] = 0
return dZ
def _forward_propagation(self, W, A, B, activation):
Z = np.dot(W, A) + B
if activation == "sigmoid":
A = self._sigmoid(Z)
elif activation == "relu":
A = self._relu(Z)
return A, Z
def _backward_propagation(self, W, A_prev, dZ):
m = self.m
dW = 1/m * np.dot(dZ, A_prev.T)
db = 1/m * np.sum(dZ, axis=1, keepdims=True)
dA_prev = np.dot(W.T, dZ)
return dA_prev, dW, db
# def _update_parameters(self, grads):
# self.parameters['W'+str(l+1)] -= learning_rate * \
# grads['dW'+str(l+1)]
# self.parameters['b'+str(l+1)] -= learning_rate * \
# grads['db'+str(l+1)]
def _initialize_parameters(self, layer_dims):
np.random.seed(1)
params = dict()
L = len(layer_dims)
for l in range(1, L):
params['W'+str(l)] = np.random.randn(layer_dims[l],
layer_dims[l-1]) * 0.01
params['b'+str(l)] = np.zeros((layer_dims[l], 1))
return params
def _compute_cost(self, Y, AL):
m = self.m
cost = (1/m) * (-np.dot(Y, np.log(AL).T) -
np.dot(1-Y, np.log(1-AL).T))
return np.squeeze(cost)
def fit(self, X, Y, layer_dims, iterations=1000, learning_rate=0.01, activation="relu"):
self.m = X.shape[1]
L = len(layer_dims)
layer_dims.insert(0, X.shape[0])
self.parameters = self._initialize_parameters(layer_dims)
cache = {
'A0': X
}
grads = {}
for i in range(iterations):
# Forward Propagation
for l in range(1, L):
cache['A'+str(l)], cache['Z'+str(l)] = self._forward_propagation(
self.parameters['W' + str(l)],
cache['A'+str(l-1)],
self.parameters['b'+str(l)],
activation='relu'
)
cache['A'+str(L)], cache['Z'+str(L)] = self._forward_propagation(
self.parameters['W' + str(L)],
cache['A'+str(L-1)],
self.parameters['b'+str(L)],
activation='sigmoid'
)
# Computing Cost
cost = self._compute_cost(Y, cache['A' + str(L)])
# BackPropagation
AL = cache['A'+str(L)]
grads['dA'+str(L)] = - (np.divide(Y, AL) - np.divide(1-Y, 1-AL))
grads['dZ'+str(L)] = self._sigmoid_derivative(
grads['dA'+str(L)], cache['Z'+str(L)])
grads['dA'+str(L-1)], grads['dW'+str(L)], grads['db' + str(L)] = self._backward_propagation(
self.parameters['W'+str(L)],
cache['A'+str(L-1)],
grads['dZ'+str(L)]
)
for l in reversed(range(1, L)):
if activation == "sigmoid":
grads['dZ'+str(l)] = self._sigmoid_derivative(
grads['dA'+str(l)],
cache['Z'+str(l)]
)
elif activation == "relu":
grads['dZ'+str(l)] = self._relu_derivative(
grads['dA'+str(l)],
cache['Z'+str(l)]
)
grads['dA'+str(l-1)], grads['dW'+str(l)], grads['db'+str(l)] = self._backward_propagation(
self.parameters['W' + str(l)],
cache['A' + str(l-1)],
grads['dZ' + str(l)]
)
# Update Parameters
for l in range(L):
self.parameters['W'+str(l+1)] -= learning_rate * \
grads['dW'+str(l+1)]
self.parameters['b'+str(l+1)] -= learning_rate * \
grads['db'+str(l+1)]
# print Cost
if i % 50 == 0:
self.cost.append(cost)
print(f"The Cost after {i} iteration is: {cost}")
def predict(self, X, y):
params = self.parameters
L = len(params)//2
cache = {
'A0': X
}
m = self.m
p = np.zeros((1, m))
for l in range(1, L):
cache['A'+str(l)], cache['Z'+str(l)] = self._forward_propagation(
params['W' + str(l)],
cache['A'+str(l-1)],
params['b'+str(l)],
activation='relu'
)
cache['A'+str(L)], cache['Z'+str(L)] = self._forward_propagation(
params['W' + str(L)],
cache['A'+str(L-1)],
params['b'+str(L)],
activation='sigmoid'
)
probas = cache['A'+str(L)]
for i in range(0, probas.shape[1]):
if probas[0, i] > 0.5:
p[0, i] = 1
else:
p[0, i] = 0
print("Accuracy: " + str(np.sum((p == y)/m)))
return p
if __name__ == "__main__":
np.random.seed(1)
X = np.array([
[1., 0., 0., 0., -10., 9.],
[0., 1., 0., -6., -4., -1.]
])
Y = np.array([1, 1, 1, 0, 0, 1])
Y = Y.reshape(1, 6)
nn = NeuralNetwork()
print(nn.parameters)
nn.fit(X, Y, layer_dims=[
2, 1], iterations=50, learning_rate=0.01, activation="relu")
print(nn.parameters)
p = nn.predict(X, Y)