Machine Learning —— Gradient Descent

本文深入探讨了梯度下降法在机器学习中的应用,包括其基本概念、可视化表示、学习率的影响、Adagrad算法以及随机梯度下降法。讨论了特征缩放的重要性,并分析了梯度下降的理论基础——泰勒级数。同时,指出了梯度下降可能遇到的局限,如局部最小值和鞍点问题。

Machine Learning —— Gradient Descent

1、Review

前面预测的pokemon的CP值例子中已经初步介绍了Gradient Descent的用法:
In step3, we have to solve the following optimization problem:
θ ∗ = arg ⁡ m i n f L ( θ ) \theta^*=\operatorname{arg}\mathop{min}\limits_fL(\theta) θ=argfminL(θ)
L : l o s s     f u n c t i n L:loss\,\ functin L:loss functin
θ j i :   p a r a m e t e r \theta_j^i:\,parameter θji:parameter(第i组参数的第j个参数)
Suppose that θ \theta θ has two variables{ θ 1 \theta_1 θ1, θ 2 \theta_2 θ2}
Randomly start at
θ 0 = [ θ 1 0 θ 2 0 ] \theta^0= \begin{bmatrix} \theta^0_1 \\ \theta^0_2 \\ \end{bmatrix} θ0=[θ10θ20]
计算 θ \theta θ处的梯度gradient:
∇ L = [ ∂ L ( θ 1 ) ∂ θ 1 ∂ L ( θ 2 ) ∂ θ 2 ] [ θ 1 1 θ 2 1 ] = [ θ 1 0 θ 2 0 ] − η [ ∂ L ( θ 1 0 ) ∂ θ 1 ∂ L ( θ 2 0 ) ∂ θ 2 ] ⟶ θ 1 = θ 0 − η ∇ L ( θ 0 ) [ θ 1 2 θ 2 2 ] = [ θ 1 1 θ 2 1 ] − η [ ∂ L ( θ 1 1 ) ∂ θ 1 ∂ L ( θ 2 1 ) ∂ θ 2 ] ⟶ θ 2 = θ 1 − η ∇ L ( θ 1 ) \nabla L= \begin{bmatrix} \frac{\partial {L(\theta_1)}}{\partial\theta_1}\\ \frac{\partial {L(\theta_2)}}{\partial\theta_2}\\ \end{bmatrix}\\ \begin{bmatrix} \theta^1_1 \\ \theta^1_2 \\ \end{bmatrix} = \begin{bmatrix} \theta^0_1 \\ \theta^0_2 \\ \end{bmatrix}-\eta \begin{bmatrix} \frac{\partial {L(\theta_1^0)}}{\partial\theta_1}\\ \frac{\partial {L(\theta_2^0)}}{\partial\theta_2}\\ \end{bmatrix}\longrightarrow \theta^1=\theta^0-\eta\nabla L(\theta^0)\\ \begin{bmatrix} \theta^2_1 \\ \theta^2_2 \\ \end{bmatrix} = \begin{bmatrix} \theta^1_1 \\ \theta^1_2 \\ \end{bmatrix}-\eta \begin{bmatrix} \frac{\partial {L(\theta_1^1)}}{\partial\theta_1}\\ \frac{\partial {L(\theta_2^1)}}{\partial\theta_2}\\ \end{bmatrix}\longrightarrow \theta^2=\theta^1-\eta\nabla L(\theta^1) L=[θ1L(θ1)θ2L(θ2)][θ11θ21]=[θ10θ20]η[θ1L(θ10)θ2L(θ20)]θ1=θ0ηL(θ0)[θ12θ22]=[θ11θ21]η[θ1L(θ11)θ2L(θ21)]θ2=θ1ηL(θ1)

2、Visualization

