【李宏毅机器学习】04:梯度下降Gradient Descent

李宏毅机器学习04:梯度下降Gradient Descent

在这里插入图片描述

ML Lecture 3-1 Gradient Descent

一、梯度下降方法

Review: 李宏毅机器学习02:回归Regression

在回归问题的第三步中,需要解决下面的最优化问题,即寻找一组参数 θ \theta θ,使损失函数Loss Function 尽可能的小。

θ ∗ = arg min ⁡ θ L ( θ ) \theta^*=\argmin_\theta L(\theta) θ=argminθL(θ)

  • L L L : 损失函数loss function
  • θ \theta θ : 参数parameters

梯度下降方法如下:

假设θ有两个变量 { θ 1 , θ 2 \theta_1,\theta_2 θ1,θ2 },随机选取初值 θ 0 = [ θ 1 0 θ 2 0 ] \theta^0=\begin{bmatrix} \theta_1^0 \\ \theta_2^0 \end{bmatrix} θ0=[θ10θ20]

损失函数loss function的梯度 ∇ L ( θ ) = [ ∂ L ( θ 1 ) ∂ θ 1 ∂ L ( θ 2 ) ∂ θ 2 ] \nabla L(\theta)=\Large\begin{bmatrix} \frac{\partial L(\theta_1)}{\partial \theta_1} \\ \frac{\partial L(\theta_2)}{\partial \theta_2} \end{bmatrix} L(θ)= θ1L(θ1)θ2L(θ2)

不断地更新参数:

  • [ θ 1 1 θ 2 1 ] = [ θ 1 0 θ 2 0 ] − η [ ∂ L ( θ 1 0 ) ∂ θ 1 ∂ L ( θ 2 0 ) ∂ θ 2 ] \begin{bmatrix} \theta_1^1 \\ \theta_2^1 \end{bmatrix} = \begin{bmatrix} \theta_1^0 \\ \theta_2^0 \end{bmatrix} -\eta\Large\begin{bmatrix} \frac{\partial L(\theta_1^0)}{\partial \theta_1} \\ \frac{\partial L(\theta_2^0)}{\partial \theta_2} \end{bmatrix} [θ11θ21]=[θ10θ20]η θ1L(θ10)θ2L(θ20) ⟹ \large\Longrightarrow θ 1 = θ 0 − η ∇ L ( θ 0 ) \theta^1=\theta^0-\eta\nabla L(\theta^0) θ1=θ0ηL(θ0)
  • [ θ 1 2 θ 2 2 ] = [ θ 1 1 θ 2 1 ] − η [ ∂ L ( θ 1 1 ) ∂ θ 1 ∂ L ( θ 2 1 ) ∂ θ 2 ] \begin{bmatrix} \theta_1^2 \\ \theta_2^2 \end{bmatrix} = \begin{bmatrix} \theta_1^1 \\ \theta_2^1 \end{bmatrix} -\eta\Large\begin{bmatrix} \frac{\partial L(\theta_1^1)}{\partial \theta_1} \\ \frac{\partial L(\theta_2^1)}{\partial \theta_2} \end{bmatrix} [θ12θ22]=[θ11θ21]η θ1L(θ11)θ2L(θ21) ⟹ \large\Longrightarrow θ 2 = θ 1 − η ∇ L ( θ 1 ) \theta^2=\theta^1-\eta\nabla L(\theta^1) θ2=θ1ηL(θ1)
  • … …
    在这里插入图片描述

二、梯度下降的改进方法

Tip 1: Tuning your learning rates 调整学习率

θ i = θ i − 1 − η ∇ L ( θ i − 1 ) \theta^{i}=\theta^{i-1}-\eta\nabla L(\theta^{i-1}) θi=θi1ηL(θi1)
η \eta η is called Learning Rate

1.学习率大小对梯度下降的影响
  • Learning Rate Ver large 步长非常大
    学习率步长太大,会出现损失函数Loss Function不降反增的情况
  • Learning Rate Small 步长小
    学习率步长小,会出现损失函数Loss Function下降速度过慢的情况
  • Learning Rate Just make 步长刚刚好
    学习率大小适当,可以使损失函数Loss Function收敛于最小值
  • Learning Rate Large 步长大
    学习率大可能出现损失函数Loss Function无法到达最低点的情况

