-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadagrad_liam.py
More file actions
38 lines (31 loc) · 823 Bytes
/
adagrad_liam.py
File metadata and controls
38 lines (31 loc) · 823 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
35
36
37
38
# -*-coding:utf-8-*-
# Author: alphadl
# adagrad_liam.py 2018/8/21 19:58
import math
def f(x):
return x ** 3 - 2 * x - 10 + x ** 2
def derivative_f(x):
return 3 * (x ** 2) - 2 + 2 * x
"""
step 6224: x = 0.548565, f(x) = -10.631130,gradient=-0.000100
已收敛,在6224步停止
"""
x = 0.0
y = 0.0
learning_rate = 0.01
gradient = 0
e = 0.00000001
sum = 0.0
for i in range(100000):
print('step {:d}: x = {:6f}, f(x) = {:6f},gradient={:6f}'.format(i + 1, x, y, gradient))
if ((abs(gradient) > 0.00001) and (abs(gradient) < 0.0001)):
print("已收敛,在%d步停止" % (i + 1))
break
else:
gradient = derivative_f(x)
sum += gradient ** 2;
x = x - learning_rate * gradient / (math.sqrt(sum) + e)
y = f(x)
if __name__ == '__main__':
print
""