/
descent.py
60 lines (49 loc) · 1.74 KB
/
descent.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
import matplotlib.pyplot as plt
import numpy as np
import random
import time
def minimize(f, x0, eta, h, precision):
x = x0
while True:
prev = x
finite_diff = f(x + h) - f(x) # /h rolls into learning rate
x = x - eta * finite_diff # decelerates x step as it flattens out
# print "f(%1.12f) = %1.12f delta = %1.20f" % (x, f(x), f(x) - f(prev))
if abs(x - prev) < precision:
# OR, if abs(finite_diff)<precision:
return x
def minimize_trace(f, x0, eta, h, precision):
tracex = [x0]
x = x0
while True:
prev = x
finite_diff = f(x + h) - f(x) # /h rolls into learning rate
x = x - eta * finite_diff # decelerates x step as it flattens out
tracex.append(x)
if abs(x - prev) < precision:
return tracex
def viz_trace(x0):
# Plot the damped sine curve
graphx = np.arange(.1, 1.3, 0.01)
graphy = [f(x) for x in graphx] # or just f(graphx)!
plt.plot(graphx, graphy)
random.seed(int(round(time.time() * 1000)))
tracex = minimize_trace(f, x0, ETA, h, PRECISION)
tracey = [f(x) for x in tracex]
plt.scatter(tracex, tracey, color="green", marker='o')
plt.text(0.3, 4.5, "f(x=%1.5f) = %1.9f, steps = %d" %
(tracex[-1], f(tracex[-1]), len(tracex)), fontsize=14,
color="green")
plt.show()
if __name__ == '__main__':
ETA = 10
h = 0.0001
PRECISION = 0.0000001 # can't be too small as f(x)-f(xprev) prec is low
def f(x): return np.cos(3 * np.pi * x) / x
x0 = random.uniform(.1, 1.2)
viz_trace(x0)
# Or can just call to get x
x0 = random.uniform(.1, 1.2)
minx = minimize(f, x0, ETA, h, PRECISION)
print "f(%f) = %f" % (minx, f(minx))
plt.show()