-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.py
executable file
·220 lines (182 loc) · 7 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import numpy as np
# Function
def sigmoid(z):
return 1 / (1 + np.exp(-1 * z))
def sigmoid_prime(z):
return sigmoid(z) * (1 - sigmoid(z))
def relu(z):
return np.maximum(0, z)
def relu_prime(z):
return np.array(z > 0, int)
def tanh(z):
return np.tanh(z)
def tanh_prime(z):
return 1 - np.power(tanh(z), 2)
# Model
class NN(object):
#init with data set and layer dims
def __init__(self, train_set_x, train_set_y, layer_dims, standardize, activation_fn = "relu"):
"""
:param train_set_x: training set X (n, m)
:param train_set_y: training set Y (1, m)
:param layer_dims: python array (list) containing the dimensions of each layer in our network
:param standardize: standardize function
:param activation_fn: activation function
"""
self._standardize = standardize
self._train_set_x = self._standardize_data(train_set_x)
self._train_set_y = train_set_y
self._activation_fn = activation_fn
self._params = self._init_params(layer_dims)
def _standardize_data(self, X):
return self._standardize(X)
def _init_params(self, layer_dims):
params = []
n_x = self._train_set_x.shape[0]
n_y = self._train_set_y.shape[0]
params.append({
'W': np.random.randn(layer_dims[0], n_x) * 0.01,
'b': np.zeros((layer_dims[0], 1))
})
for i in range(1, len(layer_dims)):
layer_params = {
'W': np.random.randn(layer_dims[i], layer_dims[i - 1]) * 0.01,
'b': np.zeros((layer_dims[i], 1))
}
params.append(layer_params)
params.append({
'W': np.random.randn(n_y, layer_dims[len(layer_dims) - 1]),
'b': np.zeros((n_y, 1))
})
return params
# function
def _cost_function(self, y_hat, lambd):
m = self._train_set_x.shape[1]
logprobs = np.multiply(np.log(y_hat), self._train_set_y) \
+ np.multiply(np.log(1 - y_hat), 1 - self._train_set_y)
cost = -1 / m * np.sum(logprobs)
#Regularzation
reg = 0
for i in range(len(self._params)):
reg += np.sum(np.multiply(self._params[i]['W'], self._params[i]['W']))
cost += lambd / (2 * m) * reg
return cost
#training
def _liner_forward(self, A, W, b):
Z = np.dot(W, A) + b
return Z
def _linear_activation_forward(self, A_prev, W, b, activation, keep_prop):
Z = self._liner_forward(A_prev, W, b)
A = None
if activation == "sigmoid":
A = sigmoid(Z)
elif activation == "relu":
A = relu(Z)
elif activation == "tanh":
A = tanh(Z)
D = np.random.rand(A.shape[0], A.shape[1]) < keep_prop
A = np.multiply(A, D)
A /= keep_prop
return A, Z, D
def _forward_propagation(self, X, keep_prop = 1):
A = X
caches = []
L = len(self._params)
for i in range(L - 1):
A, Z, D = self._linear_activation_forward(A, self._params[i]['W'], self._params[i]['b'], self._activation_fn, keep_prop)
caches.append({
'A': A,
'Z': Z,
'D': D
})
#Final layer keep_prop must be 1.
AL, ZL, DL = self._linear_activation_forward(A, self._params[L - 1]['W'], self._params[L - 1]['b'], "sigmoid", 1)
caches.append({
'A': AL,
'Z': ZL,
'D': DL
})
return AL, caches
def _liner_backward(self, dZ, A_prev, W, b):
m = self._train_set_y.shape[1]
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 _linear_activation_backward(self, dA, A_prev, Z, D, W, b, activation, keep_prop, lambd):
dA = np.multiply(dA, D)
dA /= keep_prop
dZ = None
if activation == "relu":
dZ = dA * relu_prime(Z)
elif activation == "tanh":
dZ = dA * tanh_prime(Z)
elif activation == "sigmoid":
dZ = dA * sigmoid_prime(Z)
dA_prev, dW, db = self._liner_backward(dZ, A_prev, W, b)
m = self._train_set_x.shape[1]
dW += lambd / m * W
return dA_prev, dW, db
def _backward_propagation(self, AL, caches, keep_prop, lambd):
grads = []
dAL = -1 * self._train_set_y / AL + (1 - self._train_set_y) / (1 - AL)
L = len(caches)
Z = caches[L - 1]['Z']
D = caches[L - 1]['D']
A_prev = caches[L - 2]['A']
W = self._params[L - 1]['W']
b = self._params[L - 1]['b']
dA_prev, dW, db = self._linear_activation_backward(dAL, A_prev, Z, D, W, b, "sigmoid", 1, lambd)
grads.insert(0, {
'dW': dW,
'db': db
})
for i in reversed(range(1, L - 1)):
Z = caches[i]['Z']
D = caches[i]['D']
A_prev = caches[i - 1]['A']
W = self._params[i]['W']
b = self._params[i]['b']
dA_prev, dW, db = self._linear_activation_backward(dA_prev, A_prev, Z, D, W, b, self._activation_fn, keep_prop, lambd)
grads.insert(0, {
'dW': dW,
'db': db
})
Z = caches[0]['Z']
D = caches[0]['D']
A_prev = self._train_set_x
W = self._params[0]['W']
b = self._params[0]['b']
dA_prev, dW, db = self._linear_activation_backward(dA_prev, A_prev, Z, D, W, b, self._activation_fn, keep_prop, lambd)
grads.insert(0, {
'dW': dW,
'db': db
})
return grads
def _update_params(self, grads, learning_rate):
L = len(grads)
for i in range(L):
self._params[i]['W'] -= learning_rate * grads[i]['dW']
self._params[i]['b'] -= learning_rate * grads[i]['db']
return
def _training_iterate(self, num_iterations, learning_rate, keep_prop, lambd):
for i in range(0, num_iterations):
Y_hat, caches = self._forward_propagation(self._train_set_x, keep_prop)
cost = self._cost_function(Y_hat, lambd)
grads = self._backward_propagation(Y_hat, caches, keep_prop, lambd)
self._update_params(grads, learning_rate)
if i % 100 == 0:
print("Cost after iteration %i: %f" % (i, cost))
return cost
def _predict(self, X):
y_hat, caches = self._forward_propagation(X)
Y_prediction = np.array(y_hat > 0.5, int)
return Y_prediction
# API
def train_model_run(self, num_iterations = 1001, learning_rate = 0.01, keep_prop = 0.8, lambd = 0.7):
self._training_iterate(num_iterations, learning_rate, keep_prop, lambd)
def predict_model_run(self, data_x, data_y):
X = self._standardize_data(data_x)
Y_prediction = self._predict(X)
print ("Accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction - data_y)) * 100))
return Y_prediction