-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathitalgos.c
1329 lines (950 loc) · 32.2 KB
/
italgos.c
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2013-2017. The Regents of the University of California.
* Copyright 2016-2021. Uecker Lab. University Center Göttingen.
* All rights reserved. Use of this source code is governed by
* a BSD-style license which can be found in the LICENSE file.
*
* Authors:
* 2012-2018 Martin Uecker <[email protected]>
* 2013-2014 Frank Ong <[email protected]>
* 2013-2014,2017 Jon Tamir <[email protected]>
*
*
*
* Landweber L. An iteration formula for Fredholm integral equations of the
* first kind. Amer. J. Math. 1951; 73, 615-624.
*
* Nesterov Y. A method of solving a convex programming problem with
* convergence rate O (1/k2). Soviet Mathematics Doklady 1983; 27(2):372-376
*
* Bakushinsky AB. Iterative methods for nonlinear operator equations without
* regularity. New approach. In Dokl. Russian Acad. Sci 1993; 330:282-284.
*
* Daubechies I, Defrise M, De Mol C. An iterative thresholding algorithm for
* linear inverse problems with a sparsity constraint.
* Comm Pure Appl Math 2004; 57:1413-1457.
*
* Beck A, Teboulle M. A fast iterative shrinkage-thresholding algorithm for
* linear inverse problems. SIAM Journal on Imaging Sciences 2.1 2009; 183-202.
*
* Chambolle A, Pock, T. A First-Order Primal-Dual Algorithm for Convex Problems
* with Applications to Imaging. J. Math. Imaging Vis. 2011; 40, 120-145.
*
*/
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "misc/misc.h"
#include "misc/debug.h"
#include "iter/vec.h"
#include "iter/monitor.h"
#include "iter/monitor_iter6.h"
// FIXME: shouldn't this be a monitor?
#include "iter/iter_dump.h"
#include "italgos.h"
extern inline void iter_op_call(struct iter_op_s op, float* dst, const float* src);
extern inline void iter_nlop_call(struct iter_nlop_s op, int N, float* args[N]);
extern inline void iter_nlop_call_select_der(struct iter_nlop_s op, int N, float* args[N], unsigned long der_out, unsigned long der_in);
extern inline void iter_op_p_call(struct iter_op_p_s op, float rho, float* dst, const float* src);
extern inline void iter_op_arr_call(struct iter_op_arr_s op, int NO, unsigned long oflags, float* dst[NO], int NI, unsigned long iflags, const float* src[NI]);
/**
* ravine step
* (Nesterov 1983)
*/
static void ravine(const struct vec_iter_s* vops, long N, float* ftp, float* xa, float* xb)
{
float ft = *ftp;
float tfo = ft;
ft = (1.f + sqrtf(1.f + 4.f * ft * ft)) / 2.f;
*ftp = ft;
vops->swap(N, xa, xb);
vops->axpy(N, xa, (1.f - tfo) / ft - 1.f, xa);
vops->axpy(N, xa, (tfo - 1.f) / ft + 1.f, xb);
}
void landweber_sym(unsigned int maxiter, float epsilon, float alpha, long N,
const struct vec_iter_s* vops,
struct iter_op_s op,
float* x, const float* b,
struct iter_monitor_s* monitor)
{
float* r = vops->allocate(N);
double rsnot = vops->norm(N, b);
for (unsigned int i = 0; i < maxiter; i++) {
iter_monitor(monitor, vops, x);
iter_op_call(op, r, x); // r = A x
vops->xpay(N, -1., r, b); // r = b - r = b - A x
double rsnew = vops->norm(N, r);
debug_printf(DP_DEBUG3, "#%d: %f\n", i, rsnew / rsnot);
if (rsnew < epsilon)
break;
vops->axpy(N, x, alpha, r);
}
vops->del(r);
}
/**
* Iterative Soft Thresholding
*
* @param maxiter maximum number of iterations
* @param epsilon stop criterion
* @param tau (step size) weighting on the residual term, A^H (b - Ax)
* @param N size of input, x
* @param vops vector ops definition
* @param op linear operator, e.g. A
* @param thresh threshold function, e.g. complex soft threshold
* @param x initial estimate
* @param b observations
* @param monitor compute objective value, errors, etc.
*/
void ist(unsigned int maxiter, float epsilon, float tau, long N,
const struct vec_iter_s* vops,
ist_continuation_t ist_continuation,
struct iter_op_s op,
struct iter_op_p_s thresh,
float* x, const float* b,
struct iter_monitor_s* monitor)
{
struct ist_data itrdata = {
.rsnew = 1.,
.rsnot = 1.,
.iter = 0,
.maxiter = maxiter,
.tau = tau,
.scale = 1.,
};
float* r = vops->allocate(N);
itrdata.rsnot = vops->norm(N, b);
for (itrdata.iter = 0; itrdata.iter < maxiter; itrdata.iter++) {
iter_monitor(monitor, vops, x);
if (NULL != ist_continuation)
ist_continuation(&itrdata);
iter_op_p_call(thresh, itrdata.scale * itrdata.tau, x, x);
iter_op_call(op, r, x); // r = A x
vops->xpay(N, -1., r, b); // r = b - r = b - A x
itrdata.rsnew = vops->norm(N, r);
debug_printf(DP_DEBUG3, "#It %03d: %f \n", itrdata.iter, itrdata.rsnew / itrdata.rsnot);
if (itrdata.rsnew < epsilon)
break;
vops->axpy(N, x, itrdata.tau, r);
}
debug_printf(DP_DEBUG3, "\n");
vops->del(r);
}
/**
* Iterative Soft Thresholding/FISTA to solve min || b - Ax ||_2 + lambda || T x ||_1
*
* @param maxiter maximum number of iterations
* @param epsilon stop criterion
* @param tau (step size) weighting on the residual term, A^H (b - Ax)
* @param N size of input, x
* @param vops vector ops definition
* @param op linear operator, e.g. A
* @param thresh threshold function, e.g. complex soft threshold
* @param x initial estimate
* @param b observations
*/
void fista(unsigned int maxiter, float epsilon, float tau,
long N,
const struct vec_iter_s* vops,
ist_continuation_t ist_continuation,
struct iter_op_s op,
struct iter_op_p_s thresh,
float* x, const float* b,
struct iter_monitor_s* monitor)
{
struct ist_data itrdata = {
.rsnew = 1.,
.rsnot = 1.,
.iter = 0,
.maxiter = maxiter,
.tau = tau,
.scale = 1.,
};
float* r = vops->allocate(N);
float* o = vops->allocate(N);
float ra = 1.;
vops->copy(N, o, x);
itrdata.rsnot = vops->norm(N, b);
for (itrdata.iter = 0; itrdata.iter < maxiter; itrdata.iter++) {
iter_monitor(monitor, vops, x);
if (NULL != ist_continuation)
ist_continuation(&itrdata);
iter_op_p_call(thresh, itrdata.scale * itrdata.tau, x, x);
ravine(vops, N, &ra, x, o); // FISTA
iter_op_call(op, r, x); // r = A x
vops->xpay(N, -1., r, b); // r = b - r = b - A x
itrdata.rsnew = vops->norm(N, r);
debug_printf(DP_DEBUG3, "#It %03d: %f \n", itrdata.iter, itrdata.rsnew / itrdata.rsnot);
if (itrdata.rsnew < epsilon)
break;
vops->axpy(N, x, itrdata.tau, r);
}
debug_printf(DP_DEBUG3, "\n");
debug_printf(DP_DEBUG2, "\t\tFISTA iterations: %u\n", itrdata.iter);
vops->del(o);
vops->del(r);
}
/**
* Landweber L. An iteration formula for Fredholm integral equations of the
* first kind. Amer. J. Math. 1951; 73, 615-624.
*/
void landweber(unsigned int maxiter, float epsilon, float alpha, long N, long M,
const struct vec_iter_s* vops,
struct iter_op_s op,
struct iter_op_s adj,
float* x, const float* b,
struct iter_op_s callback,
struct iter_monitor_s* monitor)
{
float* r = vops->allocate(M);
float* p = vops->allocate(N);
double rsnot = vops->norm(M, b);
for (unsigned int i = 0; i < maxiter; i++) {
iter_monitor(monitor, vops, x);
iter_op_call(op, r, x); // r = A x
vops->xpay(M, -1., r, b); // r = b - r = b - A x
double rsnew = vops->norm(M, r);
debug_printf(DP_DEBUG3, "#%d: %f\n", i, rsnew / rsnot);
if (rsnew < epsilon)
break;
iter_op_call(adj, p, r);
vops->axpy(N, x, alpha, p);
if (NULL != callback.fun)
iter_op_call(callback, x, x);
}
vops->del(r);
vops->del(p);
}
/**
* Conjugate Gradient Descent to solve Ax = b for symmetric A
*
* @param maxiter maximum number of iterations
* @param regularization parameter
* @param epsilon stop criterion
* @param N size of input, x
* @param vops vector ops definition
* @param linop linear operator, i.e. A
* @param x initial estimate
* @param b observations
*/
float conjgrad(unsigned int maxiter, float l2lambda, float epsilon,
long N,
const struct vec_iter_s* vops,
struct iter_op_s linop,
float* x, const float* b,
struct iter_monitor_s* monitor)
{
float* r = vops->allocate(N);
float* p = vops->allocate(N);
float* Ap = vops->allocate(N);
// The first calculation of the residual might not
// be necessary in some cases...
iter_op_call(linop, r, x); // r = A x
vops->axpy(N, r, l2lambda, x);
vops->xpay(N, -1., r, b); // r = b - r = b - A x
vops->copy(N, p, r); // p = r
float rsnot = (float)pow(vops->norm(N, r), 2.);
float rsold = rsnot;
float rsnew = rsnot;
float eps_squared = pow(epsilon, 2.);
unsigned int i = 0;
if (0. == rsold) {
debug_printf(DP_DEBUG3, "CG: early out\n");
goto cleanup;
}
for (i = 0; i < maxiter; i++) {
iter_monitor(monitor, vops, x);
debug_printf(DP_DEBUG3, "#%d: %f\n", i, (double)sqrtf(rsnew));
iter_op_call(linop, Ap, p); // Ap = A p
vops->axpy(N, Ap, l2lambda, p);
float pAp = (float)vops->dot(N, p, Ap);
if (0. == pAp)
break;
float alpha = rsold / pAp;
vops->axpy(N, x, +alpha, p);
vops->axpy(N, r, -alpha, Ap);
rsnew = pow(vops->norm(N, r), 2.);
float beta = rsnew / rsold;
rsold = rsnew;
if (rsnew <= eps_squared)
break;
vops->xpay(N, beta, p, r); // p = beta * p + r
}
cleanup:
vops->del(Ap);
vops->del(p);
vops->del(r);
debug_printf(DP_DEBUG2, "\t cg: %3d\n", i);
return sqrtf(rsnew);
}
/**
* Iteratively Regularized Gauss-Newton Method
* (Bakushinsky 1993)
*
* y = F(x) = F xn + DF dx + ...
*
* IRGNM: DF^H ((y - F xn) + DF (xn - x0)) = ( DF^H DF + alpha ) (dx + xn - x0)
* DF^H ((y - F xn)) - alpha (xn - x0) = ( DF^H DF + alpha) dx
*
* This version only solves the second equation for the update 'dx'. This corresponds
* to a least-squares problem where the quadratic regularization applies to the difference
* to 'x0'.
*/
void irgnm(unsigned int iter, float alpha, float alpha_min, float redu, long N, long M,
const struct vec_iter_s* vops,
struct iter_op_s op,
struct iter_op_s adj,
struct iter_op_p_s inv,
float* x, const float* xref, const float* y,
struct iter_op_s callback,
struct iter_monitor_s* monitor)
{
float* r = vops->allocate(M);
float* p = vops->allocate(N);
float* h = vops->allocate(N);
for (unsigned int i = 0; i < iter; i++) {
iter_monitor(monitor, vops, x);
iter_op_call(op, r, x); // r = F x
vops->xpay(M, -1., r, y); // r = y - F x
debug_printf(DP_DEBUG2, "Step: %u, Res: %f\n", i, vops->norm(M, r));
iter_op_call(adj, p, r);
if (NULL != xref)
vops->axpy(N, p, +alpha, xref);
vops->axpy(N, p, -alpha, x);
iter_op_p_call(inv, alpha, h, p);
vops->axpy(N, x, 1., h);
alpha = (alpha - alpha_min) / redu + alpha_min;
if (NULL != callback.fun)
iter_op_call(callback, x, x);
}
vops->del(h);
vops->del(p);
vops->del(r);
}
/**
* Iteratively Regularized Gauss-Newton Method
* (Bakushinsky 1993)
*
* y = F(x) = F xn + DF dx + ...
*
* IRGNM: R(DF^H, DF^H DF, alpha) ((y - F xn) + DF (xn - x0)) = (dx + xn - x0)
*
* This version has an extra call to DF, but we can use a generic regularized
* least-squares solver.
*/
void irgnm2(unsigned int iter, float alpha, float alpha_min, float alpha_min0, float redu, long N, long M,
const struct vec_iter_s* vops,
struct iter_op_s op,
struct iter_op_s der,
struct iter_op_p_s lsqr,
float* x, const float* xref, const float* y,
struct iter_op_s callback,
struct iter_monitor_s* monitor)
{
float* r = vops->allocate(M);
float* q = vops->allocate(M);
for (unsigned int i = 0; i < iter; i++) {
iter_monitor(monitor, vops, x);
iter_op_call(op, r, x); // r = F x
vops->xpay(M, -1., r, y); // r = y - F x
debug_printf(DP_DEBUG2, "Step: %u, Res: %f\n", i, vops->norm(M, r));
if (NULL != xref)
vops->axpy(N, x, -1., xref);
iter_op_call(der, q, x);
vops->axpy(M, r, +1., q);
iter_op_p_call(lsqr, alpha, x, r);
if (NULL != xref)
vops->axpy(N, x, +1., xref);
alpha = (alpha - alpha_min) / redu + alpha_min;
if (alpha < alpha_min0)
alpha = alpha_min0;
if (NULL != callback.fun)
iter_op_call(callback, x, x);
}
vops->del(q);
vops->del(r);
}
/**
* Alternating Minimzation
*
* Minimize residual by calling each min_op in turn.
*/
void altmin(unsigned int iter, float alpha, float redu,
long N,
const struct vec_iter_s* vops,
unsigned int NI,
struct iter_nlop_s op,
struct iter_op_p_s min_ops[__VLA(NI)],
float* x[__VLA(NI)], const float* y,
struct iter_nlop_s callback)
{
float* r = vops->allocate(N);
vops->clear(N, r);
float* args[1 + NI];
args[0] = r;
for (long i = 0; i < NI; ++i)
args[1 + i] = x[i];
for (unsigned int i = 0; i < iter; i++) {
for (unsigned int j = 0; j < NI; ++j) {
iter_nlop_call(op, 1 + NI, args); // r = F x
vops->xpay(N, -1., r, y); // r = y - F x
debug_printf(DP_DEBUG2, "Step: %u, Res: %f\n", i, vops->norm(N, r));
iter_op_p_call(min_ops[j], alpha, x[j], y);
if (NULL != callback.fun)
iter_nlop_call(callback, NI, x);
}
alpha /= redu;
}
vops->del(r);
}
/**
* Projection onto Convex Sets
*
* minimize 0 subject to: x in C_1, x in C_2, ..., x in C_D,
* where the C_i are convex sets
*/
void pocs(unsigned int maxiter,
unsigned int D, struct iter_op_p_s proj_ops[static D],
const struct vec_iter_s* vops,
long N, float* x,
struct iter_monitor_s* monitor)
{
UNUSED(N);
UNUSED(vops);
for (unsigned int i = 0; i < maxiter; i++) {
debug_printf(DP_DEBUG3, "#Iter %d\n", i);
iter_monitor(monitor, vops, x);
for (unsigned int j = 0; j < D; j++)
iter_op_p_call(proj_ops[j], 1., x, x); // use temporary memory here?
}
}
/**
* Power iteration
*/
double power(unsigned int maxiter,
long N,
const struct vec_iter_s* vops,
struct iter_op_s op,
float* u)
{
double s = vops->norm(N, u);
vops->smul(N, 1. / s, u, u);
for (unsigned int i = 0; i < maxiter; i++) {
iter_op_call(op, u, u); // r = A x
s = vops->norm(N, u);
vops->smul(N, 1. / s, u, u);
}
return s;
}
/**
* Chambolle Pock First Order Primal Dual algorithm. Solves min_x F(Ax) + G(x)
*
* @param maxiter maximum number of iterations
* @param epsilon stop criterion
* @param tau primal step size
* @param sigma dual step size
* @param decay decay rate
* @param theta convex combination rate
* @param N size of input, x
* @param M size of transformed input, Ax
* @param vops vector ops definition
* @param op_forw forward operator, A
* @param op_adj adjoint operator, AH
* @param prox1 proximal function of F, e.g. prox_l2ball
* @param prox2 proximal function of G, e.g. prox_wavelet_thresh
* @param x initial estimate
* @param monitor callback function
*/
void chambolle_pock(unsigned int maxiter, float epsilon, float tau, float sigma, float theta, float decay,
long N, long M,
const struct vec_iter_s* vops,
struct iter_op_s op_forw,
struct iter_op_s op_adj,
struct iter_op_p_s prox1,
struct iter_op_p_s prox2,
float* x,
struct iter_monitor_s* monitor)
{
float* x_avg = vops->allocate(N);
float* x_old = vops->allocate(N);
float* x_new = vops->allocate(N);
float* u_old = vops->allocate(M);
float* u = vops->allocate(M);
float* u_new = vops->allocate(M);
vops->copy(N, x_old, x);
vops->copy(N, x_new, x);
vops->copy(N, x_avg, x);
vops->clear(M, u);
vops->clear(M, u_new);
vops->clear(M, u_old);
for (unsigned int i = 0; i < maxiter; i++) {
float lambda = (float)pow(decay, i);
/* update u
* u0 = u
* p = u + sigma * A(x)
* u = p - sigma * prox1(p / sigma, 1 / sigma)
* u = lambda * u + (1 - lambda) * u0
*/
iter_op_call(op_forw, u_old, x_avg);
vops->axpy(M, u_old, 1. / sigma, u); // (u + sigma * A(x)) / sigma
iter_op_p_call(prox1, 1. / sigma, u_new, u_old);
vops->axpbz(M, u_new, -1. * sigma, u_new, sigma, u_old);
vops->copy(M, u_old, u);
vops->axpbz(M, u, lambda, u_new, 1. - lambda, u_old);
/* update x
* x0 = x
* q = x0 - tau * AH(u)
* x = prox2(q, tau)
* x = lambda * x + (1 - lambda * x0)
*/
vops->copy(N, x_old, x);
iter_op_call(op_adj, x_new, u);
vops->axpy(N, x, -1. * tau, x_new);
iter_op_p_call(prox2, tau, x_new, x);
vops->axpbz(N, x, lambda, x_new, 1. - lambda, x_old);
/* update x_avg
* a_avg = x + theta * (x - x0)
*/
vops->axpbz(N, x_avg, 1 + theta, x, -1. * theta, x_old);
// residual
vops->sub(N, x_old, x, x_old);
vops->sub(M, u_old, u, u_old);
float res1 = vops->norm(N, x_old) / sigma;
float res2 = vops->norm(M, u_old) / tau;
iter_monitor(monitor, vops, x);
debug_printf(DP_DEBUG3, "#It %03d: %f %f \n", i, res1, res2);
if (epsilon > (res1 + res2))
break;
}
debug_printf(DP_DEBUG3, "\n");
vops->del(x_avg);
vops->del(x_old);
vops->del(x_new);
vops->del(u_old);
vops->del(u);
vops->del(u_new);
}
/**
* Compute the sum of the selected outputs, selected outputs must be scalars
*
* @param NO number of outputs of nlop
* @param NI number of inputs of nlop
* @param nlop nlop to apply
* @param args out- and inputs of operator
* @param out_optimize_flag sums outputs over selected outputs, selected outputs must be scalars
* @param der_in_flag only information to compute derivatives with respect to selected inputs are stores
* @param vops vector operators
**/
static float compute_objective(long NO, long NI, struct iter_nlop_s nlop, float* args[NO + NI], unsigned long out_optimize_flag, unsigned long der_in_flag, const struct vec_iter_s* vops)
{
float result = 0;
iter_nlop_call_select_der(nlop, NO + NI, args, out_optimize_flag, der_in_flag); // r = F x
for (int o = 0; o < NO; o++) {
if (MD_IS_SET(out_optimize_flag, o)) {
float tmp;
vops->copy(1, &tmp, args[o]);
result += tmp;
}
}
return result;
}
/**
* Compute the gradient with respect to the inputs selected by in_optimize_flag.
* The result is the sum of the gradients with respect to the outputs selected by out_optimize_flag
*
* @param NI number of inputs of nlop
* @param in_optimize_flag compute gradients with respect to selected inputs
* @param isize sizes of input tensors
* @param grad output of the function, grad[i] must be allocated for selected inputs
* @param NO number of outputs of nlop
* @param out_optimize_flag sums gradients over selected outputs, selected outputs must be scalars
* @param adj array of adjoint operators
* @param vops vector operators
**/
static void getgrad(int NI, unsigned long in_optimize_flag, long isize[NI], float* grad[NI], int NO, unsigned long out_optimize_flag, struct iter_op_arr_s adj, const struct vec_iter_s* vops)
{
float* one = vops->allocate(2);
float one_var[2] = { 1., 0. }; // complex
const float* one_arr[] = { one };
vops->copy(2, one, one_var);
float* tmp_grad[NI];
for (int i = 0; i < NI; i++)
if ((1 < NO) && MD_IS_SET(in_optimize_flag, i))
tmp_grad[i] = vops->allocate(isize[i]);
for (int o = 0, count = 0; o < NO; o++) {
if (!MD_IS_SET(out_optimize_flag, o))
continue;
iter_op_arr_call(adj, NI, in_optimize_flag, (0 == count) ? grad : tmp_grad, 1, MD_BIT(o), one_arr);
for (int i = 0; i < NI; i++)
if ((0 < count) && MD_IS_SET(in_optimize_flag, i))
vops->add(isize[i], grad[i], grad[i], tmp_grad[i]);
count += 1;
}
for (int i = 0; i < NI; i++)
if ((1 < NO) && MD_IS_SET(in_optimize_flag, i))
vops->del(tmp_grad[i]);
vops->del(one);
}
/**
* Prototype for sgd-like algorithm
* The gradient is computed and the operator "update" computes the update, this operator can remember information such as momentum
*
* @param epochs number of epochs to train (one epoch corresponds to seeing each dataset once)
* @param batches number of updates per epoch
* @param learning_rate (overwritten by learning_rate_schedule if != NULL)
* @param batchnorm_momentum momentum for updating mean and variance of batch normalization
* @param learning_rate_schedule learning rate for each update
* @param NI number of input tensors
* @param isize size of input tensors (flattened as real)
* @param in_type type of inputs (static, batchgen, to optimize)
* @param x inputs of operator (weights, train data, reference data)
* @param NO number of output tensors (i.e. objectives)
* @param osize size of output tensors (flattened as real)
* @param out_type type of output (i.e. should be minimized)
* @param N_batch batch size
* @param N_total total size of datasets
* @param vops
* @param nlop nlop for minimization
* @param adj array of adjoints of nlop
* @param prox prox operators applied after each update on the current weights
* @param nlop_batch_gen nlop for generating a new batch for each update
* @param update diagonal array of operator computing the update based on the gradient
* @param callback UNUSED
* @param monitor UNUSED
*/
void sgd( unsigned int epochs, unsigned int batches,
float learning_rate, float batchnorm_momentum,
float learning_rate_schedule[epochs][batches],
long NI, long isize[NI], enum IN_TYPE in_type[NI], float* x[NI],
long NO, long osize[NO], enum OUT_TYPE out_type[NI],
int N_batch, int N_total,
const struct vec_iter_s* vops,
struct iter_nlop_s nlop, struct iter_op_arr_s adj,
struct iter_op_p_s update[NI],
struct iter_op_p_s prox[NI],
struct iter_nlop_s nlop_batch_gen,
struct iter_op_s callback, struct monitor_iter6_s* monitor, const struct iter_dump_s* dump)
{
UNUSED(callback);
float* grad[NI];
float* dxs[NI];
float* args[NO + NI];
float* x_batch_gen[NI]; //arrays which are filled by batch generator
long N_batch_gen = 0;
unsigned long in_optimize_flag = 0;
unsigned long out_optimize_flag = 0;
if ((int)batches != N_total / N_batch)
error("Wrong number of batches!");
for (int i = 0; i < NI; i++) {
switch (in_type[i]) {
case IN_STATIC:
grad[i] = NULL;
dxs[i] = NULL;
break;
case IN_BATCH:
grad[i] = NULL;
dxs[i] = NULL;
break;
case IN_OPTIMIZE:
grad[i] = vops->allocate(isize[i]);
dxs[i] = vops->allocate(isize[i]);
in_optimize_flag = MD_SET(in_optimize_flag, i);
if (NULL != prox[i].fun)
iter_op_p_call(prox[i], 0, x[i], x[i]); //project to constraint
break;
case IN_BATCH_GENERATOR:
grad[i] = NULL;
dxs[i] = NULL;
if (NULL != x[i])
error("NULL != x[%d] for batch generator\n", i);
x[i] = vops->allocate(isize[i]);
x_batch_gen[N_batch_gen] = x[i];
N_batch_gen += 1;
break;
case IN_BATCHNORM:
grad[i] = NULL;
dxs[i] = NULL;
break;
default:
error("unknown flag\n");
break;
}
args[NO + i] = x[i];
}
for (int o = 0; o < NO; o++) {
args[o] = vops->allocate(osize[o]);
if (OUT_OPTIMIZE == out_type[o])
out_optimize_flag = MD_SET(out_optimize_flag, o);
}
for (unsigned int epoch = 0; epoch < epochs; epoch++) {
iter_dump(dump, epoch, NI, (const float**)x);
for (int i_batch = 0; i_batch < N_total / N_batch; i_batch++) {
if (0 != N_batch_gen)
iter_nlop_call(nlop_batch_gen, N_batch_gen, x_batch_gen);
float r0 = compute_objective(NO, NI, nlop, args, out_optimize_flag, in_optimize_flag, vops); // update graph and compute loss
getgrad(NI, in_optimize_flag, isize, grad, NO, out_optimize_flag, adj, vops);
int batchnorm_counter = 0;
if (NULL != learning_rate_schedule)
learning_rate = learning_rate_schedule[epoch][i_batch];
for (int i = 0; i < NI; i++) {
if (in_type[i] == IN_OPTIMIZE) {
iter_op_p_call(update[i], learning_rate, dxs[i], grad[i]);
vops->add(isize[i], args[NO + i], args[NO + i], dxs[i]);
if (NULL != prox[i].fun)
iter_op_p_call(prox[i], learning_rate, args[NO + i], args[NO + i]);
}
if (in_type[i] == IN_BATCH)
args[NO + i] += isize[i];
if (in_type[i] == IN_BATCHNORM) {
int o = 0;
int j = batchnorm_counter;
while ((OUT_BATCHNORM != out_type[o]) || (j > 0)) {
if (OUT_BATCHNORM == out_type[o])
j--;
o++;
}
vops->smul(isize[i], batchnorm_momentum, x[i], x[i]);
vops->axpy(isize[i], x[i], 1. - batchnorm_momentum, args[o]);
batchnorm_counter++;
}
}
monitor_iter6(monitor, epoch, i_batch, N_total / N_batch, r0, NI, (const float**)x, NULL);
}
for (int i = 0; i < NI; i++)
if (in_type[i] == IN_BATCH)
args[NO + i] -= isize[i] * (N_total / N_batch);
}
for (int i = 0; i < NI; i++) {
if (NULL != grad[i])
vops->del(grad[i]);
if (NULL != dxs[i])
vops->del(dxs[i]);