-
Notifications
You must be signed in to change notification settings - Fork 42
/
lbfgs.py
212 lines (162 loc) · 5.73 KB
/
lbfgs.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
import torch
import numpy as np
def lbfgs(f, init, maxIter=50, gEps=1e-9, histSize=10, lr=1.0, clamp=False, display=False):
"""
input:
f: a function
in: value x; 1-d tensor
out: result fx, gradient g
init: a valid input for f
maxIter: ---
gEps: ---
histSize: ---
output:
x: argmin{x} f(x); 1-d tensor
"""
xk = init
fk, gk = f(xk)
H0 = 1.0
step = 0
stat = "LBFGS REACH MAX ITER"
alpha = list(range(histSize))
rho = []
s = []
y = []
for it in range(maxIter):
if display and (it + 1) % 20 == 0:
print(f"LBFGS | iter:{it+1}; loss:{fk:.4f}; grad:{gk.pow(2).sum().pow(0.5).item():.4f}; step:{step:.5f}")
if clamp:
xk = xk.clamp(0, 1e7)
xSquaredNorm = xk.pow(2).sum()
gSquaredNorm = gk.pow(2).sum()
if gSquaredNorm < (gEps ** 2) * xSquaredNorm:
stat = "LBFGS BELOW GRADIENT EPS"
return xk, stat
z = -gk
maxIdx = min(it, histSize)
for i in range(maxIdx):
alpha[i] = s[i].dot(z) * rho[i]
z -= alpha[i] * y[i]
z *= H0
for i in range(maxIdx-1, -1, -1):
beta = rho[i] * y[i].dot(z)
z += s[i] * (alpha[i] - beta)
fkm1, gkm1 = fk, gk
step, stat_ls, args = linesearch(xk.clone(), z, f, fk, gk.clone(), fkm1, gkm1.clone(), 10000, lr)
if step is None:
xk, fk, gk = args
return xk, stat_ls
else:
xk, fk, gk = args
if it >= histSize:
s.pop(0)
y.pop(0)
rho.pop(0)
s.append(z * step)
y.append(gk - gkm1)
yDots = y[-1].dot(s[-1])
try:
rho.append(1.0 / yDots)
except ZeroDivisionError:
print(y[-1], s[-1])
return xk, "Zero division"
yNorm2 = y[-1].dot(y[-1])
if yNorm2 > 1e-5:
H0 = yDots / yNorm2
return xk, stat
def linesearch(xk, z, f, fk, gk, fkm1, gkm1, maxEvals, lr):
c1 = 1e-4
c2 = 0.9
evals = 0
alpha_0 = 0.0
phi_0 = fkm1
phi_prime_0 = z.dot(gk)
if phi_prime_0 >= 0.0:
stat = "LINE SEARCH FAILED"
return None, stat, [xk, fk, gk]
alpha_max = 1e8
alpha = lr
alpha_old = 0.0
alpha_cor = lr
second_iter = False
while True:
xk += (alpha - alpha_old) * z
fk, gk = f(xk)
evals += 1
phi_alpha = fk
phi_prime_alpha = z.dot(gk)
armijo_violated = (phi_alpha > phi_0 + c1 * alpha * phi_prime_0 or (second_iter and phi_alpha >= phi_0))
strong_wolfe = (torch.abs(phi_prime_alpha) <= -c2 * phi_prime_0)
if (not armijo_violated) and strong_wolfe:
stat = "LINE SEARCH DONE"
return alpha, stat, [xk, fk, gk]
if evals > maxEvals:
stat = "LINE SEARCH REACH MAX EVALS"
return None, stat, [xk, fk, gk]
if armijo_violated or phi_prime_alpha >= 0:
if armijo_violated:
alpha_low = alpha_0
alpha_high = alpha
phi_low = phi_0
phi_high = phi_alpha
phi_prime_low = phi_prime_0
phi_prime_high = phi_prime_alpha
else:
alpha_low = alpha
alpha_high = alpha_0
phi_low = phi_alpha
phi_high = phi_0
phi_prime_low = phi_prime_alpha
phi_prime_high = phi_prime_0
alpha_old = alpha;
alpha = 0.5 * (alpha_low + alpha_high)
alpha += (phi_high - phi_low) / (phi_prime_low - phi_prime_high)
if (alpha < min(alpha_low, alpha_high) or alpha > max(alpha_low, alpha_high)):
alpha = 0.5 * (alpha_low + alpha_high)
alpha_cor = alpha - alpha_old
break
alpha_new = alpha + 4 * (alpha - alpha_old)
alpha_old = alpha
alpha = alpha_new
alpha_cor = alpha - alpha_old
if alpha > alpha_max:
stat = "LINE SEARCH FAILED"
return None, stat, [xk, fk, gk]
second_iter = True
tries = 0
minTries = 10
while True:
tries += 1
xk += alpha_cor * z
fk, gk = f(xk)
evals += 1
phi_j = fk
phi_prime_j = z.dot(gk)
armijo_violated = (phi_j > phi_0 + c1 * alpha * phi_prime_0 or phi_j >= phi_low)
strong_wolfe = (torch.abs(phi_prime_j) <= -c2 * phi_prime_0)
if (not armijo_violated) and strong_wolfe:
stat = "LINE SEARCH DONE"
return alpha, stat, [xk, fk, gk]
elif abs(alpha_high - alpha_low) < 1e-5 and tries > minTries:
stat = "LINE SEARCH FAILED"
return None, stat, [xk, fk, gk]
elif armijo_violated:
alpha_high = alpha
phi_high = phi_j
phi_prime_high = phi_prime_j
else:
if (phi_prime_j * (alpha_high - alpha_low) >= 0):
alpha_high = alpha_low
phi_high = phi_low
phi_prime_high = phi_prime_low
alpha_low = alpha
phi_low = phi_j
phi_prime_low = phi_prime_j
alpha = 0.5 * (alpha_low + alpha_high)
alpha += (phi_high - phi_low) / (phi_prime_low - phi_prime_high)
if (alpha < min(alpha_low, alpha_high) or alpha > max(alpha_low, alpha_high)):
alpha = 0.5 * (alpha_low + alpha_high)
alpha_cor = alpha - alpha_old
if evals >= maxEvals:
stat = "LINE SEARCH REACHED MAX EVALS"
return None, stat, [xk, fk, gk]