y
=
t
h
e
t
a
0
∗
x
+
t
h
e
t
a
1
y = theta0 *x + theta1
y=theta0∗x+theta1
y
=
2
∗
x
+
96
y = 2 * x + 96
y=2∗x+96
# 构造数据集
X=list(range(1,21))
Y=[98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136]
m = len(X)
def sgd(m, theta0, theta1, X, Y, lr):
grad0, grad1 = 0,0 # 梯度初始化
for i in range(10000) :
for a in range(m):
grad0 += (theta0*X[a] + theta1 -Y[a]) * X[a] #theta0的梯度
grad1 += theta0*X[a] + theta1 -Y[a] #theta1的梯度
theta0 -= lr * grad0 / m # theta0下山
theta1 -= lr * grad1 / m # theta1下山
grad0 = 0 ## 梯度清零!!!这很重要
grad1 = 0
print (theta0, theta1) # 显示迭代后最终的theta0和theta1
lr, theta0, theta1 = 0.012 ,0 ,0 # 参数初始化
sgd(m, theta0, theta1, X, Y, lr)
简单调试后学习率lr大于0.013后出现步幅过大发散现象
梯度一定一定要清零