Repository navigation
Expand file tree
/
Copy pathhessianApprox.cpp
More file actions
279 lines (238 loc) · 6.88 KB
/
Copy pathhessianApprox.cpp
File metadata and controls
279 lines (238 loc) · 6.88 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
// Copyright
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Underlying paper:
//
// Layer-Parallel Training of Deep Residual Neural Networks
// S. Guenther, L. Ruthotto, J.B. Schroder, E.C. Czr, and N.R. Gauger
//
// Download: https://arxiv.org/pdf/1812.04352.pdf
//
#include "hessianApprox.hpp"
HessianApprox::HessianApprox(MPI_Comm comm) {
dimN = 0;
MPIcomm = comm;
}
HessianApprox::~HessianApprox() {}
L_BFGS::L_BFGS(MPI_Comm comm, int N, int stages) : HessianApprox(comm) {
dimN = N;
M = stages;
H0 = 1.0;
/* Allocate memory for sk and yk for all stages */
s = new MyReal *[M];
y = new MyReal *[M];
for (int imem = 0; imem < M; imem++) {
s[imem] = new MyReal[dimN];
y[imem] = new MyReal[dimN];
for (int i = 0; i < dimN; i++) {
s[imem][i] = 0.0;
y[imem][i] = 0.0;
}
}
/* Allocate memory for rho's values */
rho = new MyReal[M];
for (int i = 0; i < M; i++) {
rho[i] = 0.0;
}
/* Allocate memory for storing design at previous iteration */
design_old = new MyReal[dimN];
gradient_old = new MyReal[dimN];
}
L_BFGS::~L_BFGS() {
/* Deallocate memory */
delete[] rho;
for (int imem = 0; imem < M; imem++) {
delete[] s[imem];
delete[] y[imem];
}
delete[] s;
delete[] y;
delete[] design_old;
delete[] gradient_old;
}
void L_BFGS::computeAscentDir(int iter, MyReal *gradient, MyReal *ascentdir) {
int imemory;
MyReal beta;
MyReal *alpha = new MyReal[M];
int imax, imin;
/* Initialize the ascentdir with steepest descent */
for (int idir = 0; idir < dimN; idir++) {
ascentdir[idir] = gradient[idir];
}
/* Set range of the two-loop recursion */
imax = iter - 1;
if (iter < M) {
imin = 0;
} else {
imin = iter - M;
}
/* Loop backwards through lbfgs memory */
for (int i = imax; i >= imin; i--) {
imemory = i % M;
/* Compute alpha */
alpha[imemory] =
rho[imemory] * vecdot_par(dimN, s[imemory], ascentdir, MPIcomm);
/* Update the ascentdir */
for (int idir = 0; idir < dimN; idir++) {
ascentdir[idir] -= alpha[imemory] * y[imemory][idir];
}
}
/* scale the ascentdir by H0 */
for (int idir = 0; idir < dimN; idir++) {
ascentdir[idir] *= H0;
}
/* loop forwards through the l-bfgs memory */
for (int i = imin; i <= imax; i++) {
imemory = i % M;
/* Compute beta */
beta = rho[imemory] * vecdot_par(dimN, y[imemory], ascentdir, MPIcomm);
/* Update the ascentdir */
for (int idir = 0; idir < dimN; idir++) {
ascentdir[idir] += s[imemory][idir] * (alpha[imemory] - beta);
}
}
delete[] alpha;
}
void L_BFGS::updateMemory(int iter, MyReal *design, MyReal *gradient) {
/* Update lbfgs memory only if iter > 0 */
if (iter > 0) {
MyReal yTy, yTs;
/* Get storing state */
int imemory = (iter - 1) % M;
/* Update BFGS memory for s, y */
for (int idir = 0; idir < dimN; idir++) {
y[imemory][idir] = gradient[idir] - gradient_old[idir];
s[imemory][idir] = design[idir] - design_old[idir];
}
/* Update rho and H0 */
yTs = vecdot_par(dimN, y[imemory], s[imemory], MPIcomm);
yTy = vecdot_par(dimN, y[imemory], y[imemory], MPIcomm);
if (yTs == 0.0) {
printf(" Warning: resetting yTs to 1.\n");
yTs = 1.0;
}
if (yTy == 0.0) {
printf(" Warning: resetting yTy to 1.\n");
yTy = 1.;
}
rho[imemory] = 1. / yTs;
H0 = yTs / yTy;
}
/* Update old design and gradient */
vec_copy(dimN, design, design_old);
vec_copy(dimN, gradient, gradient_old);
}
BFGS::BFGS(MPI_Comm comm, int N) : HessianApprox(comm) {
dimN = N;
Hessian = new MyReal[N * N];
setIdentity();
y = new MyReal[N];
s = new MyReal[N];
Hy = new MyReal[N];
A = new MyReal[N * N];
B = new MyReal[N * N];
/* Allocate memory for storing design at previous iteration */
design_old = new MyReal[dimN];
gradient_old = new MyReal[dimN];
/* Sanity check */
int size;
MPI_Comm_size(MPIcomm, &size);
if (size > 1)
printf(
"\n\n WARNING: Parallel BFGS not implemented.\n BFGS updates will "
"be LOCAL to each processor -> block-BFGS. \n\n");
}
void BFGS::setIdentity() {
for (int i = 0; i < dimN; i++) {
for (int j = 0; j < dimN; j++) {
if (i == j)
Hessian[i * dimN + j] = 1.0;
else
Hessian[i * dimN + j] = 0.0;
}
}
}
BFGS::~BFGS() {
delete[] Hessian;
delete[] y;
delete[] s;
delete[] A;
delete[] B;
delete[] Hy;
delete[] design_old;
delete[] gradient_old;
}
void BFGS::updateMemory(int iter, MyReal *design, MyReal *gradient) {
/* Update BFGS memory for s, y */
for (int idir = 0; idir < dimN; idir++) {
y[idir] = gradient[idir] - gradient_old[idir];
s[idir] = design[idir] - design_old[idir];
}
}
void BFGS::computeAscentDir(int iter, MyReal *gradient, MyReal *ascentdir) {
MyReal yTy, yTs, H0;
MyReal b, rho;
/* Steepest descent in first iteration */
if (iter == 0) {
setIdentity();
matvec(dimN, Hessian, gradient, ascentdir);
return;
}
/* Check curvature conditoin */
yTs = vecdot(dimN, y, s);
if (yTs < 1e-12) {
printf(" Warning: Curvature condition not satisfied %1.14e \n", yTs);
setIdentity();
} else {
/* Scale first Hessian approximation */
yTy = vecdot(dimN, y, y);
if (iter == 1) {
H0 = yTs / yTy;
for (int i = 0; i < dimN; i++) {
Hessian[i * dimN + i] = H0;
}
}
/** BFGS Update for H (Noceda, Wright, Chapter 6.1)
* H_new = H + \rho( B - (A+A'))
* where B = (1.0 + \rho * y'Hy) * ss'
* A = Hys'
*/
/* Compute A = Hys' */
matvec(dimN, Hessian, y, Hy);
vecvecT(dimN, Hy, s, A);
/* scalar 1 + rho y'Hy */
rho = 1. / yTs;
b = 1.0 + rho * vecdot(dimN, y, Hy);
/* Compute B */
vecvecT(dimN, s, s, B);
/* H += rho * (b*B - (A+A')) */
for (int i = 0; i < dimN; i++) {
for (int j = 0; j < dimN; j++) {
Hessian[i * dimN + j] +=
rho * (b * B[i * dimN + j] - A[i * dimN + j] - A[j * dimN + i]);
}
}
}
/* Compute the ascentdir */
matvec(dimN, Hessian, gradient, ascentdir);
}
Identity::Identity(MPI_Comm comm, int N) : HessianApprox(comm) { dimN = N; }
Identity::~Identity() {}
void Identity::updateMemory(int iter, MyReal *design, MyReal *gradient) {}
void Identity::computeAscentDir(int iter, MyReal *gradient, MyReal *ascentdir) {
/* Steepest descent */
for (int i = 0; i < dimN; i++) {
ascentdir[i] = gradient[i];
}
}