本来是想做一个特征缩放的,然鹅发现我那种归一化缩放反而让训练速度变慢了。。先搞一个二元的。
if __name__ == '__main__':
X1 =[12.46, 0.25, 5.22, 11.3, 6.81, 4.59, 0.66, 14.53, 15.49, 14.43,
2.19, 1.35, 10.02, 12.93, 5.93, 2.92, 12.81, 4.88, 13.11, 5.8,
29.01, 4.7, 22.33, 24.99, 18.85, 14.89, 10.58, 36.84, 42.36, 39.73,
11.92, 7.45, 22.9, 36.62, 16.04, 16.56, 31.55, 20.04, 35.26, 23.59]
X2 =[29.01, 4.7, 22.33, 24.99, 18.85, 14.89, 10.58, 36.84, 42.36, 39.73,
11.92, 7.45, 22.9, 36.62, 16.04, 16.56, 31.55, 20.04, 35.26, 23.59,
12.46, 0.25, 5.22, 11.3, 6.81, 4.59, 0.66, 14.53, 15.49, 14.43,
2.19, 1.35, 10.02, 12.93, 5.93, 2.92, 12.81, 4.88, 13.11, 5.8]
Y= []
for i in range(len(X1)):
Y.append(2 * X1[i] +3*X2[i]+ 5)
# for i in range(1):
X1.append(4.5)
X2.append(10)
Y.append(40)
#特征缩放
X1_train=[]
X2_train=[]
for i in range(len(X1)):
X1_train.append(X1[i]/(max(X1)-min(X1)))
X2_train.append(X2[i] / (max(X2) - min(X2)))
w1=1
w2=-1
b=2
a=0.001 # 学习率
w1_temp=-100
w2_temp = -100
b_temp=-100
w1change = 100
w2change = 100
bchange = 100
while abs(w1change)>1e-12 and abs(w2change)>1e-12 and abs(bchange)>1e-12:
print(w1change)
w1change=0
w2change=0
bchange=0
for i in range(len(X1)):
w1change+=(w1*X1[i]+w2*X2[i]+b-Y[i])*X1[i]
w2change += (w1 * X1[i] + w2 * X2[i] + b - Y[i]) * X2[i]
bchange+=w1*X1[i]+ w2 * X2[i]+b-Y[i]
w1change/=len(X1)
w2change /= len(X2)
bchange /= len(X1)
w1_temp=w1-a*w1change
w2_temp = w2 - a * w2change
b_temp=b-a*bchange
w1=w1_temp
w2 = w2_temp
b=b_temp
print("y=%.4f*x1+%.4f*x2+%.4f" % (w1,w2, b))
加入了一点噪声,训练效果还不错。