通常可以做出参数更新值和损失函数Loss Function的图像来判断学习率的情况
在这里插入图片描述

2.Adaptive Learning Rates 自适应学习率
  • Popular & Simple Idea: Reduce the learning rate by some factor every few epochs
    通俗、简单的思想:随着次数的增加,通过一些因子来减少学习率
    • At the beginning, we are far from the destination, so we use larger learning rate
      通常刚开始,初始点会距离最低点比较远,所以使用大一点的学习率
    • After several epochs, we are close to the destination, so we reduce the learning rate
      经过一段时间后,参数比较靠近最低点了,此时减少学习率
    • E.g. : 1/t decay η t = η t + 1 \eta^t=\Large\frac{\eta}{\sqrt{\smash[b]{t+1}}} ηt=t+1 η
      例如分数减缓1/t decay: η t \eta^t ηt表示第t次的步长,随着次数的增加,步长不断减小。
  • Learning rate cannot be one-size-fits-all
    学习率不能是一个值通用所有特征
    • Giving different parameters different learning rates
      不同的参数需要不同的学习率
3.Adagrad算法
(1)Adagrad 是什么

Divide the learning rate of each parameter by the root mean square of
its previous derivatives 将每个参数的学习速率除以其先前导数的均方根

  • Vanilla Gradient descent
    w t + 1 ← w t − η t g t w^{t+1} \gets w^t - \eta^t g^t wt+1wtηtgt

  • Adagrad
    w t + 1 ← w t − η t σ t g t w^{t+1} \gets w^t - \Large\frac{\eta^t}{\sigma^t} \normalsize g^t wt+1wtσtηtgt

    η t \eta^t ηt, g t g^t gt 分别表示第 t t t 次的学习率和偏微分。
    η t = η t + 1 \eta^t=\Large\frac{\eta}{\sqrt{\smash[b]{t+1}}} ηt=t+1 η g t = ∂ L ( θ t ) ∂ w g^t=\Large\frac{\partial L(\theta^t)}{\partial w} gt=wL(θt)

    • w is one parameters
      w是参数
    • σ t \sigma^t σt: root mean square of the previous derivatives of parameter w,(Parameter dependent)
      σ t \sigma^t σt是之前参数的所有微分的方均根,依赖于参数w
(2)Adagrad 举例
  1. w 1 ← w 0 − η 0 σ 0 g 0 w^1 \gets w^0 - \Large\frac{\eta^0}{\sigma^0}\normalsize g^0 w1w0σ0η0g0 , 其中 σ 0 = ( g 0 ) 2 \sigma^0 =\sqrt{(g^0)^2} σ0=(g0)2

  2. w 2 ← w 1 − η 1 σ 1 g 1 w^2 \gets w^1 -\Large\frac{\eta^1}{\sigma^1}\normalsize g^1 w2w1σ1η1g1 , 其中 σ 1 = 1 2 [ ( g 0 ) 2 + ( g 1 ) 2 ] \sigma^1 =\sqrt{\frac1 2[(g^0)^2+(g^1)^2]} σ1=21[(g0)2+(g1)2]

  3. w 3 ← w 2 − η 2 σ 2 g 2 w^3 \gets w^2 - \Large\frac{\eta^2}{\sigma^2}\normalsize g^2 w3w2σ2η2g2 , 其中 σ 2 = 1 3 [ ( g 0 ) 2 + ( g 1 ) 2 + ( g 2 ) 2 ] \sigma^2 =\sqrt{\frac1 3[(g^0)^2+(g^1)^2+(g^2)^2]} σ2=31[(g0)2+(g1)2+(g2)2]

    . … …

  • w t + 1 ← w t − η t σ t g t w^{t+1} \gets w^t - \Large\frac{\eta^t}{\sigma^t}\normalsize g^t wt+1wtσtηtgt , 其中 σ t = 1 t + 1 ∑ i = 0 t ( g i ) 2 \sigma^t =\sqrt{\frac{1}{t+1}\displaystyle\sum_{i=0}^t(g^i)^2} σt=t+11i=0t(gi)2