下图将gradient descent投影到二维坐标系中可视化的样子,图上每一个点都是( θ 1 , θ 2 , l o s s \theta_1,\theta_2,loss θ1,θ2,loss)在该平面的投影
在这里插入图片描述

  • 红色箭头是指在( θ 1 , θ 2 \theta_1,\theta_2 θ1,θ2)点处的梯度,梯度方向即箭头方向,从低处指向高处,梯度大小即箭头长度(表示 θ i \theta^i θi 点处最陡的那条切线的导数大小,该方向也是梯度上升最快的方向
  • 蓝色箭头代表实际情况下参数 θ 1 \theta_1 θ1 θ 2 \theta_2 θ2的更新过程图,每次更新沿着蓝色箭头方向loss会减小,蓝色箭头方向与红色箭头方向刚好相反,代表梯度下降的方向

因此在整个gradient descent过程中,梯度不一定是递减的(红色箭头的长度不一),但是沿着梯度下降的方向,loss一定是递减的,且在gradient=0时,loss下降到了局部最小值,梯度下降法是指loss函数随梯度下降的方向减小

  • 初始随机在三维坐标系中取一点( θ 1 , θ 2 , l o s s \theta_1,\theta_2,loss θ1,θ2,loss),我们的目标是找到最小的loss,也就是三维坐标系中最低点,而gradient梯度是图像上高度上升最快的方向,于是其反方向就是下降最快的方向
  • 每次update沿着梯度反方向,update的步长由梯度大小和learning rate共同决定,一直迭代,当某次update完成后,该点的gradient=0时,说明达到了局部最小值

3、Learning rate的问题

gradient descent过程中,影响结果的一个关键因素就是learning rate的大小

  • 如果learning rate合适,则可以顺利地达到loss的最大值(红色)
  • 如果learning rate太小,虽然最终能够走到local minimal的位置,但是会非常非常慢(蓝色)
  • 如果learning rate太大,每次迭代的步伐太大,就会永远无法走下去(绿色)
  • 如果learning rate非常大,可能会飞出,在update后,loss反而会越来越大(黄色)
    在这里插入图片描述
    当参数个数>3时,很难将loss随每个参数的变化可视化出来,但是我们可以把update的次数作为唯一的参数,将loss随着update的增加而变化的趋势可视化出来(上面右图所示)

Adaptive Learning rate

使用一些自动调整learning rate的方法,最基本的原则是让learning rate随update的次数增加而减小
因为在起始时,通常是离最低点比较远的,这时候步伐应该大一些;经过几次update后,比较接近目标,应该减小learning rate,使其能够收敛在最低点的位置

3、Adagrad

Adagrad是将不同参数的learning rate分开考虑的一种算法(adagrad算法会随迭代次数增加而变慢,这只是adaptive算法中最简单的一种)
在这里插入图片描述
图中w是function的某个参数,t是update的次数, g t g^t gt表示Loss对w的偏微分,而 σ t \sigma^t σt是之前所有loss对w偏微分的方均根(平方的均值开根号),这个值对每一个参数都是不一样的
在这里插入图片描述
η t \eta^t ηt σ t \sigma^t σt有相同的因子,最终得到adagrad的表达式:
w t + 1 = w t − η ∑ i = 0 t ( g i ) 2 g t w^{t+1}=w^t-\frac{\eta}{\sum_{i=0}^t(g^i)^2}g^t wt+1=wti=0t(gi)2ηgt

Adagrad算法中的contradiction

在这里插入图片描述

  • 一些paper中的解释:Adagrad考虑的是,gradient有多surpise,即反差有多大,假设t = 4时 g 4 g^4 g4与之前的gradient反差特别大那么 g t g^t gt 1 t + 1 ∑ i = 0 t ( g i ) 2 \sqrt{\frac{1}{t+1}\sum_{i=0}^t(g^i)^2} t+11i=0t(gi)2 之间的大小反差就好比较大,这两者的商可以将反差效果表现出来
    在这里插入图片描述
    gradient越大,离最低点越远这件事在多参数的情况下不一定成立
    如下图所示,w1和w2分别是loss function的两个参数,loss的值投影到该平面中以颜色深度表示大小,分别在w1和w2处垂直切一刀(在这条直线上只有一个变量的gradient变化),对应的情况为右边的两条曲线,相比于a点,c点距离最低点更近,但c处的gradient却更大
    在这里插入图片描述
    实际上,对于一个二次函数 y = a x 2 + b x + c y=ax^2+bx+c y=ax2+bx+c来说,最小值点在 x = − b 2 a x=-\frac{b}{2a} x=2ab,而对于任意一点 x 0 x_0 x0处迈出best step= ∣ x 0 + b 2 a ∣ = ∣ 2 a x 0 + b 2 a ∣ |x_0+\frac{b}{2a}|=|\frac{2ax_0+b}{2a}| x0+2ab=2a2ax0+b,联系到该函数的一阶、二阶导数 y ′ = 2 a x + b y' =2ax+b y=2ax+b y ′ ′ = 2 a y''=2a y=2a,可以得到best step is ∣ y ′ y ′ ′ ∣ |\frac{y'}{y''}| yy,也就是说最合适的步长不仅和一阶导数有关还和二阶导数有关
  • Adagrad的表达式: w t + 1 = w t − η ∑ i = 0 t ( g i ) 2 g t w^{t+1}=w^t-\frac{\eta}{\sum_{i=0}^t(g^i)^2}g^t wt+1=wti=0t(gi)2ηgt g t g^t gt是一次微分,分母中 ∑ i = 0 t ( g i ) 2 \sum_{i=0}^t(g^i)^2 i=0t(gi)2反映二次微分的大小,所以Adagrad的作用就是在不额外加运算的前提下估计二次微分的值
    在这里插入图片描述

