forked from buptjz/LearningFromData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient_descent.py
More file actions
56 lines (39 loc) · 1.15 KB
/
gradient_descent.py
File metadata and controls
56 lines (39 loc) · 1.15 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
# -*- coding: utf-8 -*-
# __author__ = 'wangjz'
"""
Learning From Data
HW #5
Consider the nonlinear error surface E(u, v) = (uev − 2ve−u
)
2
. We start at the point
(u, v) = (1, 1) and minimize this error using gradient descent in the uv space. Use
η = 0.1 (learning rate, not step size).
How many iterations (among the given choices) does it take for the error E(u, v)
to fall below 10−14 for the first time? In your programs, make sure to use double
precision to get the needed accuracy
w(t + 1) = w(t) − η ∇Ein(w(t))
"""
from math import e
def e_func(u, v):
x = u * pow(e, v) - 2 * v * pow(e, -u)
return x * x
def partial_u(u, v):
"""计算u偏导"""
fir = 2 * (u * pow(e, v) - 2 * v * pow(e, -u))
sec = (pow(e, v) + 2 * v * pow(e, -u))
return fir * sec
def partial_v(u, v):
"""计算v偏导"""
fir = 2 * (u * pow(e, v) - 2 * v * pow(e, -u))
sec = (u * pow(e, v) - 2 * pow(e, -u))
return fir * sec
glb_u, glb_v = 1.0, 1.0
eta = 0.1
goal = pow(10, -14)
cnt = 15
while cnt > 0:
cnt -= 1
glb_u -= eta * partial_u(glb_u, glb_v)
glb_v -= eta * partial_v(glb_u, glb_v)
print e_func(glb_u, glb_v)