参考伯禹学习平台《动手学深度学习》课程内容内容撰写的学习笔记
原文链接:https://www.boyuai.com/elites/course/cZu18YmweLv10OeV/lesson/VjB5UFeXPJ2EQsr9amShYU
感谢伯禹平台,Datawhale,和鲸,AWS给我们提供的免费学习机会!!
总的学习感受:伯禹的课程做的很好,课程非常系统,每个较高级别的课程都会有需要掌握的前续基础知识的介绍,因此很适合本人这种基础较差的同学学习,建议基础较差的同学可以关注伯禹的其他课程:
数学基础:https://www.boyuai.com/elites/course/D91JM0bv72Zop1D3
机器学习基础:https://www.boyuai.com/elites/course/5ICEBwpbHVwwnK3C
优化算法进阶
Momentum
在 Section 11.4 中,我们提到,目标函数有关自变量的梯度代表了目标函数在自变量当前位置下降最快的方向。因此,梯度下降也叫作最陡下降(steepest descent)。在每次迭代中,梯度下降根据自变量当前位置,沿着当前位置的梯度更新自变量。然而,如果自变量的迭代方向仅仅取决于自变量当前位置,这可能会带来一些问题。对于noisy gradient,我们需要谨慎的选取学习率和batch size, 来控制梯度方差和收敛的结果。
g t = ∂ w 1 ∣ B t ∣ ∑ i ∈ B t f ( x i , w t − 1 ) = 1 ∣ B t ∣ ∑ i ∈ B t g i , t − 1 . \mathbf{g}_t = \partial_{\mathbf{w}} \frac{1}{|\mathcal{B}_t|} \sum_{i \in \mathcal{B}_t} f(\mathbf{x}_{i}, \mathbf{w}_{t-1}) = \frac{1}{|\mathcal{B}_t|} \sum_{i \in \mathcal{B}_t} \mathbf{g}_{i, t-1}. gt=∂w∣Bt∣1i∈Bt∑f(xi,wt−1)=∣Bt∣1i∈Bt∑gi,t−1.
An ill-conditioned Problem
Condition Number of Hessian Matrix:
c o n d H = λ m a x λ m i n cond_{H} = \frac{\lambda_{max}}{\lambda_{min}} condH=λminλmax
where λ m a x , λ m i n \lambda_{max}, \lambda_{min} λmax,λmin is the maximum amd minimum eignvalue of Hessian matrix.
让我们考虑一个输入和输出分别为二维向量 x = [ x 1 , x 2 ] ⊤ \boldsymbol{x} = [x_1, x_2]^\top x=[x1,x2]⊤和标量的目标函数:
f ( x ) = 0.1 x 1 2 + 2 x 2 2 f(\boldsymbol{x})=0.1x_1^2+2x_2^2 f(x)=0.1x12+2x22
c o n d H = 4 0.2 = 20 → ill-conditioned cond_{H} = \frac{4}{0.2} = 20 \quad \rightarrow \quad \text{ill-conditioned} condH=0.24=20→ill-conditioned
Maximum Learning Rate
- For f ( x ) f(x) f(x), according to convex optimizaiton conclusions, we need step size η < 1 / L \eta<1/L η<1/L.to have the fastest
convergence,where L = m a x x ∇ 2 f ( x ) L=max_{x}\nabla^{2}f(x) L=maxx∇2f(x) called smooth - To guarantee the convergence, we need to have η < 2 / L \eta<2/L η<2/L .
Supp: Preconditioning
在二阶优化中,我们使用Hessian matrix的逆矩阵(或者pseudo inverse)来左乘梯度向量 i . e . Δ x = H − 1 g i.e. \Delta_{x} = H^{-1}\mathbf{g} i.e.Δx=H−1g,这样的做法称为precondition,相当于将 H H H 映射为一个单位矩阵,拥有分布均匀的Spectrum,也即我们去优化的等价标函数的Hessian matrix为良好的identity matrix。
与Section 11.4一节中不同,这里将 x 1 2 x_1^2 x12系数从 1 1 1减小到了 0.1 0.1 0.1。下面实现基于这个目标函数的梯度下降,并演示使用学习率为 0.4 0.4 0.4时自变量的迭代轨迹。
%matplotlib inline
import sys
sys.path.append("/home/kesci/input")
import d2lzh1981 as d2l
import torch
eta = 0.4
def f_2d(x1, x2):
return 0.1 * x1 ** 2 + 2 * x2 ** 2
def gd_2d(x1, x2, s1, s2):
return (x1 - eta * 0.2 * x1, x2 - eta * 4 * x2, 0, 0)
d2l.show_trace_2d(f_2d, d2l.train_2d(gd_2d))
可以看到,同一位置上,目标函数在竖直方向( x 2 x_2 x2轴方向)比在水平方向( x 1 x_1 x1轴方向)的斜率的绝对值更大。因此,给定学习率,梯度下降迭代自变量时会使自变量在竖直方向比在水平方向移动幅度更大。那么,我们需要一个较小的学习率从而避免自变量在竖直方向上越过目标函数最优解。然而,这会造成自变量在水平方向上朝最优解移动变慢。
下面我们试着将学习率调得稍大一点,此时自变量在竖直方向不断越过最优解并逐渐发散。
Solution to ill-condition
- Preconditioning gradient vector: applied in Adam, RMSProp, AdaGrad, Adelta, KFC, Natural gradient and other secord-order optimization algorithms.
- Averaging history gradient: like momentum, which allows larger learning rates to accelerate convergence; applied in Adam, RMSProp, SGD momentum.
eta = 0.6
d2l.show_trace_2d(f_2d, d2l.train_2d(gd_2d))
Momentum Algorithm
动量法的提出是为了解决梯度下降的上述问题。设时间步 t t t 的自变量为 x t \boldsymbol{x}_t xt,学习率为 η t \eta_t ηt。
在时间步 t = 0 t=0 t=0,动量法创建速度变量 m 0 \boldsymbol{m}_0 m0,并将其元素初始化成 0。在时间步 t > 0 t>0 t>0,动量法对每次迭代的步骤做如下修改:
m t ← β m t − 1 + η t g t , x t ← x t − 1 − m t , \begin{aligned} \boldsymbol{m}_t &\leftarrow \beta \boldsymbol{m}_{t-1} + \eta_t \boldsymbol{g}_t, \\ \boldsymbol{x}_t &\leftarrow \boldsymbol{x}_{t-1} - \boldsymbol{m}_t, \end{aligned} mtxt←βmt−1+ηtgt,←xt−1−mt,
Another version:
m t ← β m t − 1 + ( 1 − β ) g t , x t ← x t − 1 − α t m t , \begin{aligned} \boldsymbol{m}_t &\leftarrow \beta \boldsymbol{m}_{t-1} + (1-\beta) \boldsymbol{g}_t, \\ \boldsymbol{x}_t &\leftarrow \boldsymbol{x}_{t-1} - \alpha_t \boldsymbol{m}_t, \end{aligned} mtxt←βmt−1+(1−β)gt,←xt−1−αtmt,
α t = η t 1 − β \alpha_t = \frac{\eta_t}{1-\beta} αt=1−βηt
其中,动量超参数 β \beta β满足 0 ≤ β < 1 0 \leq \beta < 1 0≤β<1。当 β = 0 \beta=0 β=