forked from snyderg2/car_price_nnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizers.py
298 lines (232 loc) · 11.3 KB
/
optimizers.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import copy
import numpy as np
import time
import math
import sys
floatPrecision = sys.float_info.epsilon
class Optimizers():
def __init__(self, all_weights):
'''all_weights is a vector of all of a neural networks weights concatenated into a one-dimensional vector'''
self.all_weights = all_weights
self.scg_initialized = False
# The following initializations are only used by adam.
# Only initializing m, v, beta1t and beta2t here allows multiple calls to adam to handle training
# with multiple subsets (batches) of training data.
self.mt = np.zeros_like(all_weights)
self.vt = np.zeros_like(all_weights)
self.beta1 = 0.9
self.beta2 = 0.999
self.beta1t = 1
self.beta2t = 1
def sgd(self, error_f, gradient_f, fargs=[], n_epochs=100, learning_rate=0.001, save_wtrace=False,
verbose=True, error_convert_f=None):
'''
error_f: function that requires X and T as arguments (given in fargs) and returns mean squared error.
gradient_f: function that requires X and T as arguments (in fargs) and returns gradient of mean squared error
with respect to each weight.
error_convert_f: function that converts the standardized error from error_f to original T units.
'''
error_trace = []
weights_trace = []
if save_wtrace:
weights_trace = [self.all_weights.copy()]
epochs_per_print = n_epochs // 10
for epoch in range(n_epochs):
error = error_f(*fargs)
grad = gradient_f(*fargs)
# Update all weights using -= to modify their values in-place.
self.all_weights -= learning_rate * grad
if error_convert_f:
error = error_convert_f(error)
error_trace.append(error)
if save_wtrace:
weights_trace.append(self.all_weights.copy())
if verbose and ((epoch + 1) % max(1, epochs_per_print) == 0):
error_scalar = np.asscalar(error) if isinstance(error, np.ndarray) else error
print(f'sgd: Epoch {epoch+1:d} Error={error_scalar:.5f}')
return (error_trace, np.array(weights_trace)) if save_wtrace else error_trace
def adam(self, error_f, gradient_f, fargs=[], n_epochs=100, learning_rate=0.001,
save_wtrace=False, verbose=True, error_convert_f=None):
'''
error_f: function that requires X and T as arguments (given in fargs) and returns mean squared error.
gradient_f: function that requires X and T as arguments (in fargs) and returns gradient of mean squared error
with respect to each weight.
error_convert_f: function that converts the standardized error from error_f to original T units.
'''
alpha = learning_rate # learning rate called alpha in original paper on adam
epsilon = 1e-8
error_trace = []
weights_trace = []
if save_wtrace:
weights_trace = [self.all_weights.copy()]
epochs_per_print = n_epochs // 10
for epoch in range(n_epochs):
error = error_f(*fargs)
grad = gradient_f(*fargs)
self.mt[:] = self.beta1 * self.mt + (1 - self.beta1) * grad
self.vt[:] = self.beta2 * self.vt + (1 - self.beta2) * grad * grad
self.beta1t *= self.beta1
self.beta2t *= self.beta2
m_hat = self.mt / (1 - self.beta1t)
v_hat = self.vt / (1 - self.beta2t)
# Update all weights using -= to modify their values in-place.
self.all_weights -= alpha * m_hat / (np.sqrt(v_hat) + epsilon)
if error_convert_f:
error = error_convert_f(error)
error_trace.append(error)
if save_wtrace:
weights_trace.append(self.all_weights.copy())
if verbose and ((epoch + 1) % max(1, epochs_per_print) == 0):
error_scalar = np.asscalar(error) if isinstance(error, np.ndarray) else error
print(f'Adam: Epoch {epoch+1:d} Error={error_scalar:.5f}')
return (error_trace, np.array(weights_trace)) if save_wtrace else error_trace
def scg(self, error_f, gradient_f, fargs=[], n_epochs=100, learning_rate=None,
save_wtrace=False, error_convert_f=lambda x: x, verbose=True):
'''learning_rate not used in scg'''
if not self.scg_initialized:
shape = self.all_weights.shape
self.w_new = np.zeros(shape)
self.w_temp = np.zeros(shape)
self.g_new = np.zeros(shape)
self.g_old = np.zeros(shape)
self.g_smallstep = np.zeros(shape)
self.search_dir = np.zeros(shape)
self.scg_initialized = True
sigma0 = 1.0e-6
fold = error_f(*fargs)
error = fold
self.g_new[:] = gradient_f(*fargs)
# print('scg g\n', self.g_new)
self.g_old[:] = copy.deepcopy(self.g_new)
self.search_dir[:] = -self.g_new
success = True # Force calculation of directional derivs.
nsuccess = 0 # nsuccess counts number of successes.
beta = 1.0e-6 # Initial scale parameter. Lambda in Moeller.
betamin = 1.0e-15 # Lower bound on scale.
betamax = 1.0e20 # Upper bound on scale.
nvars = len(self.all_weights)
iteration = 1 # j counts number of iterations
error_trace = []
weights_trace = []
if save_wtrace:
weights_trace = [self.all_weights.copy()]
error_trace.append(error_convert_f(error))
thisIteration = 1
startTime = time.time()
startTimeLastVerbose = startTime
# Main optimization loop.
while thisIteration <= n_epochs:
# Calculate first and second directional derivatives.
if success:
mu = self.search_dir @ self.g_new
if mu >= 0:
self.search_dir[:] = - self.g_new
mu = self.search_dir.T @ self.g_new
kappa = self.search_dir.T @ self.search_dir
if math.isnan(kappa):
print('kappa', kappa)
if kappa < floatPrecision:
return (error_trace, np.array(weights_trace)) if save_wtrace else error_trace
sigma = sigma0 / math.sqrt(kappa)
self.w_temp[:] = self.all_weights
self.all_weights += sigma * self.search_dir
# error_f(*fargs) # forward pass through model for intermediate variable values for gradient
# gradient_f assumed to do forward pass to save hidden layer outputs
self.g_smallstep[:] = gradient_f(*fargs)
# print('scg smallstep g\n', self.g_smallstep)
self.all_weights[:] = self.w_temp
theta = self.search_dir @ (self.g_smallstep - self.g_new) / sigma
if math.isnan(theta):
print('theta', theta, 'sigma', sigma, 'search_dir[0]', self.search_dir[0],
'g_smallstep[0]', self.g_smallstep[0]) #, 'gradnew[0]', gradnew[0])
## Increase effective curvature and evaluate step size alpha.
delta = theta + beta * kappa
# if math.isnan(scalarv(delta)):
if math.isnan(delta):
print('delta is NaN', 'theta', theta, 'beta', beta, 'kappa', kappa)
elif delta <= 0:
delta = beta * kappa
beta = beta - theta / kappa
if delta == 0:
success = False
fnow = fold
else:
alpha = -mu / delta
## Calculate the comparison ratio Delta
self.w_temp[:] = self.all_weights
self.all_weights += alpha * self.search_dir
fnew = error_f(*fargs)
Delta = 2 * (fnew - fold) / (alpha * mu)
if not math.isnan(Delta) and Delta >= 0:
success = True
nsuccess += 1
fnow = fnew
else:
success = False
fnow = fold
self.all_weights[:] = self.w_temp
iterationsPerPrint = math.ceil(n_epochs/10)
# print('fnow', fnow, 'converted', error_convert_f(fnow))
error_trace.append(error_convert_f(fnow))
if verbose and ((thisIteration + 1) % max(1, iterationsPerPrint) == 0):
error = error_trace[-1]
error_scalar = np.asscalar(error) if isinstance(error, np.ndarray) else error
print(f'SCG: Epoch {thisIteration:d} Error={error_scalar:.5f}')
# if verbose and thisIteration % max(1, iterationsPerPrint) == 0:
# print('SCG: Iteration {:d} ObjectiveF={:.5f} Scale={:.3e} Seconds={:.3f}'.format(iteration,
# error_convert_f(fnow), beta, (time.time()-startTimeLastVerbose)))
startTimeLastVerbose = time.time()
if save_wtrace:
weights_trace.append(self.all_weights.copy())
if success:
fold = fnew
self.g_old[:] = self.g_new
self.g_new[:] = gradient_f(*fargs)
# print('scg gnew\n', self.g_new)
# If the gradient is zero then we are done.
gg = self.g_new @ self.g_new # dot(gradnew, gradnew)
if gg == 0:
return (error_trace, np.array(weights_trace)) if save_wtrace else error_trace
if math.isnan(Delta) or Delta < 0.25:
beta = min(4.0 * beta, betamax)
elif Delta > 0.75:
beta = max(0.5 * beta, betamin)
# Update search direction using Polak-Ribiere formula, or re-start
# in direction of negative gradient after nparams steps.
if nsuccess == nvars:
self.search_dir[:] = -self.g_new
nsuccess = 0
elif success:
gamma = (self.g_old - self.g_new) @ (self.g_new / mu)
#self.search_dir[:] = gamma * self.search_dir - self.g_new
self.search_dir *= gamma
self.search_dir -= self.g_new
thisIteration += 1
iteration += 1
# If we get here, then we haven't terminated in the given number of
# iterations.
return (error_trace, np.array(weights_trace)) if save_wtrace else error_trace
if __name__ == '__main__':
def parabola(wmin):
return ((w - wmin) ** 2)[0]
def parabola_gradient(wmin):
return 2 * (w - wmin)
wmin = 5
print()
w = np.array([0.0])
optimizer = Optimizers(w)
optimizer.sgd(parabola, parabola_gradient, [wmin],
n_epochs=50, learning_rate=0.1)
print(f'sgd: Minimum of parabola is at {wmin}. Value found is {w}')
print()
w = np.array([0.0])
optimizer = Optimizers(w)
optimizer.adam(parabola, parabola_gradient, [wmin],
n_epochs=50, learning_rate=0.1)
print(f'adam: Minimum of parabola is at {wmin}. Value found is {w}')
print()
w = np.array([0.0])
optimizer = Optimizers(w)
optimizer.scg(parabola, parabola_gradient, [wmin],
n_epochs=50, learning_rate=0.1)
print(f'scg: Minimum of parabola is at {wmin}. Value found is {w}')