-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.py
More file actions
283 lines (212 loc) · 9.46 KB
/
Copy pathAlgorithms.py
File metadata and controls
283 lines (212 loc) · 9.46 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[14]:
import numpy as np
import numpy.linalg as la
import time
# In[37]:
def constant_gradient(function, gradient, x_0, s, iterations):
"""
Implementation of gradient descent with constant stepsize
--------------
ARGUMENTS
function [function]: real-valued function dependent on one variable
gradient [function]: gradient of above function dependent on same variable
x_0 [np.array]: starting point: compatible with function and gradient
s [float]: stepsize
iterations [int]: maximum number of iterations
--------------
RETURNS
x_curr [np.array]: approximated solution of minimizing problem, last iterate
function_values [list]: sequence of function values at every iteration
gradient_norms [list]: sequence of gradient norms at every iteration
times [list]: elapsed time from start of calculation at every iteration excluding function evaluations
"""
function_values = [function(x_0)]
time_curr = time.perf_counter()
x_curr = x_0
gradient_curr = gradient(x_curr)
gradient_norms = [la.norm(gradient_curr)]
time_next = time.perf_counter()
times = [time_next - time_curr]
time_curr = time_next
for n in range(1, iterations + 1):
gradient_curr = gradient(x_curr)
x_curr = x_curr - s * gradient_curr
gradient_norms.append(la.norm(gradient_curr))
time_next = time.perf_counter()
times.append(times[-1] + time_next - time_curr)
#don't time function evaluation
function_values.append(function(x_curr))
time_curr = time.perf_counter()
return x_curr, function_values, gradient_norms, times
# In[36]:
def Nesterov_gradient(function, gradient, x_0, s, iterations):
"""
Implementation of Nesterov accelerated gradient descent
--------------
ARGUMENTS
function [function]: real-valued function dependent on one variable
gradient [function]: gradient of above function dependent on same variable
x_0 [np.array]: starting point: compatible with function and gradient
s [float]: stepsize
iterations [int]: maximum number of iterations
--------------
RETURNS
x_curr [np.array]: approximated solution of minimizing problem, last iterate
function_values [list]: sequence of function values at every iteration
gradient_norms [list]: sequence of gradient norms at every iteration
times [list]: elapsed time from start of calculation at every iteration excluding function evaluations
"""
function_values = [function(x_0)]
time_curr = time.perf_counter()
theta_curr = 1
y_curr = x_0
x_curr = x_0
gradient_curr = gradient(x_curr)
gradient_norms = [la.norm(gradient_curr)]
time_next = time.perf_counter()
times = [time_next - time_curr]
time_curr = time_next
for n in range(iterations):
theta_next = (1 + np.sqrt(1 + 4*theta_curr))/2
y_next = x_curr - s*gradient_curr
x_next = y_next + (theta_curr - 1)/theta_next * (y_next - y_curr)
theta_curr = theta_next
y_curr = y_next
x_curr = x_next
gradient_curr = gradient(x_next)
gradient_norms.append(la.norm(gradient_curr))
time_next = time.perf_counter()
times.append(times[-1] + time_next - time_curr)
#don't time calculation of objective function
function_values.append(function(y_next))
time_curr = time.perf_counter()
return y_curr, function_values, gradient_norms, times
# Eher nicht verwenden, da schlechter als AdaNAG_G und in anderem Paper gar nicht getestet
# In[21]:
def AdaNAG(function, gradient, x_0, s_0, iterations):
"""
Implementation of Adaptive Nesterov accelerated gradient descent
--------------
ARGUMENTS
function [function]: real-valued function dependent on one variable
gradient [function]: gradient of above function dependent on same variable
x_0 [np.array]: starting point: compatible with function and gradient
s_0 [float]: starting stepsize
iterations [int]: maximum number of iterations
--------------
RETURNS
iterates [list]: sequence of iterates produced by gradient method
"""
iterates = [x_0]
theta_0 = 1
theta_1 = 1/2 *(1+np.sqrt(1 + 4*theta_0**2))
theta_2 = 1/2 *(1+np.sqrt(1 + 4*theta_1**2))
theta_3 = 1/2 *(1+np.sqrt(1 + 4*theta_2**2))
theta_4 = 1/2 *(1+np.sqrt(1 + 4*theta_3**2))
theta_5 = 1/2 *(1+np.sqrt(1 + 4*theta_4**2))
alpha_1 = 1/2 *(1 - 1/theta_3)
alpha_2 = 1/2 *(1 - 1/theta_4)
alpha_3 = 1/2 *(1 - 1/theta_5)
alpha_0 = 2*theta_2/(theta_2 - 1) * 1/(1/alpha_3 + 1/alpha_2**2 - 1/alpha_1)
x_k = x_0
z_k = x_0
s_k = s_0
theta_k2 = theta_2
alpha_k = alpha_0
for k in range(iterations):
theta_k3 = 1/2 *(1+np.sqrt(1 + 4*theta_k2**2))
alpha_k1 = 1/2 *(1 - 1/theta_k2)
y_k1 = x_k - s_k * gradient(x_k)
z_k1 = z_k - s_k * alpha_k * theta_k2 * gradient(x_k)
x_k1 = (1 - 1/theta_k3)*y_k1 + 1/theta_k3 * z_k1
if k == 0:
L_1 = -1/2 * la.norm(gradient(x_k1) - gradient(x_k))**2 / (function(x_k1) - function(x_k) + np.dot(gradient(x_k1), x_k - x_k1))
s_k = np.min([alpha_0/alpha_1 *theta_2/(theta_3*(theta_3 - 1)) * s_0, alpha_2**2*alpha_3/((alpha_3 + alpha_2**2)*alpha_1) *1/L_1])
else:
s_k = stepsize(function, gradient, x_k, x_k1, s_k, alpha_k, alpha_k1)
x_k = x_k1
z_k = z_k1
alpha_k = alpha_k1
theta_k2 = theta_k3
iterates.append(y_k1)
return iterates
# In[22]:
def stepsize(function, gradient, x_k, x_k1, s_k, alpha_k, alpha_k1):
"""
"""
L_k1 = -1/2 * la.norm(gradient(x_k1) - gradient(x_k))**2 / (function(x_k1) - function(x_k) + np.inner(gradient(x_k1), x_k - x_k1))
return np.min([alpha_k/alpha_k1 * s_k, alpha_k**2/(alpha_k1 + alpha_k**2) * 1/L_k1])
# In[35]:
def AdaNAG_G(function, gradient, x_0, s_0, iterations, tau, alpha, B_0):
"""
Implementation of generalized adaptive Nesterov accelerated gradient method
--------------
ARGUMENTS
function [function]: real-valued function dependent on one variable
gradient [function]: gradient of above function dependent on same variable
x_0 [np.array]: starting point: compatible with function and gradient
s_0 [float]: starting stepsize
iterations [int]: maximum number of iterations
tau [function]: definition of sequence tau_k dependent on natural number k
alpha [function]: definition of sequence alpha_k dependent on natural number k
B_0 [float]: starting value for sequence B_k
--------------
RETURNS
x_curr [np.array]: approximated solution of minimizing problem, last iterate
function_values [list]: sequence of function values at every iteration
gradient_norms [list]: sequence of gradient norms at every iteration
times [list]: elapsed time from start of calculation at every iteration
steps [list]: sequence of stepsizes at every iteration
"""
time_0 = time.perf_counter()
x_curr = x_0
z_curr = x_0
s_curr = s_0
function_curr = function(x_curr)
gradient_curr = gradient(x_curr)
function_values = [function_curr]
gradient_norms = [la.norm(gradient_curr)]
steps = [s_curr]
tau_curr = tau(0)
tau_next = tau(1)
tau_next2 = tau(2)
alpha_curr = alpha(0)
alpha_next = alpha(1)
A_prev = 0
A_curr = alpha_next * tau_next * (tau_next - 1)
B_curr = B_0
B_next = alpha_next**2 * tau_next**2 * ((tau_next -1)**2/(alpha_curr * tau_curr**2) - 1)
times = [time.perf_counter() - time_0]
for k in range(iterations):
y_next = x_curr - s_curr*gradient_curr
z_next = z_curr - s_curr*alpha_curr*tau_curr*gradient_curr
x_next = (1 - 1/tau_next) * y_next + 1/tau_next * z_next
function_next = function(x_next)
gradient_next = gradient(x_next)
denominator = (function_next - function_curr) + np.dot(gradient_next, x_curr - x_next)
#mathematically denominator < 0, however at small scales rounding errors can cause it to become positive
#Then fallback on negative denominator from iteration before
if denominator <= 0:
L_next = -1/2 * la.norm(gradient_next - gradient_curr)**2/denominator
s_curr = np.min([(A_prev + alpha_curr * tau_curr)/A_curr * s_curr, 1/(A_curr/B_curr + (B_next + alpha_next**2*tau_next**2)/A_curr) * 1/L_next])
x_curr = x_next
z_curr = z_next
function_curr = function_next
gradient_curr = gradient_next
tau_curr = tau_next
tau_next = tau_next2
tau_next2 = tau(k + 3)
alpha_curr = alpha_next
alpha_next = alpha(k + 2)
A_prev = A_curr
A_curr = alpha_next * tau_next * (tau_next - 1)
B_curr = B_next
B_next = alpha_next**2 * tau_next**2 * ((tau_next -1)**2/(alpha_curr * tau_curr**2) - 1)
function_values.append(function_next)
gradient_norms.append(la.norm(gradient_next))
steps.append(s_curr)
times.append(time.perf_counter() - time_0)
return x_curr, function_values, gradient_norms, times, steps
# In[ ]: