little-ProMonkey

机器学习

梯度下降法

参考链接

基本概念

梯度下降法(gradient descent),又名最速下降法(steepest descent)是求解无约束最优化问题最常用的方法,它是一种迭代方法,每一步主要的操作是求解目标函数的梯度向量,将当前位置的负梯度方向作为搜索方向(因为在该方向上目标函数下降最快,这也是最速下降法名称的由来)。

梯度下降,其实就是一个公式:1594463588290

公式推导

![img](file:///E:\qq聊天记录\1713176942\Image\C2C\2D65DC80FB67F846BD86FE94D6FF8215.jpg)

基本梯度下降步骤

步骤1594380571542

η为学习率,ε为收敛条件。梯度下降法属于机器学习,本质为:不断迭代判断是否满足条件,会用到循环语句。

st=>start: 首先设定一个较小的正数m,n;
op=>operation: 求当前位置处的各个偏导数;
修改当前函数的参数值;
cond=>condition: 参数变化量小于n
sub1=>subroutine: 回退迭代
io=>inputoutput: 求得极小值
e=>end: 结束框
st->op->cond
cond(yes)->io->e
cond(no)->sub1(right)->op

1594382365355

1594382407187

批量梯度下降(BGD)

Batch gradient descent::批量梯度下降算法(BGD),其需要计算整个训练集的梯度,即:

bgd.png

其中η为学习率,用来控制更新的“力度”/"步长"。

  • 优点:

对于凸目标函数,可以保证全局最优; 对于非凸目标函数,可以保证一个局部最优。

  • 缺点:

速度慢; 数据量大时不可行; 无法在线优化(即无法处理动态产生的新样本)。

代码实现

#引库
#引入matplotlib库,用于画图
import matplotlib.pyplot as plt
from math import pow
#图片嵌入jupyter
#matplotlib inline

#为了便于取用数据,我们将数据分为x,y,在直角坐标系中(x,y)是点
x = [1,2,3,4,5,6]
y = [13,14,20,21,25,30]
print("打印初始数据图...")
plt.scatter(x,y)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

#超参数设定
alpha = 0.01#学习率/步长
theta0 = 0#θ0
theta1 = 0#θ1
epsilon = 0.001#误差
m = len(x)

count = 0
loss = []

for time in range(1000):
    count += 1
    #求偏导theta0和theta1的结果
    temp0 = 0#J(θ)对θ0求导的结果
    temp1 = 0#J(θ)对θ1求导的结果
    diss = 0
    for i in range(m):
        temp0 += (theta0+theta1*x[i]-y[i])/m
        temp1 += ((theta0+theta1*x[i]-y[i])/m)*x[i]

    #更新theta0和theta1
    for i in range(m):
        theta0 = theta0 - alpha*((theta0+theta1*x[i]-y[i])/m) 
        theta1 = theta1 - alpha*((theta0+theta1*x[i]-y[i])/m)*x[i]

    #求损失函数J(θ)
    for i in range(m):
        diss = diss + 0.5*(1/m)*pow((theta0+theta1*x[i]-y[i]),2)
    loss.append(diss)

    #看是否满足条件
    '''
    if diss<=epsilon:
        break
    else:
        continue
    '''
print("最终的结果为:")
print("此次迭代次数为:{}次,最终theta0的结果为:{},最终theta1的结果为:{}".format(count,theta0,theta1))
print("预测的最终回归函数为:y={}+{}x\n".format(theta0,theta1))
print("迭代图像绘制...")
plt.scatter(range(count),loss)
plt.show()

运行结果

1594475742309

随机梯度下降(SGD)

Stochastic gradient descent:随机梯度下降算法(SGD),仅计算某个样本的梯度,即针对某一个训练样本 xi及其label yi更新参数:

sgd.png

逐步减小学习率,SGD表现得同BGD很相似,最后都可以有不错的收敛。

  • 优点:

更新频次快,优化速度更快; 可以在线优化(可以无法处理动态产生的新样本);一定的随机性导致有几率跳出局部最优(随机性来自于用一个样本的梯度去代替整体样本的梯度)。

  • 缺点:

随机性可能导致收敛复杂化,即使到达最优点仍然会进行过度优化,因此SGD得优化过程相比BGD充满动荡。

代码实现

#引库
#引入matplotlib库,用于画图
import matplotlib.pyplot as plt
from math import pow
import numpy as np
#图片嵌入jupyter
#matplotlib inline

#为了便于取用数据,我们将数据分为x,y,在直角坐标系中(x,y)是点
x = [1,2,3,4,5,6]
y = [13,14,20,21,25,30]
print("打印初始数据图...")
plt.scatter(x,y)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

#超参数设定
alpha = 0.01#学习率/步长
theta0 = 0#θ0
theta1 = 0#θ1
epsilon = 0.001#误差
m = len(x)

count = 0
loss = []

for time in range(1000):
    count += 1
    diss = 0
    #求偏导theta0和theta1的结果
    temp0 = 0#J(θ)对θ0求导的结果
    temp1 = 0#J(θ)对θ1求导的结果
    for i in range(m):
        temp0 += (theta0+theta1*x[i]-y[i])/m
        temp1 += ((theta0+theta1*x[i]-y[i])/m)*x[i]

    #更新theta0和theta1
    for i in range(m):
        theta0 = theta0 - alpha*((theta0+theta1*x[i]-y[i])/m) 
        theta1 = theta1 - alpha*((theta0+theta1*x[i]-y[i])/m)*x[i]

    #求损失函数J(θ)
    rand_i = np.random.randint(0,m)
    diss += 0.5*(1/m)*pow((theta0+theta1*x[rand_i]-y[rand_i]),2)
    loss.append(diss)

    #看是否满足条件
    '''
    if diss<=epsilon:
        break
    else:
        continue
    '''
print("最终的结果为:")
print("此次迭代次数为:{}次,最终theta0的结果为:{},最终theta1的结果为:{}".format(count,theta0,theta1))
print("预测的最终回归函数为:y={}+{}x\n".format(theta0,theta1))
print("迭代图像绘制...")
plt.scatter(range(count),loss)
plt.show()

运行结果

1594476028430

小批量梯度下降(MBGD)

Mini-batch gradient descent:小批量梯度下降算法(MBGD),计算包含n个样本的mini-batch的梯度

mbgd.png

MBGD是训练神经网络最常用的优化方法。

  • 优点:

参数更新时的动荡变小,收敛过程更稳定,降低收敛难度;可以利用现有的线性代数库高效的计算多个样本的梯度。

代码实现

#引库
#引入matplotlib库,用于画图
import matplotlib.pyplot as plt
from math import pow
import numpy as np
#图片嵌入jupyter
#matplotlib inline

#为了便于取用数据,我们将数据分为x,y,在直角坐标系中(x,y)是点
x = [1,2,3,4,5,6]
y = [13,14,20,21,25,30]
print("打印初始数据图...")
plt.scatter(x,y)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

#超参数设定
alpha = 0.01#学习率/步长
theta0 = 0#θ0
theta1 = 0#θ1
epsilon = 0.001#误差
diss = 0#损失函数
m = len(x)

count = 0
loss = []

for time in range(1000):
    count += 1
    diss = 0
    #求偏导theta0和theta1的结果
    temp0 = 0#J(θ)对θ0求导的结果
    temp1 = 0#J(θ)对θ1求导的结果
    for i in range(m):
        temp0 += (theta0+theta1*x[i]-y[i])/m
        temp1 += ((theta0+theta1*x[i]-y[i])/m)*x[i]

    #更新theta0和theta1
    for i in range(m):
        theta0 = theta0 - alpha*((theta0+theta1*x[i]-y[i])/m) 
        theta1 = theta1 - alpha*((theta0+theta1*x[i]-y[i])/m)*x[i]

    #求损失函数J(θ)
    result = []
    for i in range(3):
        rand_i = np.random.randint(0,m)
        result.append(rand_i)
    for j in result:
        diss += 0.5*(1/m)*pow((theta0+theta1*x[j]-y[j]),2)
    loss.append(diss)

    #看是否满足条件
    '''
    if diss<=epsilon:
        break
    else:
        continue
    '''
print("最终的结果为:")
print("此次迭代次数为:{}次,最终theta0的结果为:{},最终theta1的结果为:{}".format(count,theta0,theta1))
print("预测的最终回归函数为:y={}+{}x\n".format(theta0,theta1))
print("迭代图像绘制...")
plt.scatter(range(count),loss)
plt.show()

运行结果

1594476309210

相关文章: