1 梯度下降
import numpy as np
import matplotlib.pyplot as plt
x=np.array(range(0,50))
np.random.seed(1)
y=6*x
label_y=y+np.random.random(50)*10
x=x.reshape(50,1)
label_y=label_y.reshape(50,1)
train_data=np.concatenate((x,label_y),axis=1)
plt.scatter(train_data[:,0],train_data[:,1])
plt.show()
lr=0.0001
e=15
theax=1.0
tx=train_data[:,0]
ty=train_data[:,1]
J=[]
while True:
g=0.02*np.sum((tx*theax-ty)*tx)
theax=theax-lr*g
loss=0.01*np.sum((theax*tx-ty)**2)
J.append(loss)
print(theax)
if loss<e:
break
plt.plot(J)
plt.show()
2 小批量随机梯度下降
import numpy as np
import matplotlib.pyplot as plt
x=np.array(range(0,50))
np.random.seed(1)
y=7*x
label_y=y+np.random.random(50)*15
x=x.reshape(50,1)
label_y=label_y.reshape(50,1)
train_data=np.concatenate((x,label_y),axis=1)
plt.scatter(train_data[:,0],train_data[:,1])
plt.show()
np.random.shuffle(train_data)
train_data=train_data[0:30]
theax=1.0
lr=0.0001
e=15
J=[]
tx=train_data[:,0]
ty=train_data[:,1]
J=[]
while True:
g=0.02*np.sum((tx*theax-ty)*tx)
theax=theax-lr*g
loss=0.01*np.sum((theax*tx-ty)**2)
J.append(loss)
print(theax)
if loss<e:
break
plt.plot(J)
plt.show()