-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.py
176 lines (126 loc) · 4.95 KB
/
NN.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
import numpy as np
import math
class NN:
def __init__(self):
self.param = 0
def initialize_parametrs(self):
self.parametrs = {}
self.parametrs['W1'] = np.random.randn(8,12) * 0.001
self.parametrs['b1'] = np.zeros((8,1))
self.parametrs['W2'] = np.random.randn(8,8) * 0.001
self.parametrs['b2'] = np.zeros((8,1))
self.parametrs['W3'] = np.random.randn(4,8) * 0.001
self.parametrs['b3'] = np.zeros((4,1))
return self.parametrs
def forward_prop(self,Ap,W,b):
z = np.dot(W,Ap) + b
forward_data = (Ap, W ,b)
return z,forward_data
def forward_activation(self,Ap,W,b,activation):
z, forward_data = self.forward_prop(Ap,W,b)
if activation == 'relu':
A = relu(z)
elif activation == 'sigmoid':
A = sigmoid(z)
return A ,(forward_data,z)
def forwardModel(self,X,parameters):
datas = []
A = X
for i in range(2):
Ap = A
if i == 1:
A, data = self.forward_activation(Ap, parameters['W'+str(i+1)], parameters['b'+str(i+1)], 'sigmoid')
datas.append(data)
else:
A, data = self.forward_activation(Ap, parameters['W'+str(i+1)], parameters['b'+str(i+1)], 'relu')
datas.append(data)
yhat, data = self.forward_activation(Ap, parameters['W'+str(3)], parameters['b'+str(3)], 'relu')
datas.append(data)
return yhat,datas
def cost(self,yhat,y):
m = y.shape[1]
cost = (yhat - y)**2
cost = sum(sum(cost))/(4*m)
return cost
def back_prop(self,dz,data):
Ap ,w ,b = data
m = Ap.shape[1]
dw = np.dot(dz,Ap.T)/m
db = np.sum(dz,axis=1,keepdims=True)/m
dAp = np.dot(w.T,dz)
return dAp, dw, db
def back_activation(self,dA,data,activation):
forward_data ,z = data
if activation == 'relu':
dz = relu_backward(dA, z)
dAp, dw, db = self.back_prop(dz, forward_data)
else:
dz = sigmoid_backward(dA, z)
dAp, dw, db = self.back_prop(dz, forward_data)
return dAp, dw ,db
def backwardModel(self,yhat,y,datas):
dyhat = 2*(yhat-y)
grads = {}
current_data = datas[-1]
grads['dA2'], grads['dW3'], grads['db3'] = self.back_activation(dyhat,current_data,'relu')
for l in reversed(range(2)):
current_data = datas[l]
if l == 1:
dAp_temp, dW_temp, db_temp = self.back_activation(grads["dA"+ str(l+1)],current_data,'sigmoid')
else:
dAp_temp, dW_temp, db_temp = self.back_activation(grads["dA"+ str(l+1)],current_data,'relu')
grads["dA" + str(l)] = dAp_temp
grads["dW" + str(l + 1)] = dW_temp
grads["db" + str(l + 1)] = db_temp
return grads
def parametrs_Update(self,parameters,grads,learning_rate):
for l in range(3):
parameters["W" + str(l+1)] = parameters["W" + str(l+1)] - learning_rate * grads["dW" + str(l+1)]
parameters["b" + str(l+1)] = parameters["b" + str(l+1)] - learning_rate * grads["db" + str(l+1)]
return parameters
def run(self,X,Y):
if not self.param:
self.parametrs = self.initialize_parametrs()
self.param = 1
yhat, datas = self.forwardModel(X,self.parametrs)
grades = self.backwardModel(yhat,Y,datas)
cost = self.cost(yhat,Y)
self.parametrs = self.parametrs_Update(self.parametrs,grades,0.05)
return self.parametrs, cost
def predict(self,X,Y):
yhat, datas = self.forwardModel(X,self.parametrs)
cost = self.cost(yhat,Y)
yhat = yhat.T
for i in range(yhat.shape[0]):
for j in range(yhat.shape[1]):
yhat[i][j] = yhat[i][j]
ceil = math.ceil(yhat[i][j])
floor = math.floor(yhat[i][j])
yhat[i][j] = floor if abs(ceil - yhat[i][j])> abs(floor - yhat[i][j]) else ceil
return cost,yhat
def relu(z):
for i in range(len(z)):
for j in range(len(z[i])):
z[i][j] = max(0,z[i][j])
return z
def relu_backward(dA,z):
gprimz = np.zeros((z.shape[0],z.shape[1]))
for i in range(len(z)):
for j in range(len(z[i])):
if j >= 0:
gprimz[i][j] = 1
else:
gprimz[i][j] = 0
return np.multiply(dA,gprimz)
def sigmoid(z):
for i in range(len(z)):
for j in range(len(z[i])):
z[i][j] = max(0,z[i][j])
# z[i][j] = 1 / (1 + math.exp(-z[i][j]))
return 1 / (1 + math.exp(-z[i][j]))
def sigmoid_backward(dA,z):
gprimz = np.zeros((z.shape[0],z.shape[1]))
for i in range(len(z)):
for j in range(len(z[i])):
gprimz[i][j] = 1 / (1 + math.exp(-z[i][j])) * (1 - (1 / (1 + math.exp(-z[i][j]))))
return np.multiply(dA,gprimz)