首先是公式的推导,直接看图吧
代码:
import numpy as np
import matplotlib.pyplot as plt
x_data=[1.0,2.0,3.0]
y_data=[2.0,4.0,6.0]
w=1.0
def forward(x):
return w*x
def cost(x_data,y_data):
cost=0
for x,y in zip(x_data,y_data):
y_pred=forward(x)
cost+=(y_pred-y)**2
return cost/len(x_data)
def gradient(x_data,y_data):
grad=0
for x,y in zip(x_data,y_data):
grad+=2*x*(w*x-y)
return grad/len(x_data)
print('Predict (before training)',4,forward(4))
a=[]
b=[]
for epoch in range(100):
cost_val=cost(x_data,y_data)
grad_val=gradient(x_data,y_data)
w-=0.01*grad_val
a.append(epoch)
b.append(cost_val)
print('Epoch:',epoch,'w=',w,'loss=',cost_val)
print('Predict (after training)',4,forward(4))
plt.plot(a,b)
plt.show()
随机梯度下降的代码:
#如果画出图像最后损失函数上升,就是训练失败,这时候可以尝试把学习率也就是α调小
#随机梯度下降(可能某一段是平的,你没有办法向前走)
#随机梯度,就是从你的数据中选取一个数据,而不是所有数据的和
import numpy as np
import matplotlib.pyplot as plt
x_data=[1.0,2.0,3.0]
y_data=[2.0,4.0,6.0]
w=1.0
def forward(x):
return w*x
def cost(x,y):
y_pred=forward(x)
return (y_pred-y)**2
def gradient(x,y):
return 2*(w*x-y)*x
print('Predict (before training)',4,forward(4))
a=[]
b=[]
cnt=1
for epoch in range(100):
for x,y in zip(x_data,y_data):
loss=cost(x,y)
grade=gradient(x,y)
w=w-0.01*grade
a.append(cnt)
cnt+=1
b.append(loss)
print('Predict (after training)',4,forward(4))
plt.plot(a,b)
plt.show()