-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepNeural.py
228 lines (133 loc) · 7.72 KB
/
DeepNeural.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
221
222
223
224
225
226
227
228
import numpy as np
import math
import pandas as pd
from DeepUtils import initialize_parameters,forward_propagation,compute_cost,backward_propagation,predict
class DeepNeuralNet:
def __init__(self, data_train, data_test):
self._mean = 0
self._std = 0
self.train_x,self.train_y = self.clean_data(data_train, train=True)
self.test_x,self.test_y = self.clean_data(data_test)
def clean_data(self,data,train=False):
X_vars = data.columns[0:len(data.columns)-1]
y_vars = data.columns[len(data.columns)-1:]
X = pd.DataFrame(data, columns = X_vars)
y = pd.DataFrame(data, columns = y_vars)
if train == True:
self._mean = X.mean()
self._std = X.std()
X = (X - self._mean)/self._std
X = X.T
y = y.T
X = X.values
y = y.values
return X,y
def update_parameters_with_gd(self, parameters, grads, learning_rate):
L = len(parameters) // 2
for l in range(L):
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 random_mini_batches(self, X, Y, mini_batch_size = 64):
m = X.shape[1]
mini_batches = []
permutation = list(np.random.permutation(m))
shuffled_X = X[:, permutation]
shuffled_Y = Y[:, permutation]
num_complete_minibatches = math.floor(m/mini_batch_size)
for k in range(0, num_complete_minibatches):
mini_batch_X = shuffled_X[:, k*mini_batch_size:(k+1)*mini_batch_size]
mini_batch_Y = shuffled_Y[:, k*mini_batch_size:(k+1)*mini_batch_size]
mini_batch = (mini_batch_X, mini_batch_Y)
mini_batches.append(mini_batch)
if m % mini_batch_size != 0:
mini_batch_X = shuffled_X[:,num_complete_minibatches*mini_batch_size:]
mini_batch_Y = shuffled_Y[:,num_complete_minibatches*mini_batch_size:]
mini_batch = (mini_batch_X, mini_batch_Y)
mini_batches.append(mini_batch)
return mini_batches
def initialize_velocity(self, parameters):
L = len(parameters) // 2
v = {}
for l in range(L):
v["dW" + str(l+1)] = np.zeros((parameters["W"+str(l+1)].shape))
v["db" + str(l+1)] = np.zeros((parameters["b"+str(l+1)].shape))
return v
def update_parameters_with_momentum(self, parameters, grads, v, beta, learning_rate):
L = len(parameters) // 2
for l in range(L):
v["dW" + str(l+1)] = beta*v["dW" + str(l+1)] + (1-beta)*grads["dW"+str(l+1)]
v["db" + str(l+1)] = beta*v["db" + str(l+1)] + (1-beta)*grads["db"+str(l+1)]
parameters["W" + str(l+1)] = parameters["W" + str(l+1)] - learning_rate*v["dW" + str(l+1)]
parameters["b" + str(l+1)] = parameters["b" + str(l+1)] - learning_rate*v["db" + str(l+1)]
return parameters, v
def initialize_adam(self, parameters) :
L = len(parameters) // 2
v = {}
s = {}
for l in range(L):
v["dW" + str(l+1)] = np.zeros((parameters["W"+str(l+1)].shape))
v["db" + str(l+1)] = np.zeros((parameters["b"+str(l+1)].shape))
s["dW" + str(l+1)] = np.zeros((parameters["W"+str(l+1)].shape))
s["db" + str(l+1)] = np.zeros((parameters["b"+str(l+1)].shape))
return v, s
def update_parameters_with_adam(self, parameters, grads, v, s, t, learning_rate = 0.01,
beta1 = 0.9, beta2 = 0.999, epsilon = 1e-8):
L = len(parameters) // 2
v_corrected = {}
s_corrected = {}
for l in range(L):
v["dW" + str(l+1)] = beta1*v["dW" + str(l+1)] + (1-beta1)*grads["dW"+str(l+1)]
v["db" + str(l+1)] = beta1*v["db" + str(l+1)] + (1-beta1)*grads["db"+str(l+1)]
v_corrected["dW" + str(l+1)] = v["dW"+str(l+1)]/(1-(beta1)**t)
v_corrected["db" + str(l+1)] = v["db"+str(l+1)]/(1-(beta1)**t)
s["dW" + str(l+1)] = beta2*s["dW" + str(l+1)] + (1-beta2)*(grads["dW"+str(l+1)]**2)
s["db" + str(l+1)] = beta2*s["db" + str(l+1)] + (1-beta2)*(grads["db"+str(l+1)]**2)
s_corrected["dW" + str(l+1)] = s["dW"+str(l+1)]/(1-(beta2)**t)
s_corrected["db" + str(l+1)] = s["db"+str(l+1)]/(1-(beta2)**t)
parameters["W" + str(l+1)] = parameters["W" + str(l+1)] - learning_rate*v_corrected["dW"+str(l+1)]/(np.sqrt(s_corrected["dW"+str(l+1)])+epsilon)
parameters["b" + str(l+1)] = parameters["b" + str(l+1)] - learning_rate*v_corrected["db"+str(l+1)]/(np.sqrt(s_corrected["db"+str(l+1)])+epsilon)
return parameters, v, s
# def model(self, X, Y, layers_dims, optimizer, learning_rate = 0.0007, mini_batch_size = 64, beta = 0.9,
# beta1 = 0.9, beta2 = 0.999, epsilon = 1e-8, num_epochs = 10000, print_cost = True):
# L = len(layers_dims)
# costs = []
# t = 0
# m = X.shape[1]
# parameters = initialize_parameters(layers_dims)
# if optimizer == "gd":
# pass
# elif optimizer == "momentum":
# v = self.initialize_velocity(parameters)
# elif optimizer == "adam":
# v, s = self.initialize_adam(parameters)
# for i in range(num_epochs):
# minibatches = self.random_mini_batches(X, Y, mini_batch_size)
# cost_total = 0
# for minibatch in minibatches:
# (minibatch_X, minibatch_Y) = minibatch
# a3, caches = forward_propagation(minibatch_X, parameters)
# cost_total += compute_cost(a3, minibatch_Y)
# grads = backward_propagation(minibatch_X, minibatch_Y, caches)
# if optimizer == "gd":
# parameters = self.update_parameters_with_gd(parameters, grads, learning_rate)
# elif optimizer == "momentum":
# parameters, v = self.update_parameters_with_momentum(parameters, grads, v, beta, learning_rate)
# elif optimizer == "adam":
# t = t + 1
# parameters, v, s = self.update_parameters_with_adam(parameters, grads, v, s,
# t, learning_rate, beta1, beta2, epsilon)
# cost_avg = cost_total / m
# if print_cost and i % 1000 == 0:
# print ("Cost after epoch %i: %f" %(i, cost_avg))
# if print_cost and i % 100 == 0:
# costs.append(cost_avg)
# return parameters
# data_train = pd.read_csv('log_train.csv')
# data_test = pd.read_csv('log_test.csv')
# deep_net = DeepNeuralNet(data_train, data_test)
# # train 3-layer model
# layers_dims = [deep_net.train_x.shape[0], 5, 2, 1]
# parameters = deep_net.model(deep_net.train_x, deep_net.train_y, layers_dims, beta = 0.9, optimizer = "momentum")
# # Predict
# predictions = predict(deep_net.test_x, deep_net.test_y, parameters)