1.梯度下降
梯度下降算法:每次迭代都朝梯度下降最快的方向走。
梯度优化:找使目标损失函数最小的权重组合的任务(不一定得到最优,只能得到局部最优)。
权重更新的推导:
示例代码:
import numpy as np
import matplotlib.pyplot as plt
w = 1.0
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
def forward(x):
'求预测值:y_hat = x * w'
return x * w
def cost(xs, ys):
'输入:x_data,y_data;输出:损失cost = (y_hat - y)的平方累加再求均值'
cost_val=0
for x,y in zip(xs,ys):
y_pred = forw