l14107cb 发表于 2024-8-31 00:35:27

从梯度下降到 Adam!一文看懂各样神经网络优化算法

<span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;Datawhale干货&nbsp;</p>
    </span><span style="color: black;"><strong style="color: blue;">编译:王小新,<span style="color: black;">源自</span>:量子位</strong></span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">调节</span>模型更新权重和偏差参数的方式时,你<span style="color: black;">是不是</span><span style="color: black;">思虑</span>过哪种优化算法能使模型产生更好且更快的效果?应该用梯度下降,随机梯度下降,还是Adam<span style="color: black;">办法</span>?</p>
    </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这篇<span style="color: black;">文案</span>介绍了<span style="color: black;">区别</span>优化算法之间的<span style="color: black;">重点</span>区别,以及<span style="color: black;">怎样</span><span style="color: black;">选取</span>最佳的优化<span style="color: black;">办法</span>。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">什么是优化算法?</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">优化算法的功能,是<span style="color: black;">经过</span>改善训练方式,来最小化(或最大化)损失函数E(x)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">模型内部有些参数,是用来计算测试集中<span style="color: black;">目的</span>值Y的真实值和预测值的偏差程度的,基于这些参数,就形<span style="color: black;">成为了</span>损失函数E(x)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">例如</span>说,权重(W)和偏差(b)<span style="color: black;">便是</span><span style="color: black;">这般</span>的内部参数,<span style="color: black;">通常</span>用于计算输出值,在训练神经网络模型时起到<span style="color: black;">重点</span><span style="color: black;">功效</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">在有效地训练模型并产生准确结果时,模型的内部参数起到了非常重要的<span style="color: black;">功效</span>。</strong>这<span style="color: black;">亦</span>是<span style="color: black;">为何</span><span style="color: black;">咱们</span>应该用<span style="color: black;">各样</span>优化策略和算法,来更新和计算影响模型训练和模型输出的网络参数,使其逼近或达到最优值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">优化算法分为两大类:</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1. 一阶优化算法</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种算法<span style="color: black;">运用</span>各参数的梯度值来最小化或最大化损失函数E(x)。<strong style="color: blue;">最常用的一阶优化算法是梯度下降。</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">函数梯度:导数dy/dx的多变量表达式,用来<span style="color: black;">暗示</span>y相<span style="color: black;">针对</span>x的瞬时变化率。<span style="color: black;">常常</span>为了计算多变量函数的导数时,会用梯度取代导数,并<span style="color: black;">运用</span>偏导数来计算梯度。梯度和导数之间的一个<span style="color: black;">重点</span>区别是函数的梯度形<span style="color: black;">成为了</span>一个向量场。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此呢</span>,对单变量函数,<span style="color: black;">运用</span>导数来分析;而梯度是基于多变量函数而产生的。<span style="color: black;">更加多</span>理论细节在<span style="color: black;">这儿</span><span style="color: black;">再也不</span>进行<span style="color: black;">仔细</span>解释。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2. 二阶优化算法</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">二阶优化算法<span style="color: black;">运用</span>了二阶导数(<span style="color: black;">亦</span>叫做<strong style="color: blue;">Hessian<span style="color: black;">办法</span></strong>)来最小化或最大化损失函数。<span style="color: black;">因为</span>二阶导数的计算成本很高,<span style="color: black;">因此</span>这种<span style="color: black;">办法</span>并<span style="color: black;">无</span>广泛<span style="color: black;">运用</span>。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">详解<span style="color: black;">各样</span>神经网络优化算法</span></h1>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">梯度下降</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在训练和优化智能系统时,梯度下降是一种最重要的技术和<span style="color: black;">基本</span>。梯度下降的功能是:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经过</span>寻找最小值,<span style="color: black;">掌控</span>方差,更新模型参数,<span style="color: black;">最后</span>使模型收敛。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">网络更新参数的公式为:θ=θ−η×∇(θ).J(θ) ,其中η是学习率,∇(θ).J(θ)是损失函数J(θ)的梯度。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是在神经网络中最常用的优化算法。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">如今,梯度下降<span style="color: black;">重点</span>用于在神经网络模型中进行权重更新,即在一个方向上更新和<span style="color: black;">调节</span>模型的参数,来最小化损失函数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2006年引入的反向传播技术,使得训练深层神经网络<span style="color: black;">作为</span>可能。反向传播技术是先在前向传播中计算输入信号的乘积及其对应的权重,<span style="color: black;">而后</span>将激活函数<span style="color: black;">功效</span>于这些乘积的总和。这种将输入信号转换为输出信号的方式,是一种对<span style="color: black;">繁杂</span>非线性函数进行建模的重要手段,并引入了非线性激活函数,使得模型能够学习到几乎任意形式的函数映射。<span style="color: black;">而后</span>,在网络的反向传播过程中回传<span style="color: black;">关联</span>误差,<span style="color: black;">运用</span>梯度下降更新权重值,<span style="color: black;">经过</span>计算误差函数E相<span style="color: black;">针对</span>权重参数W的梯度,在损失函数梯度的相反方向上更新权重参数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/YicUhk5aAGtB6zUnEhyibO3XJk6N5DqrmaVSGibibBx12WcumRgxJz95rrXHpmtFJoTjL5GRUFmTfKfMHT4zWL9wPw/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图1:</strong>权重更新方向与梯度方向相反</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">图1<span style="color: black;">表示</span>了权重更新过程与梯度矢量误差的方向相反,其中U形曲线为梯度。要<span style="color: black;">重视</span>到,当权重值W太小或太大时,会存在<span style="color: black;">很强</span>的误差,需要更新和优化权重,使其转化为合适值,所以<span style="color: black;">咱们</span>试图在与梯度相反的方向找到一个局部最优值。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">梯度下降的变体</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">传统的批量梯度下降将计算<span style="color: black;">全部</span>数据集梯度,但只会进行一次更新,<span style="color: black;">因此呢</span>在处理大型数据集时速度很慢且难以<span style="color: black;">掌控</span>,<span style="color: black;">乃至</span><span style="color: black;">引起</span>内存溢出。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">权重更新的快慢<span style="color: black;">是由于</span>学习率η决定的,并且<span style="color: black;">能够</span>在凸面误差曲面中收敛到全局最优值,在非凸曲面中可能趋于局部最优值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>标准形式的批量梯度下降还有一个问题,<span style="color: black;">便是</span>在训练大型数据集时存在冗余的权重更新。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">标准梯度下降的<span style="color: black;">以上</span>问题在随机梯度下降<span style="color: black;">办法</span>中得到<span style="color: black;">认识</span>决。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1. 随机梯度下降(SDG)</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">随机梯度下降(Stochastic gradient descent,SGD)对<span style="color: black;">每一个</span>训练样本进行参数更新,每次执行都进行一次更新,且执行速度更快。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">θ=θ−η⋅∇(θ) × J(θ;x(i);y(i)),其中x(i)和y(i)为训练样本。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">频繁的更新使得参数间<span style="color: black;">拥有</span>高方差,损失函数会以<span style="color: black;">区别</span>的强度波动。这<span style="color: black;">实质</span>上是一件好事,<span style="color: black;">由于</span>它有助于<span style="color: black;">咱们</span><span style="color: black;">发掘</span>新的和可能更优的局部最小值,而标准梯度下降将只会收敛到某个局部最优值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但SGD的问题是,<span style="color: black;">因为</span>频繁的更新和波动,<span style="color: black;">最后</span>将收敛到最小限度,并会因波动频繁存在超调量。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">虽然<span style="color: black;">已然</span><span style="color: black;">显示</span>,当缓慢降低学习率η时,标准梯度下降的收敛模式与SGD的模式相同。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/YicUhk5aAGtB6zUnEhyibO3XJk6N5DqrmahcNYXib4j1Cn7HI5nR6xriar5Rb7LdN9xdKzYId5ob1Sdr7YZkST0Mjw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图2:</strong><span style="color: black;">每一个</span>训练样本中高方差的参数更新会<span style="color: black;">引起</span>损失函数大幅波动,<span style="color: black;">因此呢</span><span style="color: black;">咱们</span>可能<span style="color: black;">没法</span><span style="color: black;">得到</span>给出损失函数的最小值。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一种<span style="color: black;">叫作</span>为“小批量梯度下降”的变体,则<span style="color: black;">能够</span><span style="color: black;">处理</span>高方差的参数更新和不稳定收敛的问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2. 小批量梯度下降</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了避免SGD和标准梯度下降中存在的问题,一个改进<span style="color: black;">办法</span>为小批量梯度下降(Mini Batch Gradient Descent),<span style="color: black;">由于</span>对<span style="color: black;">每一个</span>批次中的n个训练样本,这种<span style="color: black;">办法</span>只执行一次更新。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">运用</span>小批量梯度下降的优点是:</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1)</strong><span style="color: black;">能够</span>减少参数更新的波动,<span style="color: black;">最后</span>得到效果更好和更稳定的收敛。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2)</strong>&nbsp;还<span style="color: black;">能够</span><span style="color: black;">运用</span>最新的深层学习库中通用的矩阵优化<span style="color: black;">办法</span>,使计算小批量数据的梯度更加<span style="color: black;">有效</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3)</strong>&nbsp;<span style="color: black;">一般</span><span style="color: black;">来讲</span>,小批量样本的<span style="color: black;">体积</span>范围是从50到256,<span style="color: black;">能够</span><span style="color: black;">按照</span><span style="color: black;">实质</span>问题而有所<span style="color: black;">区别</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">4)</strong>&nbsp;在训练神经网络时,<span style="color: black;">一般</span>都会<span style="color: black;">选取</span>小批量梯度下降算法。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种<span style="color: black;">办法</span>有时候还是被<span style="color: black;">作为</span>SGD。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">运用</span>梯度下降及其变体时面临的挑战</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1.</strong>很难<span style="color: black;">选取</span>出合适的学习率。太小的学习率会<span style="color: black;">引起</span>网络收敛过于缓慢,而学习率太大可能会影响收敛,并<span style="color: black;">引起</span>损失函数在最小值上波动,<span style="color: black;">乃至</span><span style="color: black;">显现</span>梯度发散。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2.</strong>&nbsp;<span style="color: black;">另外</span>,相同的学习率并不适用于所有的参数更新。<span style="color: black;">倘若</span>训练集数据很稀疏,且特征频率非常<span style="color: black;">区别</span>,则<span style="color: black;">不该</span>该将其<span style="color: black;">所有</span>更新到相同的程度,<span style="color: black;">然则</span><span style="color: black;">针对</span>很少<span style="color: black;">显现</span>的特征,应<span style="color: black;">运用</span>更大的更新率。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3.</strong>在神经网络中,最小化非凸误差函数的另一个关键挑战是避免陷于多个其他局部最小值中。<span style="color: black;">实质</span>上,问题并非源于局部极小值,而是来自鞍点,即一个维度向上倾斜且另一维度向下倾斜的点。这些鞍点<span style="color: black;">一般</span>被相同误差值的平面所<span style="color: black;">包裹</span>,这使得SGD算法很难脱离出来,<span style="color: black;">由于</span>梯度在所有维度上接近于零。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">进一步优化梯度下降</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span>要讨论用于进一步优化梯度下降的<span style="color: black;">各样</span>算法。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1. 动量</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SGD<span style="color: black;">办法</span>中的高方差振荡使得网络很难稳定收敛,<span style="color: black;">因此</span>有<span style="color: black;">科研</span>者提出了一种<span style="color: black;">叫作</span>为动量(Momentum)的技术,<strong style="color: blue;"><span style="color: black;">经过</span>优化<span style="color: black;">关联</span>方向的训练和弱化无关方向的振荡,来加速SGD训练</strong>。换句话说,这种新<span style="color: black;">办法</span>将上个<span style="color: black;">过程</span>中更新向量的分量’γ’添加到当前更新向量。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">V(t)=γV(t−1)+η∇(θ).J(θ)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">最后<span style="color: black;">经过</span>θ=θ−V(t)来更新参数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">动量项γ<span style="color: black;">一般</span>设定为0.9,或相近的某个值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>的动量与经典<span style="color: black;">理学</span>学中的动量是一致的,就像从山上投出一个球,在下落过程中收集动量,小球的速度<span style="color: black;">持续</span><span style="color: black;">增多</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在参数更新过程中,其原理类似:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1)</strong>&nbsp;使网络能更优和更稳定的收敛;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2)</strong>&nbsp;减少振荡过程。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当其梯度指向实际移动方向时,动量项γ增大;当梯度与<span style="color: black;">实质</span>移动方向相反时,γ减小。这种方式<span style="color: black;">寓意</span>着动量项只对<span style="color: black;">关联</span>样本进行参数更新,减少了不必要的参数更新,从而得到更快且稳定的收敛,<span style="color: black;">亦</span>减少了振荡过程。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2. Nesterov梯度加速法</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一位名叫Yurii Nesterov<span style="color: black;">科研</span>员,认为动量<span style="color: black;">办法</span>存在一个问题:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>一个滚下山坡的球,<span style="color: black;">茫然</span>沿着斜坡下滑,这是非常不合适的。一个更聪明的球应该要<span style="color: black;">重视</span>到它将要去哪,<span style="color: black;">因此呢</span>在上坡再次向上倾斜时小球应该进行减速。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">实质</span>上,当小球达到曲线上的最低点时,动量相当高。<span style="color: black;">因为</span>高动量可能会<span style="color: black;">引起</span>其完全地<span style="color: black;">错失</span>最小值,<span style="color: black;">因此呢</span>小球不<span style="color: black;">晓得</span>何时进行减速,故继续向上移动。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Yurii Nesterov在1983年<span style="color: black;">发布</span>了一篇关于<span style="color: black;">处理</span>动量问题的论文,<span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span>把这种<span style="color: black;">办法</span>叫做Nestrov梯度加速法。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">在该<span style="color: black;">办法</span>中,他提出先<span style="color: black;">按照</span>之前的动量进行大步跳跃,<span style="color: black;">而后</span>计算梯度进行校正,从而实现参数更新。这种预更新<span style="color: black;">办法</span>能防止大幅振荡,不会<span style="color: black;">错失</span>最小值,并对参数更新更加<span style="color: black;">敏锐</span>。</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Nesterov梯度加速法(NAG)是一种赋予了动量项预知能力的<span style="color: black;">办法</span>,<span style="color: black;">经过</span><span style="color: black;">运用</span>动量项γV(t−1)来更改参数θ。<span style="color: black;">经过</span>计算θ−γV(t−1),得到下一位置的参数近似值,<span style="color: black;">这儿</span>的参数是一个粗略的概念。<span style="color: black;">因此呢</span>,<strong style="color: blue;"><span style="color: black;">咱们</span>不是<span style="color: black;">经过</span>计算当前参数θ的梯度值,而是<span style="color: black;">经过</span><span style="color: black;">关联</span>参数的大致<span style="color: black;">将来</span>位置,来有效地预知<span style="color: black;">将来</span></strong>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">V(t)=γV(t−1)+η∇(θ)J( θ−γV(t−1) ),<span style="color: black;">而后</span><span style="color: black;">运用</span>θ=θ−V(t)来更新参数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>,<span style="color: black;">咱们</span><span style="color: black;">经过</span>使网络更新与误差函数的斜率相适应,并依次加速SGD,<span style="color: black;">亦</span>可<span style="color: black;">按照</span><span style="color: black;">每一个</span>参数的重要性来<span style="color: black;">调节</span>和更新对应参数,以执行更大或更小的更新幅度。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3. Adagrad<span style="color: black;">办法</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Adagrad<span style="color: black;">办法</span>是<span style="color: black;">经过</span>参数来<span style="color: black;">调节</span>合适的学习率η,对稀疏参数进行大幅更新和对频繁参数进行小幅更新。<span style="color: black;">因此呢</span>,Adagrad<span style="color: black;">办法</span>非常适合处理稀疏数据。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">在时间步长中,Adagrad<span style="color: black;">办法</span>基于<span style="color: black;">每一个</span>参数计算的过往梯度,为<span style="color: black;">区别</span>参数θ设置<span style="color: black;">区别</span>的学习率。</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">先前,<span style="color: black;">每一个</span>参数θ(i)<span style="color: black;">运用</span>相同的学习率,每次会对所有参数θ进行更新。在<span style="color: black;">每一个</span>时间步t中,Adagrad<span style="color: black;">办法</span>为<span style="color: black;">每一个</span>参数θ<span style="color: black;">选择</span><span style="color: black;">区别</span>的学习率,更新对应参数,<span style="color: black;">而后</span>进行向量化。为了简单起见,<span style="color: black;">咱们</span>把在t时刻参数θ(i)的损失函数梯度设为g(t,i)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/YicUhk5aAGtB6zUnEhyibO3XJk6N5Dqrma2lwfpeJdic6hfaY0vJzEWap9VE9rXT9fe5dVE0W1G58gXvO3HrZQdjg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图3:</strong>参数更新公式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Adagrad<span style="color: black;">办法</span>是在<span style="color: black;">每一个</span>时间步中,<span style="color: black;">按照</span>过往已计算的参数梯度,来为<span style="color: black;">每一个</span>参数θ(i)修改对应的学习率η。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Adagrad<span style="color: black;">办法</span>的<span style="color: black;">重点</span>好处是,不需要手工来<span style="color: black;">调节</span>学习率。大<span style="color: black;">都数</span>参数<span style="color: black;">运用</span>了默认值0.01,且保持不变。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Adagrad<span style="color: black;">办法</span>的<span style="color: black;">重点</span>缺点是,学习率η总是在降低和衰减。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span><span style="color: black;">每一个</span>附加项都是正的,在分母中累积了多个平方梯度值,故累积的总和在训练<span style="color: black;">时期</span>保持增长。这反过来又<span style="color: black;">引起</span>学习率下降,变为很小数量级的数字,该模型完全停止学习,停止获取新的额外知识。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span>随着学习速度的越来越小,模型的学习能力<span style="color: black;">快速</span>降低,<span style="color: black;">况且</span>收敛速度非常慢,需要很长的训练和学习,即<strong style="color: blue;">学习速度降低</strong>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一个叫做Adadelta的算法改善了这个学习率<span style="color: black;">持续</span>衰减的问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">4. AdaDelta<span style="color: black;">办法</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是一个AdaGrad的延伸<span style="color: black;">办法</span>,它倾向于<span style="color: black;">处理</span>其学习率衰减的问题。Adadelta不是累积所有之前的平方梯度,而是将累积之前梯度的窗口限制到某个固定<span style="color: black;">体积</span>w。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">与之前无效地存储w先前的平方梯度<span style="color: black;">区别</span>,梯度的和被递归地定义为所有先前平方梯度的衰减平均值。<span style="color: black;">做为</span>与动量项<span style="color: black;">类似</span>的分数γ,在t时刻的滑动平均值Eg²仅仅取决于先前的平均值和当前梯度值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Eg²=γ.Eg²+(1−γ).g²(t),其中γ设置为与动量项相近的值,约为0.9。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Δθ(t)=−η⋅g(t,i).</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">θ(t+1)=θ(t)+Δθ(t)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/YicUhk5aAGtB6zUnEhyibO3XJk6N5Dqrma9fTFlURibKPA4REq9iafVwzrvOoytb0xmntLH3JSFUn2t6zPrXQjsU0A/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图4:</strong>参数更新的<span style="color: black;">最后</span>公式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">AdaDelta<span style="color: black;">办法</span>的另一个优点是,<span style="color: black;">已然</span>不需要设置一个默认的学习率。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">日前</span>已完成的改进</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">1)</strong>&nbsp;为<span style="color: black;">每一个</span>参数计算出<span style="color: black;">区别</span>学习率;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">2)&nbsp;</strong><span style="color: black;">亦</span>计算了动量项momentum;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">3)</strong>&nbsp;防止<strong style="color: blue;">学习率衰减或梯度消失</strong>等问题的<span style="color: black;">显现</span>。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">还<span style="color: black;">能够</span>做什么改进?</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在之前的<span style="color: black;">办法</span>中计算了<span style="color: black;">每一个</span>参数的对应学习率,<span style="color: black;">然则</span><span style="color: black;">为何</span>不计算<span style="color: black;">每一个</span>参数的对应动量变化并独立存储呢?这<span style="color: black;">便是</span>Adam算法提出的改良点。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">Adam算法</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Adam算法<strong style="color: blue;">即自适应时刻估计<span style="color: black;">办法</span>(Adaptive Moment Estimation)</strong>,能计算<span style="color: black;">每一个</span>参数的自适应学习率。这个<span style="color: black;">办法</span>不仅存储了AdaDelta先前平方梯度的指数衰减平均值,<span style="color: black;">况且</span>保持了先前梯度M(t)的指数衰减平均值,这一点与动量类似:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">M(t)为梯度的<span style="color: black;">第1</span>时刻平均值,V(t)为梯度的第二时刻非中心方差值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/YicUhk5aAGtB6zUnEhyibO3XJk6N5Dqrmadicibibvp3ejofTiconQ4k2Jefxho1KscYw2jt6qShibDXapiaUfYibcCE3hw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图5:</strong>两个公式分别为梯度的<span style="color: black;">第1</span>个时刻平均值和第二个时刻方差</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">则参数更新的<span style="color: black;">最后</span>公式为:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/YicUhk5aAGtB6zUnEhyibO3XJk6N5DqrmaKdWCB5iaAO7W97PjB6XUkeNMTY4uI76qehH0icKxdlicjmXIgj4bjv2fg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图6:</strong>参数更新的<span style="color: black;">最后</span>公式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中,β1设为0.9,β2设为0.9999,ϵ设为10-8。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">实质</span>应用中,Adam<span style="color: black;">办法</span>效果良好。与其他自适应学习率算法相比,其收敛速度更快,学习效果更为有效,<span style="color: black;">况且</span><span style="color: black;">能够</span>纠正其他优化技术中存在的问题,如学习率消失、收敛过慢或是高方差的参数更新<span style="color: black;">引起</span>损失函数波动<span style="color: black;">很强</span>等问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">对优化算法进行可视化</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/YicUhk5aAGtB6zUnEhyibO3XJk6N5DqrmaqvK2Pl9ds2GG1EwGt2jiciaPCoLgl1oNaPJCGFtia9OR2iaalIpnMQuVSQ/640?wx_fmt=gif&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><strong style="color: blue;">图7:</strong>对鞍点进行SGD优化</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">从上面的动画<span style="color: black;">能够</span>看出,自适应算法能<span style="color: black;">火速</span>收敛,并快速找到参数更新中正确的<span style="color: black;">目的</span>方向;而标准的SGD、NAG和动量项等<span style="color: black;">办法</span>收敛缓慢,且很难找到正确的方向。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">结论</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>应该<span style="color: black;">运用</span>哪种优化器?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在构建神经网络模型时,<span style="color: black;">选取</span>出最佳的优化器,以便快速收敛并正确学习,<span style="color: black;">同期</span><span style="color: black;">调节</span>内部参数,最大程度地最小化损失函数。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Adam在<span style="color: black;">实质</span>应用中效果良好,超过了其他的自适应技术。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">倘若</span>输入数据集比较稀疏,SGD、NAG和动量项等<span style="color: black;">办法</span>可能效果<span style="color: black;">欠好</span>。</strong><span style="color: black;">因此呢</span><span style="color: black;">针对</span>稀疏数据集,应该<span style="color: black;">运用</span>某种自适应学习率的<span style="color: black;">办法</span>,且另一好处为不需要人为<span style="color: black;">调节</span>学习率,<span style="color: black;">运用</span>默认参数就可能<span style="color: black;">得到</span>最优值。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">倘若</span>想使训练深层网络模型快速收敛或所构建的神经网络较为<span style="color: black;">繁杂</span>,则应该<span style="color: black;">运用</span>Adam或其他自适应学习速率的<span style="color: black;">办法</span></strong>,<span style="color: black;">由于</span>这些<span style="color: black;">办法</span>的<span style="color: black;">实质</span>效果更优。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">期盼</span>你能<span style="color: black;">经过</span>这篇<span style="color: black;">文案</span>,很好地理解<span style="color: black;">区别</span>优化算法间的特性差异。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">关联</span>链接</span><span style="color: black;">:</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">二阶优化算法:</p>https://web.stanford.edu/class/msande311/lecture13.pdf

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Nesterov梯度加速法:http://cs231n.github.io/neural-networks-3/</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/vI9nYe94fsGxu3P5YibTO899okS0X9WaLmQCtia4U8Eu1xWCz9t8Qtq9PH6T1bTcxibiaCIkGzAxpeRkRFYqibVmwSw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">干货学习,</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">点</span></strong></span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">赞</span></strong></span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">三连</span></span></strong><span style="color: black;">↓</span></p>




b1gc8v 发表于 2024-9-26 07:50:08

楼主的文章非常有意义,提升了我的知识水平。

b1gc8v 发表于 2024-9-30 18:57:53

你说得对,我们一起加油,未来可期。

qzmjef 发表于 2024-10-14 05:39:07

外链发布论坛学习网络优化SEO。

qzmjef 发表于 2024-11-10 08:33:25

感谢你的精彩评论,带给我新的思考角度。

j8typz 发表于 3 天前

对于这个问题,我有不同的看法...
页: [1]
查看完整版本: 从梯度下降到 Adam!一文看懂各样神经网络优化算法