-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptalg.py
461 lines (383 loc) · 13.3 KB
/
optalg.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
import time
import numpy as np
import sigpy as sp
from tqdm.auto import tqdm
import optpoly
def cg(
num_iters,
ptol,
A,
b,
P=None,
lamda=None,
ref=None,
save=None,
verbose=True,
idx=None,
):
r"""Conjugate Gradient.
Solves for the following optimization problem.
.. math::
\min_x \frac{1}{2} \| A x - b \|_2^2
Inputs:
num_iters (Int): Maximum number of iterations.
ptol (Float): l1-percentage tolerance between iterates.
A (Linop): Forward model.
b (Array): Measurement.
P (None or Linop): Preconditioner.
ref (None or Array): Reference to compare against.
lamda (None or Float): l2-regularization.
save (None or String): If specified, path to save iterations and
timings.
verbose (Bool): Print information.
idx (Tuple): If passed, slice iterates before saving.
Returns:
x (Array): Reconstruction.
"""
device = sp.get_device(b)
xp = device.xp
with device:
lst_time = []
lst_err = None
if type(ref) != type(None):
ref = sp.to_device(ref, device)
lst_err = ([], [])
AHA = A.N
if lamda is not None:
AHA = AHA + lamda * sp.linop.Identitiy(A.ishape)
if P == None:
P = sp.linop.Identity(A.ishape)
# Set-up time.
start_time = time.perf_counter()
AHb = A.H(b)
x = xp.zeros_like(AHb)
r = AHb - AHA(x)
z = P(r.copy())
p = z.copy()
rzold = xp.real(xp.vdot(r, z))
end_time = time.perf_counter()
lst_time.append(end_time - start_time)
if lst_err != None:
lst_err[0].append(calc_perc_err(ref, x, ord=1))
lst_err[1].append(calc_perc_err(ref, x, ord=2))
save_helper(save, x, 0, lst_time, lst_err, idx)
if verbose:
pbar = tqdm(total=num_iters, desc="CG", leave=True)
for k in range(0, num_iters):
x_old = x.copy()
start_time = time.perf_counter()
Ap = AHA(p)
pAp = xp.real(xp.vdot(p, Ap)).item()
if pAp > 0:
alpha = rzold / pAp
sp.axpy(x, alpha, p)
sp.axpy(r, -alpha, Ap)
z = P(r.copy())
rznew = xp.real(xp.vdot(r, z))
beta = rznew / rzold
sp.xpay(p, beta, z)
rzold = rznew
end_time = time.perf_counter()
lst_time.append(end_time - start_time)
if lst_err != None:
lst_err[0].append(calc_perc_err(ref, x, ord=1))
lst_err[1].append(calc_perc_err(ref, x, ord=2))
save_helper(save, x, k + 1, lst_time, lst_err, idx)
calc_tol = calc_perc_err(x_old, x, ord=1)
if verbose:
pbar.set_postfix(ptol="%0.2f%%" % calc_tol)
pbar.update()
pbar.refresh()
if pAp <= 0 or calc_tol <= ptol:
break
if verbose:
pbar.close()
return x
def pgd(
num_iters,
ptol,
A,
b,
proxg,
x0=None,
precond_type=None,
pdeg=None,
accelerate=True,
l=0,
a=2.1,
stepfn=None,
ref=None,
save=None,
verbose=True,
idx=None,
):
r"""Proximal Gradient Descent.
Solves for the following optimization problem using proximal gradient
descent:
.. math::
\min_x \frac{1}{2} \| A x - b \|_2^2 + g(x)
Assumes MaxEig(A.H * A) = 1.
Inputs:
num_iters (Int): Maximum number of iterations.
ptol (Float): l1-percentage tolerance between iterates.
A (Linop): Forward model.
b (Array): Measurement.
proxg (Prox): Proximal operator of g.
x0 (Array): Initial guess.
precond_type (String): Type of preconditioner.
- "l_2" = l_2 optimized polynomial.
- "l_inf" = l_inf optimized polynomial.
- "ifista" = from DOI: 10.1137/140970537.
pdeg (None or Int): Degree of polynomial preconditioner to use.
If None, do not use preconditioner.
accelerate (Bool): If True, use Nestrov acceleration.
l (Float): If known, minimum eigenvalue of A.H * A.
a (Float): See DOI: 10.1561/2400000003.
stepfn (function): If specified, this function determines the variable
step size per iteration.
ref (None or Array): Reference to compare against.
save (None or String): If specified, path to save iterations and
timings.
verbose (Bool): Print information.
idx (Tuple): If passed, slice iterates before saving.
Returns:
x (Array): Reconstruction.
"""
if precond_type == "l_inf" and l == 0:
raise Exception("If l == 0, l_inf polynomial cannot be used.")
device = sp.get_device(b)
xp = device.xp
if verbose:
print("Proximal Gradient Descent.")
if accelerate:
print(
"> Variable step size."
if l == 0
else "> Fixed step size derived from l = %5.2f" % l
)
if precond_type == None:
print("> No preconditioning used.")
else:
print("> %s-preconditioning is used." % precond_type)
P = (
sp.linop.Identity(A.ishape)
if pdeg is None
else optpoly.create_polynomial_preconditioner(
precond_type, pdeg, A.N, l, 1, verbose=verbose
)
)
with device:
lst_time = []
lst_err = None
if type(ref) != type(None):
ref = sp.to_device(ref, device)
lst_err = ([], [])
# Set-up time.
start_time = time.perf_counter()
AHb = A.H(b)
x = AHb if x0 is None else sp.to_device(x0, device)
z = x.copy()
end_time = time.perf_counter()
lst_time.append(end_time - start_time)
if lst_err != None:
lst_err[0].append(calc_perc_err(ref, x, ord=1))
lst_err[1].append(calc_perc_err(ref, x, ord=2))
save_helper(save, x, 0, lst_time, lst_err, idx)
if verbose:
pbar = tqdm(total=num_iters, desc="PGD", leave=True)
for k in range(0, num_iters):
start_time = time.perf_counter()
x_old = x.copy()
if accelerate:
x = z.copy()
gr = A.N(x) - AHb
x = proxg(1, x - P(gr))
if accelerate:
if l > 0:
# DOI: 10.1007/978-3-319-91578-4_2
step = (1 - l ** (0.5)) / (1 + l ** (0.5))
elif stepfn == None:
# DOI: 10.1561/2400000003, after taking 0-index into account.
step = k / (k + a + 1)
else:
step = stepfn(k)
z = x + step * (x - x_old)
end_time = time.perf_counter()
lst_time.append(end_time - start_time)
if lst_err != None:
lst_err[0].append(calc_perc_err(ref, x, ord=1))
lst_err[1].append(calc_perc_err(ref, x, ord=2))
save_helper(save, x, k + 1, lst_time, lst_err, idx)
calc_tol = calc_perc_err(x_old, x, ord=1)
if verbose:
pbar.set_postfix(ptol="%0.2f%%" % calc_tol)
pbar.update()
pbar.refresh()
if calc_tol <= ptol:
break
if verbose:
pbar.close()
return x
def admm(
num_iters,
ptol,
num_normal,
A,
b,
lst_proxg,
rho,
lst_g=None,
method="cg",
P=None,
ref=None,
save=None,
verbose=True,
idx=None,
):
r"""ADMM.
Solves for the following optimization problem:
.. math::
\min_x \frac{1}{2} \| A x - y \|_2^2 + \sum_i g_i(x)
Each iteration involves solving an inner least squares problem which
is parameterized by the number of A.H * A evaluations.
Based on:
Parikh, N., & Boyd, S.
Proximal algorithms.
Foundations and Trends in optimization, 1(3), 127-239.
DOI: 10.1561/2400000003
Assumes MaxEig(A.H * A) = 1.
Inputs:
num_iters (Int): Number of ADMM iterations.
ptol (Float): l1-percentage tolerance between iterates.
num_normal (Int): Number of A.H * A evaluations.
A (Linop): Forward linear operator.
b (Array): Measurement.
lst_proxg (List of Prox): List of proximal operators.
rho (Float): ADMM step size.
lst_g (List of Functions): List of regularizations. If specified,
objective over iterations are saved.
method (String): Determines the method used to solve for the inner
least squares.
- "cg": Conjugate gradient.
- "pi": Polynomial inversion.
P (None or Linop): Preconditioner for "cg".
save (None or String): If specified, path to save iterations, costs and
timings.
verbose (Bool): Print information.
idx (Tuple): If passed, slice iterates before saving.
Returns:
x (Array): Reconstruction.
"""
device = sp.get_device(b)
xp = device.xp
assert num_normal >= 1
assert method == "cg" or method == "pi"
if num_normal == 1 and method == "cg":
raise Exception("CG requires >= 2 normal evaluations.")
c = len(lst_proxg)
def calculate_objective(x):
obj = sp.to_device(0.5 * xp.linalg.norm(A * x - b) ** 2, sp.cpu_device)
for j in range(c):
obj += sp.to_device(lst_g[j](x), sp.cpu_device)
return obj
with device:
lst_time = []
lst_err = None
if type(ref) != type(None):
ref = sp.to_device(ref, device)
lst_err = ([], [])
stats = lst_g is not None and save is not None
if stats:
lst_obj = []
start_time = time.perf_counter()
AHb = A.H(b)
x = AHb.copy()
r_AHb = rho * AHb
xi = xp.zeros([1 + c] + list(x.shape), dtype=xp.complex64)
ui = xp.zeros_like(xi)
end_time = time.perf_counter()
lst_time.append(end_time - start_time)
if lst_err != None:
lst_err[0].append(calc_perc_err(ref, x, ord=1))
lst_err[1].append(calc_perc_err(ref, x, ord=2))
if stats:
lst_obj.append(calculate_objective(x))
save_helper(save, x, 0, lst_time, lst_err, idx, lst_obj)
else:
save_helper(save, x, 0, lst_time, lst_err, idx)
T = sp.linop.Identity(x.shape) + rho * A.N
if method == "pi":
P = optpoly.create_polynomial_preconditioner(
num_normal - 1, T, 1, 1 + rho, norm="l_inf", verbose=verbose
)
def prox_f(rho, v):
if method == "cg":
sp.app.App(
sp.alg.ConjugateGradient(
T, v + r_AHb, v, P=P, max_iter=num_normal - 1, tol=1e-4
),
leave_pbar=False,
show_pbar=False,
record_time=False,
).run()
else:
v = v - P * (T(v) - (v + r_AHb))
return v
if verbose:
pbar = tqdm(total=num_iters, desc="ADMM", leave=True)
for k in range(num_iters):
x_old = x.copy()
start_time = time.perf_counter()
xi[0, ...] = prox_f(rho, x - ui[0, ...])
for j in range(c):
xi[1 + j, ...] = lst_proxg[j](rho, x - ui[1 + j, ...])
x = xp.mean(xi, axis=0)
ui += xi - x[None, ...]
end_time = time.perf_counter()
lst_time.append(end_time - start_time)
if lst_err != None:
lst_err[0].append(calc_perc_err(ref, x, ord=1))
lst_err[1].append(calc_perc_err(ref, x, ord=2))
if stats:
lst_obj.append(calculate_objective(x))
save_helper(save, x, k + 1, lst_time, lst_err, idx, lst_obj)
else:
save_helper(save, x, k + 1, lst_time, lst_err, idx)
calc_tol = calc_perc_err(x_old, x, ord=1)
if verbose:
pbar.set_postfix(ptol="%0.2f%%" % calc_tol)
pbar.update()
pbar.refresh()
if calc_tol <= ptol:
break
if verbose:
pbar.close()
return x
def calc_perc_err(ref, x, ord=2, auto_normalize=True):
dev = sp.get_device(x)
xp = dev.xp
with dev:
if auto_normalize:
p = ref / xp.linalg.norm(ref.ravel(), ord=ord)
q = x / xp.linalg.norm(x.ravel(), ord=ord)
err = xp.linalg.norm((p - q).ravel(), ord=ord)
else:
err = xp.linalg.norm((ref - x).ravel(), ord=ord)
err /= xp.linalg.norm(ref.ravel(), ord=ord)
err = sp.to_device(err, sp.cpu_device)
if np.isnan(err) or np.isinf(err):
return 100
return 100 * err
def save_helper(save, x, itr, lst_time, lst_err, idx, obj=None):
if save == None:
return
tp = sp.get_array_module(x)
np.save("%s/time.npy" % save, np.cumsum(lst_time))
if idx is None:
tp.save("%s/iter_%03d.npy" % (save, itr), x)
else:
tp.save("%s/iter_%03d.npy" % (save, itr), x[idx])
if obj is not None:
np.save("%s/obj.npy" % save, lst_obj)
if lst_err is not None:
np.save("%s/err.npy" % save, lst_err)