1.使用梯度下降算法其实是寻找的局部最优点,即寻找最优权重使得在此权重下的代价函数最小
2.梯度下降的代码实现以及图像
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 x*w
#定义cost函数
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):
y_pred = forward(x)
grad += 2*x*(y_pred-y)
return grad/len(xs)
#进行训练
print("Predict (before training)",4,forward(4))
w_list = []
cost_list =[]
for epo