-
Notifications
You must be signed in to change notification settings - Fork 1
/
fully_connected_nn.py
213 lines (145 loc) · 6.75 KB
/
fully_connected_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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 29 20:08:01 2021
@author: laptop
"""
import numpy as np
def sigmoid(Z):
return 1/(1+np.exp(-Z))
def relu(Z):
return np.maximum(0,Z)
def sigmoid_backward(dA, Z):
sig = sigmoid(Z)
return dA * sig * (1 - sig)
def relu_backward(dA, Z):
dZ = np.array(dA, copy = True)
dZ[Z <= 0] = 0
return dZ
def init_layers(nn_architecture, seed = 99):
np.random.seed(seed)
number_of_layers = len(nn_architecture)
params_values = {}
for idx, layer in enumerate(nn_architecture):
layer_idx = idx + 1
layer_input_size = layer["input_dim"]
layer_output_size = layer["output_dim"]
params_values['W' + str(layer_idx)] = np.random.randn(
layer_output_size, layer_input_size) * 0.1
params_values['b' + str(layer_idx)] = np.random.randn(
layer_output_size, 1) * 0.1
return params_values
def single_layer_forward_propagation(A_prev, W_curr, b_curr, activation="relu"):
Z_curr = np.dot(W_curr, A_prev) + b_curr
if activation == "relu":
activation_func = relu
elif activation == "sigmoid":
activation_func = sigmoid
else:
raise Exception('Non-supported activation function')
return activation_func(Z_curr), Z_curr
def full_forward_propagation(X, params_values, nn_architecture):
memory = {}
A_curr = X
for idx, layer in enumerate(nn_architecture):
layer_idx = idx + 1
A_prev = A_curr
activ_function_curr = layer["activation"]
W_curr = params_values["W" + str(layer_idx)]
b_curr = params_values["b" + str(layer_idx)]
A_curr, Z_curr = single_layer_forward_propagation(A_prev, W_curr, b_curr, activ_function_curr)
memory["A" + str(idx)] = A_prev
memory["Z" + str(layer_idx)] = Z_curr
return A_curr, memory, W_curr
def get_cost_value(Y_hat, Y, eps = 0.001):
# number of examples
m = Y_hat.shape[1]
cost = -1 / m * (np.dot(Y, np.log(Y_hat + eps).T) + np.dot(1 - Y, np.log(1 - Y_hat + eps).T))
return np.squeeze(cost)
def compute_cost_with_regularization(Y_hat, Y, W_curr, W_tre, lambd, eps= 0.001):
cros_cost = get_cost_value(Y_hat, Y, eps)
Weight_elim = np.sum((np.square(W_curr) / np.square(W_tre)) / (1 + np.square(W_curr) / np.square(W_tre))) * (lambd)
cost_r = cros_cost + Weight_elim
cost_r = np.squeeze(cost_r)
return cost_r
def convert_prob_into_class(probs):
probs_ = np.copy(probs)
probs_[probs_ > 0.5] = 1
probs_[probs_ <= 0.5] = 0
return probs_
def get_accuracy_value(Y_hat, Y):
Y_hat_ = convert_prob_into_class(Y_hat)
return (Y_hat_ == Y).all(axis=0).mean()
def get_performance_value(Y_hat, Y):
Y_hat_ = convert_prob_into_class(Y_hat)
TP = ((Y_hat_ == 1) & (Y == 1)).sum()
FP = ((Y_hat_ == 1) & (Y == 0)).sum()
#TN = ((Y_hat_ == 0) & (Y == 0)).sum()
FN = ((Y_hat_ == 0) & (Y == 1)).sum()
precision = TP / (TP + FP)
recall = TP / (TP + FN)
F1 = 2 * ((recall * precision) / (recall + precision) )
return precision, recall, F1
def single_layer_backward_propagation(dA_curr, W_curr, b_curr, Z_curr, A_prev, activation="relu"):
m = A_prev.shape[1]
if activation == "relu":
backward_activation_func = relu_backward
elif activation == "sigmoid":
backward_activation_func = sigmoid_backward
else:
raise Exception('Non-supported activation function')
dZ_curr = backward_activation_func(dA_curr, Z_curr)
dW_curr = np.dot(dZ_curr, A_prev.T) / m
db_curr = np.sum(dZ_curr, axis=1, keepdims=True) / m
dA_prev = np.dot(W_curr.T, dZ_curr)
return dA_prev, dW_curr, db_curr
def full_backward_propagation(Y_hat, Y, memory, params_values, nn_architecture, eps = 0.000000000001):
grads_values = {}
m = Y.shape[1]
Y = Y.reshape(Y_hat.shape)
dA_prev = - (np.divide(Y, Y_hat + eps) - np.divide(1 - Y, 1 - Y_hat + eps))
for layer_idx_prev, layer in reversed(list(enumerate(nn_architecture))):
layer_idx_curr = layer_idx_prev + 1
activ_function_curr = layer["activation"]
dA_curr = dA_prev
A_prev = memory["A" + str(layer_idx_prev)]
Z_curr = memory["Z" + str(layer_idx_curr)]
W_curr = params_values["W" + str(layer_idx_curr)]
b_curr = params_values["b" + str(layer_idx_curr)]
dA_prev, dW_curr, db_curr = single_layer_backward_propagation(
dA_curr, W_curr, b_curr, Z_curr, A_prev, activ_function_curr)
grads_values["dW" + str(layer_idx_curr)] = dW_curr
grads_values["db" + str(layer_idx_curr)] = db_curr
return grads_values, dW_curr
def advsry(X, epsi, grad_v):
pertubated_data = X + epsi * np.sign(grad_v[:, None])
pertubated_data = np.clip(pertubated_data, 0, 1)
return pertubated_data
def update(params_values, grads_values, nn_architecture, learning_rate):
for layer_idx, layer in enumerate(nn_architecture, 1):
params_values["W" + str(layer_idx)] -= learning_rate * grads_values["dW" + str(layer_idx)]
params_values["b" + str(layer_idx)] -= learning_rate * grads_values["db" + str(layer_idx)]
return params_values
def train(X, Y, nn_architecture, epochs, learning_rate, batch_size, verbose=False, callback=None):
params_values = init_layers(nn_architecture, 2)
cost_history = []
accuracy_history = []
examples_size = X.shape[1]
batch_number = examples_size // batch_size
for i in range(epochs):
batch_idx = epochs % batch_number
# Mini-Batch
X_batch = X[:, batch_idx * batch_size : (batch_idx +1) * batch_size]
Y_batch = Y[:, batch_idx * batch_size : (batch_idx +1) * batch_size]
Y_hat, cashe, _ = full_forward_propagation(X_batch, params_values, nn_architecture)
cost = get_cost_value(Y_hat, Y_batch)
cost_history.append(cost)
accuracy = get_accuracy_value(Y_hat, Y_batch)
accuracy_history.append(accuracy)
grads_values, grad_a = full_backward_propagation(Y_hat, Y_batch, cashe, params_values, nn_architecture)
params_values = update(params_values, grads_values, nn_architecture, learning_rate)
if(i % 50 == 0):
if(verbose):
print("Iteration: {:05} - cost: {:.5f} - accuracy: {:.5f}".format(i, cost, accuracy))
if(callback is not None):
callback(i, params_values)
return params_values, cost_history, accuracy_history, grad_a