-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithms.py
685 lines (536 loc) · 16.4 KB
/
algorithms.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import math
import time
import itertools
import autograd.numpy as np
from utils import uniform_angles_pss
def STP(f, x, a_init, step_upd='half', distribution='Uniform', T=10000):
# Initialization
y = x
a = a_init
# Function values
f_values = []
f_values.append((0 ,f.eval(y)))
# Gradient norm
g_norm = []
g_norm.append((0, np.linalg.norm(f.gradient(y))))
# Execution time per iteration
timer = []
for t in range(1, T):
# Start timer
s_time = time.time()
if distribution == 'Uniform':
s = np.random.multivariate_normal(np.zeros(f.d), np.identity(f.d))
s = s/np.linalg.norm(s)
elif distribution == 'Normal':
s = np.random.multivariate_normal(np.zeros(f.d), np.identity(f.d))
else:
raise ValueError('The option %s is not a supported sampling distribution.' %(distribution))
# List possible next iterates
V = [y+a*s, y-a*s, y]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update step
y = V[i_star]
# Step size update
if step_upd == 'half':
if t%10 == 0:
a = a/2
elif step_upd == 'inv':
a = a_init/(t+1)
elif step_upd == 'inv_sqrt':
a = a_init/np.sqrt(t+1)
else:
raise ValueError('The option %s is not a supported step size update rule.' %(step_upd))
# Stop timer
e_time = time.time()
timer.append(e_time - s_time)
f_values.append((t, f.eval(y)))
g_norm.append((t, np.linalg.norm(f.gradient(y))))
# Summary
summary = {}
summary['x_T'] = y
summary['fval'] = np.array(f_values)
summary['gnorm'] = np.array(g_norm)
summary['time'] = np.mean(timer)
return summary
def BDS(f, x, a_init, a_max, theta, gamma, rho, T=10000):
# Initialization
y = x # iterate @ t
a = a_init # step size
# Function values
f_values = []
f_values.append((0, f.eval(y)))
# Gradient norm
g_norm = []
g_norm.append((0, np.linalg.norm(f.gradient(y))))
# Execution time per iteration
timer = []
for t in range(1,T):
# Start timer
s_time = time.time()
# Reset variables
successful = False
d_opt = np.zeros(f.d)
f_y = f.eval(y) # function value at current iterate
# Generate a polling set
D, D_symmetric, D_size = uniform_angles_pss(f.d)
# Search the polling set
for i in np.random.permutation(D_size):
d = D[:,i]
if f.eval(y + a*d) < f_y - rho(a):
# Iteration succesful
d_opt = d
successful = True
# Stop searching PSS
break
# Update step
if successful:
y = y + a*d_opt
a = np.minimum(gamma*a, a_max)
else:
a = theta*a
# Stop timer
e_time = time.time()
timer.append(e_time - s_time)
f_values.append((t, f.eval(y)))
g_norm.append((t, np.linalg.norm(f.gradient(y))))
# Summary
summary = {}
summary['x_T'] = y
summary['fval'] = np.array(f_values)
summary['gnorm'] = np.array(g_norm)
summary['time'] = np.mean(timer)
return summary
def AHDS(f, x, a_init, a_max, theta, gamma, rho, T=10000):
# Initialization
y = x # iterate @ t
a = a_init # step size
# Function values
f_values = []
f_values.append((0, f.eval(y)))
# Gradient norm
g_norm = []
g_norm.append((0, np.linalg.norm(f.gradient(y))))
# Execution time per iteration
timer = []
# Flag to indicate if the algorithms get's stucked
stacked = False
for t in range(1,T):
# Start timer
s_time = time.time()
# Reset variables
successful = False
H = np.zeros((f.d, f.d)) # Hessian
f_y = f.eval(y) # function value at current iterate
d_opt = np.zeros(f.d) # descent direction
B_opt = np.zeros((f.d, f.d)) # independent set of vectors
D_table = np.zeros(f.d) # store function values for Hessian computation
B_table = np.zeros((f.d, f.d)) # store function values for Hessian computation
""" ========= Step 1 ========= """
# Generate a PSS D
D, D_symmetric, D_size = uniform_angles_pss(f.d)
# Search the PSS
for i in np.random.permutation(D_size):
d = D[:,i]
if f.eval(y + a*d) < f_y - rho(a):
# Iteration succesful
d_opt = d
successful = True
# Stop searching PSS
break
""" ========= Step 2 ========= """
# Search opposite directions in PSS
if not D_symmetric and not successful:
for i in np.random.permutation(D_size):
d = -D[:,i]
if f.eval(y + a*d) < f_y - rho(a):
# Iteration succesful
d_opt = d
successful = True
# Stop searching PSS
break
""" ========= Step 3 ========= """
if not successful:
# Choose B as a subset of D with f.d linearly independent vectors
subsets = itertools.combinations(range(D_size), f.d)
for subset in np.random.permutation(list(subsets)):
B = D[:,subset]
if np.linalg.matrix_rank(B) == f.d:
B_opt = B
# Stop search
break
break_outer = False
for i in range(f.d-1):
for j in range(i+1,f.d):
d = B_opt[:,i] + B_opt[:,j]
B_table[i,j] = f.eval(y + a*d)
if B_table[i,j] < f_y - rho(a):
# Iteration successful
d_opt = d
successful = True
# Stop searching
break_outer = True
break
if break_outer:
# Stop searching
break
""" ========= Step 4 ========= """
# if not successful and not stacked:
if not successful:
# Hessian approximation: Diagonal elements
for i in range(f.d):
di = B_opt[:,i]
D_table[i] = f.eval(y+a*di)
H[i,i] = D_table[i] - 2*f_y + f.eval(y-a*di)
# Hessian approximation: Off-diagonal elements
for i in range(f.d-1):
for j in range(i+1,f.d):
H[i,j] = B_table[i,j] - D_table[i] - D_table[j] + f_y
H[j,i] = H[i,j]
# Complete computation
H = H/(a**2)
# When iterates get very close to a minimizer the Hessian approximation
# may result to NaN values. The try statement avoids such errors.
try:
# Eigendecomposition
L, V = np.linalg.eig(H)
# Eigenvector corresponding to minimum eigenvalue
idx = np.argmin(L)
d = V[:,idx]
# Check d
if f.eval(y + a*d) < f.eval(y) - rho(a):
# Iteration successful
d_opt = d
successful = True
# Check -d
if f.eval(y - a*d) < f.eval(y) - rho(a) and f.eval(y - a*d) < f.eval(y + a*d):
# Iteration successful
d_opt = -d
successful = True
except:
pass
""" ========= Step 5 ========= """
# Update step
if successful:
y = y + a*d_opt
a = np.minimum(gamma*a, a_max)
stacked = False
else:
a = theta*a
stacked = True
# Stop timer
e_time = time.time()
timer.append(e_time - s_time)
f_values.append((t, f.eval(y)))
g_norm.append((t, np.linalg.norm(f.gradient(y))))
# Summary
summary = {}
summary['x_T'] = y
summary['fval'] = np.array(f_values)
summary['gnorm'] = np.array(g_norm)
summary['time'] = np.mean(timer)
return summary
def RS(f, x, a_init, sigma_1, sigma_2, distribution='Normal', step_upd='half', theta=0.6, T_half=10, T=10000):
# Initialization
y = x # iterate @ t
a = a_init # step size
# Function values
f_values = []
f_values.append((0, f.eval(y)))
# Gradient norm
g_norm = []
g_norm.append((0, np.linalg.norm(f.gradient(y))))
# Execution time per iteration
timer = []
for t in range(1,T):
# Start timer
s_time = time.time()
""" ========= Random Step 1 ========= """
if distribution == 'Uniform':
d1 = np.random.multivariate_normal(np.zeros(f.d), np.identity(f.d))
d1 = sigma_1*(d1/np.linalg.norm(d1))
elif distribution == 'Normal':
d1 = np.random.multivariate_normal(np.zeros(f.d), np.power(sigma_1,2.0)*np.identity(f.d))
else:
raise ValueError('The option %s is not a supported sampling distribution.' %(distribution))
V = [y, y+a*d1, y-a*d1]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update iterate
y = V[i_star]
""" ========= Random Step 2 ========= """
if i_star == 0:
if distribution == 'Uniform':
d2 = np.random.multivariate_normal(np.zeros(f.d), np.identity(f.d))
d2 = sigma_2*(d2/np.linalg.norm(d2))
elif distribution == 'Normal':
d2 = np.random.multivariate_normal(np.zeros(f.d), np.power(sigma_2,2.0)*np.identity(f.d))
else:
raise ValueError('The option %s is not a supported sampling distribution.' %(distribution))
V = [y, y+a*d2, y-a*d2]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update iterate
y = V[i_star]
# Update step-size
if step_upd == 'half':
if t%T_half == 0:
a = theta*a
elif step_upd == 'inv':
a = a_init/(t+1)
elif step_upd == 'inv_sqrt':
a = a_init/np.sqrt(t+1)
else:
raise ValueError('The option %s is not a supported step size update rule.' %(step_upd))
# Stop timer
e_time = time.time()
timer.append(e_time - s_time)
f_values.append((t, f.eval(y)))
g_norm.append((t, np.linalg.norm(f.gradient(y))))
# Summary
summary = {}
summary['x_T'] = y
summary['fval'] = np.array(f_values)
summary['gnorm'] = np.array(g_norm)
summary['time'] = np.mean(timer)
return summary
def DFPI_SPSA(f, y, c_init, beta, T_power):
# Power iteration - Compute eigenvector for max eigenvalue
r = 0.001
T_power_approx = 5
d2 = np.random.rand(f.d)
c = c_init
for i in range(T_power_approx):
Delta = np.random.binomial(n=1, p=0.5, size=f.d)
Delta[Delta == 0] = -1
# Approximate gradient vectors
d_rplus = f.eval(y + r*d2 + c*Delta) - f.eval(y + r*d2 - c*Delta)
G_rplus = np.divide(d_rplus, 2*c*Delta)
d_rminus = f.eval(y - r*d2 + c*Delta) - f.eval(y - r*d2 - c*Delta)
G_rminus = np.divide(d_rminus, 2*c*Delta)
# Approximate Hessian-vector product
Hd = (G_rplus - G_rminus)/(2*r)
# Power iteration - update
d2 = Hd/np.linalg.norm(Hd)
# Approximate gradient vectors
d_rplus = f.eval(y + r*d2 + c*Delta) - f.eval(y + r*d2 - c*Delta)
G_rplus = np.divide(d_rplus, 2*c*Delta)
d_rminus = f.eval(y - r*d2 + c*Delta) - f.eval(y - r*d2 - c*Delta)
G_rminus = np.divide(d_rminus, 2*c*Delta)
# Approximate Hessian-vector product
Hd = (G_rplus - G_rminus)/(2*r)
# Largest eigenvalue
lmax = np.linalg.norm(Hd)/np.linalg.norm(d2)
# Power iteration - Compute eigenvector for min eigenvalue
b_power = 1/lmax
d2 = np.random.rand(f.d)
for i in range(T_power):
Delta = np.random.binomial(n=1, p=0.5, size=f.d)
Delta[Delta == 0] = -1
# Approximate gradient vectors
d_rplus = f.eval(y + r*d2 + c*Delta) - f.eval(y + r*d2 - c*Delta)
G_rplus = np.divide(d_rplus, 2*c*Delta)
d_rminus = f.eval(y - r*d2 + c*Delta) - f.eval(y - r*d2 - c*Delta)
G_rminus = np.divide(d_rminus, 2*c*Delta)
# Approximate Hessian-vector product
Hd = (G_rplus - G_rminus)/(2*r)
# Power iteration - update
d2_ = d2 - b_power*Hd
d2 = d2_/np.linalg.norm(d2_)
# Negative curvature
return d2
def DFPI_FD(f, y, c, T_power):
r = 0.01
# Power iteration - Compute eigenvector for max eigenvalue
T_power_approx = 15
d2 = np.random.rand(f.d)
# Basis vectors
I = np.identity(f.d)
for i in range(T_power_approx):
# Initialize
g_p = np.empty(f.d)
g_m = np.empty(f.d)
# Approximate gradient vectors
for j in range(f.d):
g_p[j] = (f.eval(y + r*d2 + c*I[:,j]) - f.eval(y + r*d2 - c*I[:,j]))/(2*c)
g_m[j] = (f.eval(y - r*d2 + c*I[:,j]) - f.eval(y - r*d2 - c*I[:,j]))/(2*c)
# Approximate Hessian-vector product
Hd = (g_p - g_m)/(2*r)
# Power iteration - update
d2 = Hd/np.linalg.norm(Hd)
# Approximate gradient vectors
g_p = np.empty(f.d)
g_m = np.empty(f.d)
for j in range(f.d):
g_p[j] = (f.eval(y + r*d2 + c*I[:,j]) - f.eval(y + r*d2 - c*I[:,j]))/(2*c)
g_m[j] = (f.eval(y - r*d2 + c*I[:,j]) - f.eval(y - r*d2 - c*I[:,j]))/(2*c)
# Approximate Hessian-vector product
Hd = (g_p - g_m)/(2*r)
# Largest eigenvalue
lmax = np.linalg.norm(Hd)/np.linalg.norm(d2)
# Power iteration - Compute eigenvector for min eigenvalue
b_power = 1/lmax
d2 = np.random.rand(f.d)
for i in range(T_power):
# Initialize
g_p = np.empty(f.d)
g_m = np.empty(f.d)
# Approximate gradient vectors
for j in range(f.d):
g_p[j] = (f.eval(y + r*d2 + c*I[:,j]) - f.eval(y + r*d2 - c*I[:,j]))/(2*c)
g_m[j] = (f.eval(y - r*d2 + c*I[:,j]) - f.eval(y - r*d2 - c*I[:,j]))/(2*c)
# Approximate Hessian-vector product
Hd = (g_p - g_m)/(2*r)
# Power iteration - update
d2_ = d2 - b_power*Hd
d2 = d2_/np.linalg.norm(d2_)
return d2
def RSPI_SPSA(f, x, a_init, c_init, beta, sigma_1, sigma_2, distribution='Normal', step_upd='half', theta=0.6, T_half=10, T_power=100, T=10000):
# Initialization
y = x # iterate @ t
a = a_init # step size
c = c_init # SPSA step
# Function values
f_values = []
f_values.append((0, f.eval(y)))
# Gradient norm
g_norm = []
g_norm.append((0, np.linalg.norm(f.gradient(y))))
# Execution time per iteration
timer = []
for t in range(1,T):
# Start timer
s_time = time.time()
""" ========= Random Step ========= """
if distribution == 'Uniform':
d1 = np.random.multivariate_normal(np.zeros(f.d), np.identity(f.d))
d1 = sigma_1*(d1/np.linalg.norm(d1))
elif distribution == 'Normal':
d1 = np.random.multivariate_normal(np.zeros(f.d), np.power(sigma_1,2.0)*np.identity(f.d))
else:
raise ValueError('The option %s is not a supported sampling distribution.' %(distribution))
V = [y, y+a*d1, y-a*d1]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update iterate
y = V[i_star]
""" ========= Negative Curvature ========= """
if i_star == 0:
d2 = DFPI_SPSA(f, y, c, beta, T_power)
while d2 is None:
d2 = DFPI_SPSA(f, y, c, beta, T_power)
""" ========= Update Step ========= """
V = [y, y+sigma_2*d2, y-sigma_2*d2]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update iterate
y = V[i_star]
# Decrease SPSA parameter
c = c_init/pow(t,beta)
# Update step-size
if step_upd == 'half':
if t%T_half == 0:
a = theta*a
elif step_upd == 'inv':
a = a_init/(t+1)
elif step_upd == 'inv_sqrt':
a = a_init/np.sqrt(t+1)
else:
raise ValueError('The option %s is not a supported step size update rule.' %(step_upd))
# Stop timer
e_time = time.time()
timer.append(e_time - s_time)
f_values.append((t, f.eval(y)))
g_norm.append((t, np.linalg.norm(f.gradient(y))))
# Summary
summary = {}
summary['x_T'] = y
summary['fval'] = np.array(f_values)
summary['gnorm'] = np.array(g_norm)
summary['time'] = np.mean(timer)
return summary
def RSPI_FD(f, x, a_init, c_init, beta, sigma_1, sigma_2, distribution='Normal', step_upd='half', theta=0.6, T_half=10, T_power=100, T=10000):
# Initialization
y = x # iterate @ t
a = a_init # step size
c = c_init # SPSA step
# Function values
f_values = []
f_values.append((0, f.eval(y)))
# Gradient norm
g_norm = []
g_norm.append((0, np.linalg.norm(f.gradient(y))))
# Execution time per iteration
timer = []
for t in range(1,T):
# Start timer
s_time = time.time()
""" ========= Random Step ========= """
if distribution == 'Uniform':
d1 = np.random.multivariate_normal(np.zeros(f.d), np.identity(f.d))
d1 = sigma_1*(d1/np.linalg.norm(d1))
elif distribution == 'Normal':
d1 = np.random.multivariate_normal(np.zeros(f.d), np.power(sigma_1,2.0)*np.identity(f.d))
else:
raise ValueError('The option %s is not a supported sampling distribution.' %(distribution))
V = [y, y+a*d1, y-a*d1]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update iterate
y = V[i_star]
""" ========= Negative Curvature ========= """
if i_star == 0:
d2 = DFPI_FD(f, y, c, T_power)
while d2 is None:
d2 = DFPI_FD(f, y, c, T_power)
""" ========= Update Step ========= """
V = [y, y+sigma_2*d2, y-sigma_2*d2]
f_v = []
for v in V:
f_v.append(f.eval(v))
# Select optimal point
i_star = np.argmin(np.array(f_v))
# Update iterate
y = V[i_star]
# Decrease SPSA parameter
c = c_init/pow(t,beta)
# Update step-size
if step_upd == 'half':
if t%T_half == 0:
a = theta*a
elif step_upd == 'inv':
a = a_init/(t+1)
elif step_upd == 'inv_sqrt':
a = a_init/np.sqrt(t+1)
else:
raise ValueError('The option %s is not a supported step size update rule.' %(step_upd))
# Stop timer
e_time = time.time()
timer.append(e_time - s_time)
f_values.append((t, f.eval(y)))
g_norm.append((t, np.linalg.norm(f.gradient(y))))
# Summary
summary = {}
summary['x_T'] = y
summary['fval'] = np.array(f_values)
summary['gnorm'] = np.array(g_norm)
summary['time'] = np.mean(timer)
return summary