import numpy as np
import matplotlib.pyplot as plt
a=0.01
LEN = 100
X = np.arange(0, LEN)
# rand从-5到+5
np.random.seed(1)
rand = (np.random.random(LEN) * 2 - 1) * 5
Y = X * 3 + rand
# X、Y分布如下图所示
# X、Y 连接
X = X.reshape(LEN, 1)
Y = Y.reshape(LEN, 1)
allData = np.concatenate((X, Y), axis = 1)
np.random.shuffle(allData)
# 训练集:测试机 = 4:1
ratio = 0.8
index = (int)(allData.shape[0] * ratio)
trainData = allData[:index]
testData = allData[index:]
# 学习率
lr = 0.00005
# 训练集大小(每个batch随机梯度下降迭代次数)
N = trainData.shape[0]
# 待估及参数(theta)
theta = np.random.rand()
grad=0
# 迭代次数标识
iter = 1
epsilon=350
while True:
for i in range(N):
# 随机样本
x = trainData[i, 0]
y = trainData[i, 1]
# 计算梯度
grad = (theta * x - y) * x
# 更新参数
theta = theta - lr * grad
i=0
loss = np.sum(0.5 * (trainData[:, 0] * theta - trainData[:, 1]) ** 2)
print("No.%d:\t grad = %f\t theta: %f\tloss: %f" %(iter, grad, theta, loss))
iter += 1
# 达到允许的误差,结束训练
if loss < epsilon:
print("Traing Completed!")
break
result=testData[:,0]*theta
plt.plot(testData[:,0], result,'bo',label='predict')
plt.plot(testData[:,0],testData[:,1],'ro',label='standard')
plt.legend()
plt.show()