4、Stochastic Gradient Descent(SGD)

随机梯度下降法可以让训练更加快速,传统的gradient descent的思路是综合所有的样本点后再构建loss function,再update参数;而SGD的做法是看到一个样本点就update一次,因此它的loss function是随机样本点的error平方
在这里插入图片描述
SGD与传统gradient descent的对比效果:
在这里插入图片描述

5、Feature Scaling

特征缩放,当多个特征的分布范围不一样时,最好将这些不同的feature的范围缩放成一样的
在这里插入图片描述

  • y = b + w 1 x 1 + w 2 x 2 y = b+w_1x_1+w_2x_2 y=b+w1x1+w2x2,假设 x 1 x_1 x1的范围很小(1,2,3,…),而 x 2 x_2 x2的范围很大(100,200,300,…),此时若是对 w 1 w_1 w1 w 2 w_2 w2都做相同的 △ w \vartriangle w w,结果是 w 1 w_1 w1的变化对 y y y的影响较小,而 w 2 w_2 w2的变化对y影响较大
    在这里插入图片描述
    如上图所示,相比于左图,右图中的 x 1 x_1 x1 x 2 x_2 x2的scale相近,loss的图像跟接近于圆形

feature scaling对gradient descent的帮助

gradient descent的每次update并不都是想着最低点走的,每次update的方向是顺着等高线的方向(梯度下降方向),而不是直接走向最低;但当经过scaling后,loss的投影更像一个圆,不管在这个区域的哪一个点,都是向着圆心走,因此feature scaling对参数的update是有帮助的

如何做feature scaling

假设有R个example(上标i表示第i个样本点), x 1 , x 2 , x 3 , . . . , x r , x R x^1,x^2,x^3,...,x^r,x^R x1,x2,x3,...,xr,xR,每一笔example都有一组feature(小标j表示第j个特征)

  • 对每一个维度的i,都去计算它的平均值 m i m_i mi和标准差 σ i \sigma_i σi
  • 将每一个参数都归一化成标准正太分布 x i r ⟹ x i r − m i σ i x^r_i \Longrightarrow \frac{x_i^r-m_i}{\sigma_i} xirσixirmi
    在这里插入图片描述

6、Gradient Descent的理论基础

Taylor Series

泰勒表达式: h ( x ) = ∑ k = 0 ∞ h ( k ) ( x 0 ) k ! ( x − x 0 ) k h(x)=\sum_{k=0}^{\infty}\frac{h^{(k)}(x_0)}{k!}(x-x_0)^k h(x)=k=0k!h(k)(x0)(xx0)k
when x is close to x 0 x_0 x0 h ( x ) ≈ h ( x 0 ) + h ′ ( x 0 ) ( x − x 0 ) h(x)\approx h(x_0)+h'(x_0)(x-x_0) h(x)h(x0)+h(x0)(xx0)

对于二元函数
h ( x , y ) ≈ h ( x 0 , y 0 ) + ∂ h ( x 0 , y 0 ) ∂ x ( x − x 0 ) + ∂ h ( x 0 , y 0 ) ∂ y ( y − y 0 ) h(x,y)\approx h(x_0,y_0)+\frac{\partial h(x_0,y_0)}{\partial x}(x-x_0)+\frac{\partial h(x_0,y_0)}{\partial y}(y-y_0) h(x,y)h(x0,y0)+xh(x0,y0)(xx0)+yh(x0,y0)(yy0)

从泰勒级数推导出Gradient descent

对于loss图像上的某一点(a,b),如果我们要找到这个点附近loss最小的值,可以用泰勒展开的思想
在这里插入图片描述
假设用一个red circle限定点的范围,这个圆足够小以满足泰勒展开的精度,此时可以将loss function简化为:
L ( θ ) ≈ L ( a , b ) + ∂ L ( a , b ) ∂ θ 1 ( θ 1 − a ) + ∂ L ( a , b ) ∂ θ 2 ( θ 2 − b ) L(\theta)\approx L(a,b)+\frac{\partial L(a,b)}{\partial \theta_1}(\theta_1-a)+\frac{\partial L(a,b)}{\partial \theta_2}(\theta_2-b) L(θ)L(a,b)+θ1L(a,b)(θ1a)+θ2L(a,b)(θ2b)

  • s = L ( a , b ) , u = ∂ L ( a , b ) ∂ θ 1 , v = ∂ L ( a , b ) ∂ θ 2 s=L(a,b), u=\frac{\partial L(a,b)}{\partial \theta_1}, v=\frac{\partial L(a,b)}{\partial \theta_2} s=L(a,b),u=θ1L(a,b),v=θ2L(a,b)
  • 则有 L ( θ ) ≈ L ( a , b ) + u ( θ 1 − a ) + v ( θ 2 − b ) L(\theta)\approx L(a,b)+u(\theta_1-a)+v(\theta_2-b) L(θ)L(a,b)+u(θ1a)+v(θ2b)