(3)Adagrad 理解
  • 对学习率和微分的方均根的比值进行化简
    原式为: w t + 1 ← w t − η t σ t g t w^{t+1} \gets w^t - \Large\frac{\eta^t}{\sigma^t}\normalsize g^t wt+1wtσtηtgt
    其中 : η t = η t + 1 \eta^t=\Large\frac{\eta}{\sqrt{\smash[b]{t+1}}} ηt=t+1 η σ t = 1 t + 1 ∑ i = 0 t ( g i ) 2 = 1 t + 1 ∑ i = 0 t ( g i ) 2 \sigma^t =\sqrt{\frac{1}{t+1}\displaystyle\sum_{i=0}^t(g^i)^2}=\frac{1}{\sqrt{t+1}}\sqrt{\displaystyle\sum_{i=0}^t(g^i)^2} σt=t+11i=0t(gi)2 =t+1 1i=0t(gi)2

    η t \eta^t ηt σ t \sigma^t σt 代入: η t σ t = η t + 1 1 t + 1 ∑ i = 0 t ( g i ) 2 = η ∑ i = 0 t ( g i ) 2 \Large\frac{\eta^t}{\sigma^t}\normalsize=\Large\frac{\frac{\eta}{\sout{\sqrt{\smash[b]{t+1}}}}}{\frac{1}{\sout{\sqrt{t+1}}}\small\sqrt{\displaystyle\sum_{i=0}^t(g^i)^2}}\normalsize=\frac{\large\eta}{\small\sqrt{\displaystyle\sum_{i=0}^t(g^i)^2}} σtηt=t+1 1i=0t(gi)2 t+1 η=i=0t(gi)2 η

    得到: w t + 1 ← w t − η ∑ i = 0 t ( g i ) 2 g t w^{t+1} \gets w^t - \Large\frac{\eta}{\small\sqrt{\displaystyle\sum_{i=0}^t(g^i)^2}}\normalsize g^t wt+1wti=0t(gi)2 ηgt

  • Contradiction ? 出现矛盾

    对比普通梯度下降公式和Adagrad算法
    在这里插入图片描述

    梯度对迭代值的大小影响相反:梯度值在分子上,梯度越大,迭代值的更新就越大;之前梯度的方均根在分母上,梯度越大,迭代值的更新越小。

Ⅰ Intuitive Reason 直观原因

当梯度值变化很大时,方均根可以造成反差的效果
在这里插入图片描述

Ⅱ Mathematical Reason 数学原因
  • 以二次函数 y = a x 2 + b x + c ( a > 0 ) y=ax^2+bx+c (a>0) y=ax2+bx+c(a>0) 为例:

    其最小值位于 x = − b 2 a x=-\large\frac{b}{2a} x=2ab ,

    任取一点 x 0 x_0 x0 , x 0 x_0 x0 到最值点的距离为: ∣ x 0 + b 2 a ∣ |x_0+\large\frac{b}{2a}\normalsize| x0+2ab ,

    Best step = ∣ x 0 + b 2 a ∣ = ∣ 2 a x 0 + b ∣ 2 a =|x_0+\large\frac{b}{2a}\normalsize|=\large\frac{|2ax_0+b|}{2a} =x0+2ab=2a∣2ax0+b

    分子是二次函数 y = a x 2 + b x + c ( a > 0 ) y=ax^2+bx+c (a>0) y=ax2+bx+c(a>0) x 0 x_0 x0点的一阶导数值

    由此可以得出结论:

    Larger 1st order derivative means far from the minima
    较大的一阶导数意味着距离极小值较远

    在这里插入图片描述

  • 然而当考虑多个参数时,结论失效

    例如下图,参数 w 1 w_1 w1 在a点一阶导数的绝对值小于参数 w 2 w_2 w2在c点一阶导数的绝对值,而从图上来看c点反而距离极值点更近。
    在这里插入图片描述

  • 同时考虑 ∣ 2 a x 0 + b ∣ 2 a \large\frac{|2ax_0+b|}{2a} 2a∣2ax0+b 的分母:
    分母 2 a 2a 2a可以由二次函数 y = a x 2 + b x + c ( a > 0 ) y=ax^2+bx+c (a>0) y=ax2+bx+c(a>0) 二阶导数得到

    ∂ 2 y ∂ x 2 = 2 a \large\frac{\partial^2 y}{\partial x^2}=\normalsize2a x22y=2a

    The best step is ∣ F i r s t − d e r i v a t i v e ∣ S e c o n d − d e r i v a t i v e \large\frac{|First-derivative|}{Second-derivative} SecondderivativeFirstderivative

    因此最好的步长应该时一阶导数的绝对值与二阶导数的比值

  • 对比公式: w t + 1 ← w t − η ∑ i = 0 t ( g i ) 2 g t w^{t+1} \gets w^t - \Large\frac{\eta}{\small\sqrt{\displaystyle\sum_{i=0}^t(g^i)^2}}\normalsize g^t wt+1wti=0t(gi)2 ηgt

