-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGD.py
More file actions
34 lines (27 loc) · 688 Bytes
/
Copy pathGD.py
File metadata and controls
34 lines (27 loc) · 688 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
lr = .1
def ham(x):
return x**2 - 5 * x + 2
X = np.arange(-100, 100, .2)
Y = ham(X)
plt.plot(X, Y)
# plt.show()
def dao_ham(x):
return 2 * x - 5
def GD(x_init, dao_ham, lr):
x = [x_init]
for it in range(100):
x_new = x[-1] - lr*dao_ham(x[-1])
if np.abs(dao_ham(x_new)) < 1e-6:
break
x.append(x_new)
if it%10 == 0:
print("Sau %d vong lap tim duoc x = %f" %(it, x[-1]))
return it, x
x_init = 0
it, x_op = GD(x_init, dao_ham, lr)
print("Optimize x = %f sau %d vong lap!" %(x_op[-1], it))
y_op = [ham(_) for _ in x_op]
plt.plot(x_op, y_op, 'r.')
plt.show()