假定red circle的半径为d,则有限制条件: ( θ 1 − a ) 2 + ( θ 2 − b ) 2 ≤ d 2 (\theta_1-a)^2+(\theta_2-b)^2\leq d^2 (θ1a)2+(θ2b)2d2
此时取 L ( θ ) m i n L(\theta)_{min} L(θ)min,将 L ( θ ) L(\theta) L(θ)转换为两个向量的乘积:

u ( θ 1 − a ) + v ( θ 2 − b ) = ( u , v ) ( θ 1 − a , θ 2 − b ) = ( u , v ) ( Δ θ 1 , Δ θ 2 ) u(\theta_1-a)+v(\theta_2-b)=(u,v)(\theta_1-a,\theta_2-b)=(u,v)(\Delta\theta_1,\Delta\theta_2) u(θ1a)+v(θ2b)=(u,v)(θ1a,θ2b)=(u,v)(Δθ1,Δθ2)

由观察图形可知,当调整向量 ( Δ θ 1 , Δ θ 2 ) (\Delta\theta_1,\Delta\theta_2) (Δθ1,Δθ2)与向量 ( u , v ) (u,v) (u,v)反向,且刚好是red circle的半径长度时有 L ( θ ) m i n L(\theta)_{min} L(θ)min
[ Δ θ 1 Δ θ 2 ] = − η [ u v ] [ θ 1 θ 2 ] = [ a b ] − η [ u v ] = [ a b ] − η [ ∂ L ( a , b ) ∂ θ 1 ∂ L ( a , b ) ∂ θ 2 ] \begin{bmatrix} \Delta\theta_1 \\ \Delta\theta_2 \\ \end{bmatrix}=-\eta \begin{bmatrix} u\\ v \\ \end{bmatrix} \quad \\ \begin{bmatrix} \theta_1\\ \theta_2 \\ \end{bmatrix}= \begin{bmatrix} a\\ b \\ \end{bmatrix}-\eta \begin{bmatrix} u\\ v \\ \end{bmatrix}=\begin{bmatrix} a\\ b \\ \end{bmatrix}-\eta \begin{bmatrix} \frac{\partial L(a,b)}{\partial \theta_1}\\ \frac{\partial L(a,b)}{\partial \theta_2}\\ \end{bmatrix} [Δθ1Δθ2]=η[uv][θ1θ2]=[ab]η[uv]=[ab]η[θ1L(a,b)θ2L(a,b)]
在这里插入图片描述
在这里插入图片描述

注意:给定的red circle要足够小,这样泰勒展开的近似才会更精确,而 η \eta η的值与圆的半径是成正比的,理论上learning rate要无穷小才能保证每次gradient descent在update后loss越来越小。虽然泰勒可以使用更高级展开但是会使得运算量大大增加,降低运行效率。

7、Gradient Descent的限制

Gradient即微分值在接近于0的地方就会停下来,而这个地方不一定是global minima,可能是local minima,也可能是saddle point(鞍点),甚至可能是一个loss很高的plateau(平缓高原)
在这里插入图片描述
参考文章
https://github.com/Sakura-gh/ML-notes

The stochastic gradient descent (SGD) algorithm is a popular optimization algorithm used in machine learning. It is an iterative algorithm that updates the model parameters in small steps based on the gradient of the loss function with respect to the parameters. The algorithm works as follows: 1. Initialize the model parameters randomly. 2. Set the learning rate, which determines the step size of the updates. 3. For each training example: - Compute the gradient of the loss function with respect to the parameters using the current example. - Update the model parameters by subtracting the gradient multiplied by the learning rate. The key difference between SGD and regular gradient descent is that in SGD, the gradient is computed and the parameters are updated for each training example, rather than for the entire training set. This makes the algorithm faster and more scalable for large datasets. The stochastic aspect of the algorithm comes from the fact that the training examples are sampled randomly from the training set, rather than being processed in a fixed order. This randomness can help the algorithm escape from local minima and find better solutions. Here is the pseudocode for the SGD algorithm: ``` Input: Training set (X, Y), learning rate α, number of iterations T Output: Model parameters θ Initialize θ randomly for t = 1 to T do Sample a training example (x, y) from (X, Y) randomly Compute the gradient ∇θ L(θ; x, y) using the current example Update the parameters: θ ← θ - α * ∇θ L(θ; x, y) end for return θ ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值