g t g^t gt是一阶导数,二阶导数较难计算,使用 ∑ i = 0 t ( g i ) 2 \sqrt{\displaystyle\sum_{i=0}^t(g^i)^2} i=0t(gi)2 来估测
在这里插入图片描述

Tip 2: Stochastic Gradient Descent 随机梯度下降

  • Gradient Descent 普通梯度下降
    Loss is the summation over all training examples
    损失函数是所有训练样例的总和
    损失函数loss function
    L ( w , b ) = ∑ ( y n ^ − ( b + ∑ w 1 ⋅ x c p i ) ) 2 L(w,b)=\sum\big(\hat{y^n}-(b+\sum w_1\cdot x_{cp}^i)\big)^2 L(w,b)=(yn^(b+w1xcpi))2
    损失函数loss function梯度下降公式:
    θ i = θ i − 1 − η ∇ L ( θ i − 1 ) \theta^{i}=\theta^{i-1}-\eta\nabla L(\theta^{i-1}) θi=θi1ηL(θi1)

  • Stochastic Gradient Descent 随机梯度下降
    Loss for only one example
    每次使用一个样例
    损失函数loss function
    L ( w , b ) = ( y n ^ − ( b + ∑ w 1 ⋅ x c p i ) ) 2 L(w,b)=\big(\hat{y^n}-(b+\sum w_1\cdot x_{cp}^i)\big)^2 L(w,b)=(yn^(b+w1xcpi))2
    损失函数loss function梯度下降公式(与普通梯度下降相同):
    θ i = θ i − 1 − η ∇ L ( θ i − 1 ) \theta^{i}=\theta^{i-1}-\eta\nabla L(\theta^{i-1}) θi=θi1ηL(θi1)

  • 随机梯度下降Stochastic Gradient Descent优点:

    计算速度更快——“天下武功,唯快不破”
    在这里插入图片描述

Tip 3: Feature Scaling 特征缩放

1.特征缩放Feature Scaling是什么(what)?

设函数: y = b + w 1 x 1 + w 2 x 2 y=b+w_1x_1+w_2x_2 y=b+w1x1+w2x2
通过特征缩放Feature Scaling后如下图所示
在这里插入图片描述

2.特征缩放Feature Scaling为什么(why)?

Make different features have the same scaling
使不同的特征值有相同的比例

经过特征缩放后,便于进行梯度下降:
右边是两个参数scaling比较接近,右边的绿色图就比较接近圆形。
对于左边的情况,这种狭长的情形不用Adagrad算法比较难处理,两个方向上需要不同的学习率。而右边情形更新参数就会变得比较容易。左边的梯度下降并不是向着最低点方向走的,而是顺着等高线切线法线方向走的。但绿色就可以向着圆心(最低点)走,这样做参数更新也是比较有效率。
在这里插入图片描述

3.特征缩放Feature Scaling怎么做(how)?

其中一种做法类似于概率统计学中正态分布的标准化:
具体方法如下:

  • 一共有R组数据: { 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}

  • 对于第 r r r 个数据,其第 i i i 维特征值 x i r x_i^r xir 可以通过:
    x i r ← x i r − m i σ i x_i^r \gets \large\frac{x_i^r-m_i}{\sigma_i} xirσixirmi

    其中 m i m_i mi 是第 i i i 维数值的均值, σ i \sigma_i σi 是第 i i i 维数据的标准差

通过特征缩放Feature Scaling,所有维度数据的均值为0,标准差为1
在这里插入图片描述

三、Gradient Descent Theory梯度下降的数学理论

梯度下降Gradient Descent可以看作在圆圈内不断寻找最小值点,并更新圆心的过程
在这里插入图片描述

基本原理1:泰勒级数

  • 一阶泰勒展开:
    h ( x ) = ∑ k = 0 ∞ h ( k ) ( x 0 ) k ! ( x − x 0 ) k h(x)=\displaystyle\sum_{k=0}^∞\frac{h^{(k)}(x_0)}{k!}(x-x_0)^k h(x)=k=0k!h(k)(x0)(xx0)k
    = h ( x 0 ) + h ′ ( x 0 ) ( x − x 0 ) + h ′ ′ ( x 0 ) 2 ! ( x − x 0 ) 2 + . . . =h(x_0)+h'(x_0)(x-x_0)+\frac{h''(x_0)}{2!}(x-x_0)^2+... =h(x0)+h(x0)(xx0)+2!h′′(x0)(xx0)2+...
    x → x 0 x \to x_0 xx0 时,有 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)=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)+...
    由于‘…’中的项在 x → x 0 , y → y 0 x \to x_0,y \to y_0 xx0,yy0 时可以忽略,因此:
    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)

利用泰勒级数,对于有两个参数 { θ 1 , θ 2 } \{\theta_1,\theta_2\} {θ1,θ2}的损失函数loss function在点 ( a , b ) (a,b) (a,b) 处可以表示为如下形式:
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)

基本原理2:梯度矢量点乘

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 ) s=L(a,b) s=L(a,b)
u = ∂ L ( a , b ) ∂ θ 1 u=\frac{\partial L(a,b)}{\partial \theta_1} u=θ1L(a,b) , v = ∂ L ( a , b ) ∂ θ 2 v=\frac{\partial L(a,b)}{\partial \theta_2} v=θ2L(a,b)

L ( θ ) ≈ s + u ( θ 1 − a ) + v ( θ 2 − b ) L(\theta)\approx s+u(\theta_1-a)+v(\theta_2-b) L(θ)s+u(θ1a)+v(θ2b)
在这里插入图片描述
再令 ( θ 1 − a ) → Δ θ 1 , ( θ 2 − b ) → Δ θ 2 (\theta_1-a)\to \Delta\theta_1 , (\theta_2-b)\to \Delta\theta_2 (θ1a)Δθ1,(θ2b)Δθ2
( Δ θ 1 ) 2 + ( Δ θ 2 ) 2 ⩽ d 2 (\Delta\theta_1)^2+(\Delta\theta_2)^2\leqslant d^2 (Δθ1)2+(Δθ2)2d2
为了使 L ( θ ) L(\theta) L(θ)最小,考虑 Δ θ 1 , Δ θ 2 \Delta\theta_1,\Delta\theta_2 Δθ1,Δθ2组成的 ( Δ θ 1 , Δ θ 2 ) (\Delta\theta_1,\Delta\theta_2) (Δθ1,Δθ2)向量
L ( θ ) = s + ( Δ θ 1 , Δ θ 2 ) ⋅ ( u , v ) L(\theta)=s+(\Delta\theta_1,\Delta\theta_2)\cdot(u,v) L(θ)=s+(Δθ1,Δθ2)(u,v)
显然,当 ( Δ θ 1 , Δ θ 2 ) (\Delta\theta_1,\Delta\theta_2) (Δθ1,Δθ2) ( u , v ) (u,v) (u,v)反向时, L ( θ ) L(\theta) L(θ)最小。

因此,令 [ Δ θ 1 Δ θ 2 ] = η [ u v ] \begin{bmatrix} \Delta\theta_1 \\ \Delta\theta_2 \end{bmatrix}=\eta\begin{bmatrix} u \\ v \end{bmatrix} [Δθ1Δθ2]=η[uv] ,

[ θ 1 θ 2 ] = [ a b ] − η [ u v ] = [ a b ] − η [ ∂ L ( a , b ) ∂ θ 1 ∂ L ( a , b ) ∂ θ 2 ] \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]=[ab]η[uv]=[ab]η[θ1L(a,b)θ2L(a,b)]

在这里插入图片描述

四、梯度下降的限制

  • (1)Very slow at the plateau 在稳定的‘高原’时下降缓慢
  • (2)Stuck at saddle point 停在马鞍点
  • (3)Stuck at local minima 停在局部最小值点

在这里插入图片描述

ML Lecture 3-2 Gradient Descent (Demo by AOE)

ML Lecture 3-3 Gradient Descent -Demo by Minecraft-

【知识索引】【李宏毅机器学习】

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BkbK-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值