-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgd.py
More file actions
executable file
·57 lines (40 loc) · 1.17 KB
/
gd.py
File metadata and controls
executable file
·57 lines (40 loc) · 1.17 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
#!/usr/bin/env python3
# gradient descent
import math
import sys
def gauss(x, offset):
return -1.0*math.exp(-1.0*((x-offset)**2))
def grad_gauss(x, offset):
return -2.0*(x-offset)*gauss(x, offset)
def gradient_descent(grad_func, offset, x0, tol, iter_max, alpha):
x = x0
xnew = 0.0
for iter in range(iter_max):
xnew = x - alpha*grad_func(x, offset)
diff = abs(xnew - x)
if iter%10 == 0:
print("iter:", iter, "x:", x, "diff:", diff)
if diff < tol:
print("converged, iter:", iter)
break
if iter == iter_max-1 and diff >= tol:
print("gradient descent did not converge.")
sys.exit(-1)
x = xnew
return x
def main():
offset = 0.25
x0 = 1.0
tol = 1.0e-40
iter_max = 1000
argv = sys.argv
argc = len(argv)
if argc == 1:
alpha = 0.1
else:
alpha = float(argv[1])
print("alpha:", alpha, "offset:", offset)
res = gradient_descent(grad_gauss, offset, x0, tol, iter_max, alpha)
print("minimum value at x =", res)
if __name__ == "__main__":
main()