forked from LeronQ/DeepLearningPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient.py
More file actions
80 lines (50 loc) · 1.18 KB
/
Copy pathgradient.py
File metadata and controls
80 lines (50 loc) · 1.18 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# coding: utf-8
# In[21]:
'''
梯度下降-所有样本求损失
'''
x_data = [1.0,2.0,3.0]
y_data = [2.0,4.0,6.0]
w = 1.0
def forward(x):
return x*x
def cost(xs,ys):
cost = 0
for x,y in zip(xs,ys):
y_pred = forward(x)
cost += (y_pred-y)**2
return cost/len(xs)
def gradient(xs,ys):
grad = 0
for x,y in zip(xs,ys):
grad += 2*x*(x*w-y)
return grad/len(xs)
loss = []
for epoch in range(1000):
cost_val = cost(x_data,y_data)
loss.append(cost_val)
grad_val = gradient(x_data,y_data)
w -= 0.01 * grad_val
print("Epoch:",epoch,"w:",w,"loss:",cost_val)
print("predict after training",4,forward(4))
# In[23]:
'''
随机梯度下降-每个样本求损失
'''
x_data = [1.0,2.0,3.0]
y_data = [2.0,4.0,6.0]
w = 1.0
def forward(x):
return x*x
def loss(xs,ys):
y_pred = forward(x)
return (y_pred-y)**2
def gradient(x,y):
return 2*x*(x*w-y)
for epoch in range(1000):
for x,y in zip(x_data,y_data):
grad = gradient(x,y)
w -= 0.01*grad
print("\tgrad",x,y,grad)
l = loss(x,y)
print("Epoch:",epoch,"w:",w,"loss:",l)