-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsd.py
More file actions
20 lines (18 loc) · 697 Bytes
/
sd.py
File metadata and controls
20 lines (18 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# sd.py
import numpy as np
from strong_wolfe import strong_wolfe
floatX = np.longdouble
def sd(fhandle, x0, max_iter=200000, ftol=1e-8, step_tol=1e-8,gtol=1e-8):
x = np.asarray(x0, dtype=floatX).ravel().copy()
f_prev = fhandle(x, 'f')
for k in range(1, max_iter + 1):
g = fhandle(x, 'grad')
alpha = strong_wolfe(fhandle, x, -g)
x_new = x + alpha * (-g)
f_new = fhandle(x_new, 'f')
if (abs(f_new - f_prev) < ftol and
np.linalg.norm(x_new - x) < step_tol and
np.linalg.norm(g) <= gtol):
return x_new, f_new, k
x, f_prev = x_new, f_new
return x, fhandle(x, 'f'), max_iter