-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathalgorithms.py
220 lines (190 loc) · 5.96 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
import numpy as np
import scipy.linalg as LA
import scipy.sparse as spr
import scipy.sparse.linalg as spr_LA
from time import perf_counter
def safe_division(x, y):
"""
Computes safe division x/y for small positive values x and y
"""
return np.exp(np.log(x) - np.log(y)) if y != 0 else 1e16
def ad_grad(J, df, x0, la_0=1e-6, numb_iter=100):
"""
Minimize f(x) by adaptive gradient method.
Takes J as some evaluation function for comparison.
"""
begin = perf_counter()
x_old = x0
grad_old = df(x0)
x = x0 - la_0 * grad_old
la_old = 1
th = 1e9
steps_array = []
values = [J(grad_old)]
for i in range(numb_iter):
grad = df(x)
norm_x = LA.norm(x - x_old)
norm_grad = LA.norm(grad - grad_old)
#la = min(np.sqrt(1 + th) * la_old, 0.5 * norm_x / norm_grad)
la = min(np.sqrt(1 + th) * la_old, 0.5 * safe_division(norm_x, norm_grad))
th = la / la_old
x_old = x.copy()
x -= la * grad
la_old = la
grad_old = grad
values.append(J(grad))
steps_array.append(la)
end = perf_counter()
print("Time execution of adaptive gradient descent:", end - begin)
return values, x, steps_array
def ad_grad_tighter(J, df, x0, la_0=1e-6, numb_iter=100):
"""
Minimize f(x) by adaptive gradient method with tighter estimate.
Takes J as some evaluation function for comparison.
"""
begin = perf_counter()
values, x_old, steps_array = ad_grad(J, df, x0, la_0, 10)
th = 1
la_old = steps_array[-1]
grad_old = df(x_old)
x = x_old - la_old * grad_old
for i in range(numb_iter):
grad = df(x)
norm_x = LA.norm(x - x_old)
norm_grad = LA.norm(grad - grad_old)
denom = 3 * LA.norm(grad)**2 - 4 * np.vdot(grad, grad_old)
if denom > 0:
la = min(np.sqrt(1 + th) * la_old, safe_division(norm_x, np.sqrt(denom)))
else:
la = np.sqrt(1+th) * la_old
th = la / la_old
x_old = x.copy()
x -= la * grad
la_old = la
grad_old = grad
values.append(J(grad))
steps_array.append(la)
end = perf_counter()
print("Time execution of adaptive gradient descent:", end - begin)
return np.array(values), x, steps_array
def ad_grad_smooth(J, df, x0, L, numb_iter=100):
"""
Minimize f(x) by adaptive gradient method knowing L.
Takes J as some evaluation function for comparison.
"""
begin = perf_counter()
x_old = x0
grad_old = df(x0)
la_old = 1./L
th = 1e9
x = x0 - la_old * grad_old
values = [J(grad_old)]
steps_array = []
for i in range(numb_iter):
grad = df(x)
norm_x = LA.norm(x - x_old)
norm_grad = LA.norm(grad - grad_old)
la = min(
np.sqrt(1 + th) * la_old, 1 / (la_old * L**2) + 0.5 * safe_division(norm_x, norm_grad))
th = la / la_old
x_old = x.copy()
x -= la * grad
la_old = la
grad_old = grad
values.append(J(grad))
steps_array.append(la)
end = perf_counter()
print("Time execution of adaptive gradient descent (L is known):", end - begin)
return np.array(values), x, steps_array
def ad_grad_accel(J, df, x0, la_0=1e-6, numb_iter=100):
"""
Minimize f(x) by heuristic accelerated adaptive gradient method.
Takes J as some evaluation function for comparison.
"""
begin = perf_counter()
x_old = x0.copy()
y_old = x_old
grad_old = df(x0)
x = x0 - la_0 * grad_old
y = x
la_old = 1
Lambda_old = 1
th = 1e9
Th = 1e9
values = [J(grad_old)]
steps_array = []
for i in range(numb_iter):
grad = df(y)
norm_x = LA.norm(y - y_old)
norm_grad = LA.norm(grad - grad_old)
la = min(np.sqrt(1 + th) * la_old, 0.5 * safe_division(norm_x, norm_grad))
Lambda = min(
np.sqrt(1 + Th) * Lambda_old, 0.5 / safe_division(norm_x, norm_grad))
th = la / la_old
Th = Lambda / Lambda_old
t = np.sqrt(Lambda * la)
beta = (1 - t) / (1 + t)
x = y - la * grad
y_old = y
y = x + beta * (x - x_old)
x_old = x.copy()
la_old = la
Lambda_old = Lambda
grad_old = grad
values.append(J(grad))
steps_array.append(la)
end = perf_counter()
print("Time execution of accelerated adaptive gradient descent:", end - begin)
return np.array(values), x, steps_array
# GD #
def gd(J, df, x0, la=1, numb_iter=100):
"""
Gradient descent for minimizing smooth f.
"""
begin = perf_counter()
x = x0.copy()
values = [J(df(x0))]
for i in range(numb_iter):
grad = df(x)
x -= la * grad
values.append(J(grad))
end = perf_counter()
print("Time execution for GD:", end - begin)
return np.array(values), x
# accelerated GD
def accel_gd(J, df, x0, la, numb_iter=100):
"""
Accelerated (Nesterov) gradient descent for minimizing smooth f.
"""
begin = perf_counter()
x, y = x0.copy(), x0.copy()
t = 1.
values = [J(df(x0))]
for i in range(numb_iter):
grad = df(y)
x1 = y - la * grad
t1 = 0.5 * (1 + np.sqrt(1 + 4 * t**2))
y = x1 + (t - 1) / t1 * (x1 - x)
values.append(J(grad))
x, t = x1, t1
end = perf_counter()
print("Time execution for accelerated GD:", end - begin)
return np.array(values), x
def accel_str_gd(J, df, x0, la, mu, numb_iter=100):
"""
Accelerated (Nesterov) gradient descent for minimizing smooth strongly convex f.
"""
begin = perf_counter()
x, y = x0.copy(), x0.copy()
kappa = np.sqrt((1/la) / mu)
beta = (kappa - 1) / (kappa + 1)
values = [J(df(x0))]
for i in range(numb_iter):
grad = df(x)
y1 = x - la * grad
x = y1 + beta * (y1 - y)
values.append(J(grad))
y = y1
end = perf_counter()
print("Time execution for accelerated GD:", end - begin)
return np.array(values), x