一、随机梯度下降(SGD)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
##### 产生数据集 #####
x = np.arange(0,50)
np.random.seed(1)
#添加50个随机噪声,范围[-5,5]
random_array=(np.random.random(50)*2-1)*5
y = 2 * x + random_array
#转换二维数组
x=x.reshape(50,1)
y=y.reshape(50,1)
data=np.concatenate((x,y),axis=1)
plt.scatter(x,y)
plt.show()
def ML():
##### 机器学习模块调用 #####
# 训练模型
lr = LinearRegression()
lr.fit(x, y)
# 绘制数据散点图和拟合直线
plt.scatter(x, y)
plt.plot(x, lr.predict(x), color='red', linewidth=3)
plt.show()
def SGD():
NUM=1 #标记
N = 1000 #最大循环次数
epsilon = 190 #误差阈值
theta = np.random.rand() # 线性模型的系数
learning_rate = 0.001 # 学习率
#x = np.hstack([np.ones((50,1)),x]) # 增加全1列
np.random.shuffle(data)
#训练集与测试集
train_data=data[:40]
#存放数据列表
theta_list=[]
loss_list=[]
while True:
np.random.shuffle(train_data)
for n in range(N):
#取随机样本
randint = np.random.randint(0,40)
temp_x = train_data[randint][0]
temp_y = train_data[randint][1]
#计算梯度
grad = temp_x * (temp_x * theta - temp_y)
theta = theta - learning_rate*grad
x=train_data[:,0]
y=train_data[:,1]
loss=np.sum(0.5*(theta*x-y)**2)
theta_list.append(theta)
loss_list.append(loss)
NUM=NUM+1
print("num: %d ,theta: %s ,loss: %s "%(NUM,theta,loss))
if loss<epsilon:
break
plt.scatter(x,y)
plt.plot(x,x*theta,color='red',linewidth=3)
plt.show()
if __name__=="__main__":
SGD()
#BGD()
#MBGD()
|
图1 随机梯度下降(SGD)图 |
批量梯度下降(BGD)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
##### 产生数据集 #####
x = np.arange(0,50)
np.random.seed(1)
#添加50个随机噪声,范围[-5,5]
random_array=(np.random.random(50)*2-1)*5
y = 2 * x + random_array
#转换二维数组
x=x.reshape(50,1)
y=y.reshape(50,1)
data=np.concatenate((x,y),axis=1)
plt.scatter(x,y)
plt.show()
def ML():
##### 机器学习模块调用 #####
# 训练模型
lr = LinearRegression()
lr.fit(x, y)
# 绘制数据散点图和拟合直线
plt.scatter(x, y)
plt.plot(x, lr.predict(x), color='red', linewidth=3)
plt.show()
def BGD():
NUM = 1 # 标记
N = 100 # 最大循环次数
theta = np.random.rand() # 线性模型的系数
learning_rate = 0.001 # 学习率
for i in range(N):
pred_y = np.dot(x,theta)
loss = pred_y - y
# 计算梯度
grad = np.dot(x.T,loss)/x.shape[0]
# 更新theta
theta = theta - learning_rate*grad
sum_loss=np.sum(pred_y - y)
NUM = NUM + 1
# 输出当前的更新次数和误差
print("num: %d ,theta: %s ,sum_loss: %s " % (NUM, theta, sum_loss))
# 绘制拟合的曲线
plt.scatter(x,y)
plt.plot(x,x*theta,color='green',linewidth=3)
plt.show()
if __name__=="__main__":
#SGD()
BGD()
#MBGD()
|
图2 批量梯度下降(BGD)图 |
小批量梯度下降(MBGD)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
##### 产生数据集 #####
x = np.arange(0,50)
np.random.seed(1)
#添加50个随机噪声,范围[-5,5]
random_array=(np.random.random(50)*2-1)*5
y = 2 * x + random_array
#转换二维数组
x=x.reshape(50,1)
y=y.reshape(50,1)
data=np.concatenate((x,y),axis=1)
plt.scatter(x,y)
plt.show()
def ML():
##### 机器学习模块调用 #####
# 训练模型
lr = LinearRegression()
lr.fit(x, y)
# 绘制数据散点图和拟合直线
plt.scatter(x, y)
plt.plot(x, lr.predict(x), color='red', linewidth=3)
plt.show()
def MBGD():
N = 200 # 最大循环次数
theta = np.random.rand() # 线性模型的系数
learning_rate = 0.001 # 学习率
epsilon = 20 # 误差阈值
# 还原参数theta,其他参数复用梯度下降
#theta = np.random.rand(2,1)
# 指定每次更新使用的数据量
batch_size = 20
for i in range(N):
# 随机样本的列索引
indexs = np.random.randint(0, x.shape[0], size=batch_size)
# 随机样本
temp_x = x.take(indexs, axis=0)
temp_y = y.take(indexs, axis=0)
# 计算梯度
grad = np.dot(temp_x.T, (np.dot(temp_x, theta) - temp_y)) / temp_x.shape[0]
# 更新theta
theta = theta - learning_rate * grad
# 计算更新后的误差
error = np.linalg.norm(np.dot(x, theta) - y)
# 输出当前的更新次数和误差
print("number is %d. error is %f"%(i,error))
# 误差小于阈值时退出循环
print("thema: %s"%(theta))
if error < epsilon:
break
# 绘制拟合的曲线
plt.scatter(x, y)
plt.plot(x, np.dot(x, theta), color='red', linewidth=3)
plt.show()
if __name__=="__main__":
#SGD()
#BGD()
MBGD()
|
图3 小批量梯度下降(MBGD)图 |