-
Notifications
You must be signed in to change notification settings - Fork 0
/
HookeJeeves.py
271 lines (205 loc) · 8.58 KB
/
HookeJeeves.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
from aa222_finalproject_regression import BuildModel, evaluateModelDesign, datasetLoad, evaluateModelProxy, evaluateModelProxyAlt, mnistLoad
from random import randint
from scipy import linalg
import math as math
#We already know the size of the first and last layer n
#We specify the MAX amount of layers we want but Hooke-Jeeves can vary that if it wants
def HookeJeeves(input_neurons, output_neurons, alpha, max_layers):
'''Gives you optimal network structure from an initial guess'''
#Hooking and Jeeving
score_hist = []
proxy_score_hist = []
ahist = []
a = np.array([randint(0, 10) for p in range(0, max_layers + 2)])
a[0] = input_neurons
a[1] = randint(1, alpha)
a[max_layers + 1] = output_neurons
a = np.array([28, 10, 10, 10, 10, 10, 2])
#After the first zero make every next value also 0 except for the last one
idx = np.array(np.where(a == 0))
if idx.size > 0:
for i in range(idx[0, 0], len(a)-1):
a[i] = 0
a_best = a
x, y, xtest, ytest = datasetLoad()
# x, y, xtest, ytest = mnistLoad()
model = BuildModel(a, activation_function='relu', regression=True)
best_score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10, regression=True)
# best_score, KH = evaluateModelProxyAlt(a, xtest, 80)
# #Then we start the loop
while alpha >= 1:
improved = False
score_hist.append(best_score)
ahist.append(a_best.tolist())
#CHECK THE ENTIRE VECTOR
for i in range(1, len(a)-1): #The range specifically does not change the first or the last value
if a[i] <= 2*alpha:
if a[i] == 0:
a[i] += alpha
model = BuildModel(a, activation_function='relu', regression=True)
score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10, regression=True)
# score, KH = evaluateModelProxyAlt(a, xtest, 80)
if score < best_score:
a_best = np.copy(a)
best_score = score
improved = True
a[i] -= alpha
break
a[i] += alpha
model = BuildModel(a, activation_function='relu', regression=True)
score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10, regression=True)
# score, KH = evaluateModelProxyAlt(a, xtest, 80)
if score < best_score:
a_best = np.copy(a)
best_score = score
improved = True
a[i] -= alpha
elif a[i] > 2*alpha:
#Upper Branch
a[i] += alpha
model = BuildModel(a, activation_function='relu', regression=True)
score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10, regression=True)
# score, KH = evaluateModelProxyAlt(a, xtest, 80)
if score < best_score:
a_best = np.copy(a)
best_score = score
improved = True
#Lower Branch
a[i] -= 2*alpha
model = BuildModel(a, activation_function='relu', regression=True)
score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10, regression=True)
# score, KH = evaluateModelProxyAlt(a, xtest, 80)
if score < best_score:
a_best = np.copy(a)
best_score = score
improved = True
#Return to the a value
a[i] += alpha
a = a_best
if improved == False:
alpha = 0.5*alpha
if alpha >= 1:
alpha = round(alpha)
else:
break
return a, score_hist, ahist, proxy_score_hist
def PosDefMatrix(max_layers):
A = np.array(np.random.randint(10, size = (max_layers,max_layers)))
B = np.multiply(A.transpose(), A)
U, sigma, V = linalg.svd(B)
U = np.around(U)
#Adding the additional search direction
s2 = np.sum(U,axis=1)/math.sqrt(max_layers)
s2 = np.around(s2)
s2.shape = (max_layers, 1)
U = np.append(U, s2, axis=1)
#Add zeroes to index 0 and end of each vector to make sure we do not change the input or output dimensions
U = np.pad(U, ([1, 1], [0, 0]), mode='constant')
U = U.astype(int)
return U
def HookeJeevesSVD(input_neurons, output_neurons, alpha, max_layers):
'''Gives you optimal network structure from an initial guess'''
score_hist = []
proxy_score_hist = []
ahist = []
#Hooking and Jeeving
a = np.array([randint(0, 10) for p in range(0, max_layers + 2)])
a[0] = input_neurons
a[1] = randint(1, alpha)
a[max_layers + 1] = output_neurons
a = np.array([28, 10, 10, 10, 10, 10, 2])
#After the first zero make every next value also 0 except for the last one
idx = np.array(np.where(a == 0))
if idx.size > 0:
for i in range(idx[0, 0], len(a)-1):
a[i] = 0
a_best = a
x, y, xtest, ytest = datasetLoad()
# x, y, xtest, ytest = mnistLoad()
model = BuildModel(a, activation_function='relu', regression=True)
# best_score, KH = evaluateModelProxy(a, xtest, 80)
best_score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10)
# Then we start the loop
while alpha >= 1:
score_hist.append(best_score)
ahist.append(a_best.tolist())
U = PosDefMatrix(max_layers)
search = alpha*U
improved = False
#CHECK THE ENTIRE VECTOR
for i in range(max_layers): #The range specifically does not change the first or the last value
step = search[:, i]
a += step
#Replace all negatives with 0
a[a<0] = 0
idx = np.array(np.where(a == 0))
if idx.size > 0:
for i in range(idx[0, 0], len(a)-1):
a[i] = 0
if a[1] == 0:
a[1] = alpha
model = BuildModel(a, activation_function='relu', regression=True)
score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10)
# score, KH = evaluateModelProxyAlt(a, xtest, 80)
if score < best_score:
best_score = score
a_best = a
improved = True
a -= step
a = a_best
search = U
if improved == False:
alpha = 0.5*alpha
if alpha >= 1:
alpha = round(alpha)
else:
break
return a, score_hist
def HookeJeevesPop(a, alpha=10):
'''Gives you optimal network structure from an initial guess'''
#Hooking and Jeeving
max_layers = len(a) - 2
#After the first zero make every next value also 0 except for the last one
idx = np.array(np.where(a == 0))
if idx.size > 0:
for i in range(idx[0, 0], len(a)-1):
a[i] = 0
a_best = a
x, y, xtest, ytest = datasetLoad()
model = BuildModel(a, activation_function='relu')
best_score, KH = evaluateModelProxy(a, xtest, 80)
# best_score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10)
# Then we start the loop
while alpha >= 1:
U = PosDefMatrix(max_layers)
search = alpha*U
improved = False
#CHECK THE ENTIRE VECTOR
for i in range(max_layers): #The range specifically does not change the first or the last value
step = search[:, i]
a += step
#Replace all negatives with 0
a[a<0] = 0
idx = np.array(np.where(a == 0))
if idx.size > 0:
for i in range(idx[0, 0], len(a)-1):
a[i] = 0
if a[1] == 0:
a[1] = alpha
# score = evaluateModelDesign(model, a, x, y, xtest, ytest, save=False, training_epochs=10)
score, KH = evaluateModelProxy(a, xtest, 80)
if score < best_score:
best_score = score
a_best = a
improved = True
a -= step
a = a_best
search = U
if improved == False:
alpha = 0.5*alpha
if alpha >= 1:
alpha = round(alpha)
else:
break
return a