Decoupled Weight Decay Regularization/paper/zh: Difference between revisions

    From Marovi AI
    (Batch translate Decoupled Weight Decay Regularization/paper unit 71 → zh)
     
    (Batch translate Decoupled Weight Decay Regularization/paper unit 143 → zh)
     
    Line 391: Line 391:
    |}
    |}


    <div class="mw-translate-fuzzy">
    其中 <math display="inline">\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}</math> 与 <math display="inline">\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)}</math> 是乘子的取值范围,<math display="inline">T_{c\hspace{0pt}u\hspace{0pt}r}</math> 表示自上次 restart 以来已完成的 epoch 数。<math display="inline">T_{c\hspace{0pt}u\hspace{0pt}r}</math> 在每个 batch 迭代 <math display="inline">t</math> 处更新,因此不限于整数值。在每次第 <math display="inline">i</math> 次 restart 时调整(例如减小)<math display="inline">\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}</math> 与 <math display="inline">\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)}</math>(亦可参见 Smith([[#bib.bib18|2016]]))有可能进一步改善性能,但此处我们不考虑这一选项,以避免引入额外的超参数。当 <math display="inline">\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)} = 1</math> 且 <math display="inline">\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)} = 0</math> 时,可将公式([[#A2.E14|14]])简化为
    其中 <math display="inline">\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}</math> 与 <math display="inline">\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)}</math> 是乘子的取值范围,<math display="inline">T_{c\hspace{0pt}u\hspace{0pt}r}</math> 表示自上次 restart 以来已完成的 epoch 数。<math display="inline">T_{c\hspace{0pt}u\hspace{0pt}r}</math> 在每个 batch 迭代 <math display="inline">t</math> 处更新,因此不限于整数值。在每次第 <math display="inline">i</math> 次 restart 时调整(例如减小)<math display="inline">\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}</math> 与 <math display="inline">\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)}</math>(亦可参见 Smith([[#bib.bib18|2016]]))有可能进一步改善性能,但此处我们不考虑这一选项,以避免引入额外的超参数。当 <math display="inline">\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)} = 1</math> 且 <math display="inline">\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)} = 0</math> 时,可将公式([[#A2.E14|14]])简化为
    </div>


    {| id="A4.EGx6" class="wikitable"
    {| id="A4.EGx6" class="wikitable"

    Latest revision as of 07:58, 27 April 2026

    Other languages:
    SummarySource
    Research Paper
    Authors Ilya Loshchilov; Frank Hutter
    Year 2017
    Topic area Machine Learning
    Difficulty Research
    arXiv 1711.05101
    PDF Download PDF

    Decoupled Weight Decay Regularization

    Ilya Loshchilov & Frank Hutter
    / 弗赖堡大学
    / 德国 弗赖堡,
    / {ilya,fh}@cs.uni-freiburg.de

    摘要

    L2 正则化与权重衰减(weight decay)正则化对标准随机梯度下降是等价的(在按学习率重新缩放后),但正如我们所证明的,对于像 Adam 这样的自适应梯度算法,情况并非如此。尽管这些算法的常见实现使用 L2 正则化(且常将其称为「权重衰减」,鉴于我们所揭示的不等价性,这种命名可能具有误导性),我们提出一种简单的修改,通过将权重衰减从针对损失函数的优化步骤中解耦出来,恢复权重衰减正则化的原始形式。我们提供实证证据表明,所提出的修改 (i) 对标准 SGD 和 Adam 而言都将最优权重衰减系数的选择与学习率的设置解耦,并 (ii) 大幅提升 Adam 的泛化性能,使其能够在图像分类数据集上与带 momentum 的 SGD 相竞争(而在此之前 Adam 在这些任务上通常处于下风)。我们提出的解耦权重衰减已被许多研究者采用,社区也已在 TensorFlow 和 PyTorch 中实现;我们实验的完整源代码可见于 https://github.com/loshchil/AdamW-and-SGDW

    1 引言

    AdaGrad(Duchi 等,2011)、RMSProp(Tieleman 与 Hinton,2012)、Adam(Kingma 与 Ba,2014)以及最近的 AMSGrad(Reddi 等,2018)等自适应梯度方法,已成为训练前馈和循环神经网络时默认的选择(Xu 等,2015;Radford 等,2015)。尽管如此,在 CIFAR-10、CIFAR-100(Krizhevsky 2009)等流行图像分类数据集上的最新成果仍然由带 momentum 的 SGD 取得(Gastaldi,2017;Cubuk 等,2018)。此外,Wilson 等(2017)指出:在一系列多样化的深度学习任务(图像分类、字符级语言建模和成分句法分析等)上进行测试时,自适应梯度方法的泛化性能不如带 momentum 的 SGD。研究者已就这种较差泛化性能的成因提出了不同假设,例如尖锐局部极小点的存在(Keskar 等,2016;Dinh 等,2017)以及自适应梯度方法本身固有的问题(Wilson 等,2017)。本文研究:用 SGD 与 Adam 训练深度神经网络时,使用 L2 正则化还是权重衰减正则化更好。我们表明:最流行的自适应梯度方法 Adam 泛化较差的一个主要因素,是 L2 正则化对它远不如对 SGD 那样有效。具体而言,我们对 Adam 的分析得出以下观察:

    /
    L2 正则化与权重衰减并不相同。对于 SGD,可以通过基于学习率对权重衰减系数进行重参数化使两者等价;然而,常被忽视的是:对于 Adam 并非如此。特别地,当 L2 正则化与自适应梯度结合时,那些历史参数和/或梯度幅值较大的权重所受到的正则化会比使用权重衰减时弱。 / ; / : L2 正则化在 Adam 中并不有效。Adam 等自适应梯度方法之所以可能不及带 momentum 的 SGD,一个可能的原因是常见深度学习库只实现了 L2 正则化,而非原始的权重衰减。因此在那些 L2 正则化对 SGD 有益的任务/数据集(例如许多流行的图像分类数据集)上,Adam 的结果会比带 momentum 的 SGD 更差(对后者而言 L2 正则化的行为符合预期)。 / ; / : 权重衰减在 SGD 和 Adam 中同样有效。对 SGD,它等价于 L2 正则化;而对 Adam 则不然。 / ; / : 最优权重衰减取决于 batch 总通过次数 / 权重更新次数。我们对 SGD 与 Adam 的实证分析表明:要执行的运行时间/ batch 通过次数越多,最优权重衰减就越小。 / ; / : Adam 可以从计划好的学习率乘子中获得显著收益。Adam 是一种自适应梯度算法,因此会为每个参数自适应地调整学习率,但这并排除使用一个按调度(例如余弦退火)变化的全局学习率乘子来大幅提升其性能的可能性。

    本文的主要贡献是通过将权重衰减从基于梯度的更新中解耦来改进 Adam 的正则化。在一项全面的分析中,我们表明:与 L2 正则化相比,Adam 在使用解耦权重衰减时的泛化能力大幅提升,测试误差获得 15% 的相对改进(参见图 23);这一结论在多个图像识别数据集(CIFAR-10 与 ImageNet32x32)、不同训练预算(100 至 1800 epoch)和不同学习率调度(固定、阶梯衰减以及余弦退火;参见图 1)下都成立。我们还证明:解耦权重衰减使得最优学习率与最优权重衰减系数的取值大体相互独立,从而简化超参数优化(参见图 2)。

    本文的主要动机是改进 Adam,使其在那些过去不具竞争力的问题上也能与带 momentum 的 SGD 相抗衡。我们希望以此使从业者不再需要在 Adam 和 SGD 之间切换,从而减少为每个数据集/任务挑选特定训练算法及超参数这一常见难题。

    2 将权重衰减从基于梯度的更新中解耦

    在 Hanson 与 Pratt(1988)所描述的权重衰减中,权重 $ {\textstyle \mathbf{θ}} $ 按下式呈指数衰减:

    $ {\textstyle {{\mathbf{θ}}_{t + 1} = {{{({1 - \lambda})}\hspace{0pt}{\mathbf{θ}}_{t}} - {\alpha\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}}},} $ (1)

    其中 $ {\textstyle \lambda} $ 定义每一步权重衰减的速率,$ {\textstyle {\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}} $ 是第 $ {\textstyle t} $ 个 batch 的梯度,将与学习率 $ {\textstyle \alpha} $ 相乘。对标准 SGD 而言,这等价于标准的 L2 正则化:

    命题 1(对标准 SGD 而言:weight decay = L2 正则化)。

    以基础学习率 $ {\textstyle \alpha} $ 运行的标准 SGD,在 batch 损失函数 $ {\textstyle f_{t}\hspace{0pt}{({\mathbf{θ}})}} $ 上使用权重衰减 $ {\textstyle \lambda} $(由公式 1 定义)所执行的步骤,与其在 $ {\textstyle {f_{t}^{\text{reg}}\hspace{0pt}{({\mathbf{θ}})}} = {{f_{t}\hspace{0pt}{({\mathbf{θ}})}} + {\frac{\lambda^{\prime}}{2}\hspace{0pt}\left. \parallel{\mathbf{θ}}\parallel \right._{2}^{2}}}} $ 上不使用权重衰减所执行的步骤相同,其中 $ {\textstyle \lambda^{\prime} = \frac{\lambda}{\alpha}} $

    这一已知事实以及我们其它命题的证明见附录 A

    由于这一等价关系,L2 正则化在包括流行深度学习库在内的诸多场合都被称作权重衰减。然而,正如我们将在本节后面证明的那样,这一等价关系对自适应梯度方法并不成立。即便对 SGD 这一简单情形,一个常被忽视的事实是:为了使该等价关系成立,L2 正则化系数 $ {\textstyle \lambda^{\prime}} $ 必须设为 $ {\textstyle \frac{\lambda}{\alpha}} $;也就是说,若存在一个总体最佳的权重衰减取值 $ {\textstyle \lambda} $,那么 $ {\textstyle \lambda^{\prime}} $ 的最佳取值就与学习率 $ {\textstyle \alpha} $ 紧密耦合。为了解耦这两个超参数的影响,我们主张按 Hanson 与 Pratt(1988)所提出的方式(公式 1)解耦权重衰减步骤。

    1:  given initial learning rate $ {\textstyle \alpha \in {IR}} $, momentum factor $ {\textstyle \beta_{1} \in {IR}} $, weight decay/L2 regularization factor $ {\textstyle \lambda \in {IR}} $ 2:  initialize time step $ {\textstyle t\leftarrow 0} $, parameter vector $ {\textstyle {\mathbf{θ}}_{t = 0} \in {IR}^{n}} $, first moment vector $ {\textstyle \text{m}_{t = 0}\leftarrow\text{0}} $, schedule multiplier $ {\textstyle \eta_{t = 0} \in {IR}} $ 3:  repeat 4:     $ {\textstyle t\leftarrow{t + 1}} $ 5:     $ {\textstyle {{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t - 1})}}\leftarrow{\text{SelectBatch}\hspace{0pt}{({\mathbf{θ}}_{t - 1})}}} $ $ {\textstyle \rhd} $  select batch and return the corresponding gradient 6:     $ {\textstyle \text{g}_{t}\leftarrow{{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t - 1})}}} $ $ {\textstyle + {\lambda\hspace{0pt}{\mathbf{θ}}_{t - 1}}} $ 7:     $ {\textstyle \eta_{t}\leftarrow{\text{SetScheduleMultiplier}\hspace{0pt}{(t)}}} $ $ {\textstyle \rhd} $  can be fixed, decay, be used for warm restarts 8:     $ {\textstyle \text{m}_{t}\leftarrow{{\beta_{1}\hspace{0pt}\text{m}_{t - 1}} + {\eta_{t}\hspace{0pt}\alpha\hspace{0pt}\text{g}_{t}}}} $ 9:     $ {\textstyle {\mathbf{θ}}_{t}\leftarrow{{\mathbf{θ}}_{t - 1} - \text{m}_{t}}} $ $ {\textstyle - {\eta_{t}\hspace{0pt}\lambda\hspace{0pt}{\mathbf{θ}}_{t - 1}}} $ 10:  until  stopping criterion is met 11:  return  optimized parameters $ {\textstyle {\mathbf{θ}}_{t}} $

    1:  given $ {\textstyle {\alpha = 0.001},{{\beta_{1} = 0.9},{{\beta_{2} = 0.999},{{\epsilon = 10^{- 8}},{\lambda \in {IR}}}}}} $ 2:  initialize time step $ {\textstyle t\leftarrow 0} $, parameter vector $ {\textstyle {\mathbf{θ}}_{t = 0} \in {IR}^{n}} $, first moment vector $ {\textstyle \text{m}_{t = 0}\leftarrow\text{0}} $, second moment vector $ {\textstyle \text{v}_{t = 0}\leftarrow\text{0}} $, schedule multiplier $ {\textstyle \eta_{t = 0} \in {IR}} $ 3:  repeat 4:     $ {\textstyle t\leftarrow{t + 1}} $ 5:     $ {\textstyle {{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t - 1})}}\leftarrow{\text{SelectBatch}\hspace{0pt}{({\mathbf{θ}}_{t - 1})}}} $ $ {\textstyle \rhd} $  select batch and return the corresponding gradient 6:     $ {\textstyle \text{g}_{t}\leftarrow{{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t - 1})}}} $ $ {\textstyle + {\lambda\hspace{0pt}{\mathbf{θ}}_{t - 1}}} $ 7:     $ {\textstyle \text{m}_{t}\leftarrow{{\beta_{1}\hspace{0pt}\text{m}_{t - 1}} + {{({1 - \beta_{1}})}\hspace{0pt}\text{g}_{t}}}} $ $ {\textstyle \rhd} $  here and below all operations are element-wise 8:     $ {\textstyle \text{v}_{t}\leftarrow{{\beta_{2}\hspace{0pt}\text{v}_{t - 1}} + {{({1 - \beta_{2}})}\hspace{0pt}\text{g}_{t}^{2}}}} $ 9:     $ {\textstyle {\hat{\text{m}}}_{t}\leftarrow{\text{m}_{t}/{({1 - \beta_{1}^{t}})}}} $ $ {\textstyle \rhd} $  $ {\textstyle \beta_{1}} $ is taken to the power of $ {\textstyle t} $ 10:     $ {\textstyle {\hat{\text{v}}}_{t}\leftarrow{\text{v}_{t}/{({1 - \beta_{2}^{t}})}}} $ $ {\textstyle \rhd} $  $ {\textstyle \beta_{2}} $ is taken to the power of $ {\textstyle t} $ 11:     $ {\textstyle \eta_{t}\leftarrow{\text{SetScheduleMultiplier}\hspace{0pt}{(t)}}} $ $ {\textstyle \rhd} $  can be fixed, decay, or also be used for warm restarts 12:     $ {\textstyle {\mathbf{θ}}_{t}\leftarrow{{\mathbf{θ}}_{t - 1} - {\eta_{t}\hspace{0pt}\left( {{{\alpha\hspace{0pt}{\hat{\text{m}}}_{t}}/{({\sqrt{{\hat{\text{v}}}_{t}} + \epsilon})}}\hspace{0pt}{+ {\lambda\hspace{0pt}{\mathbf{θ}}_{t - 1}}}} \right)}}} $ 13:  until  stopping criterion is met 14:  return  optimized parameters $ {\textstyle {\mathbf{θ}}_{t}} $

    首先看 SGD 的情形:我们提议在 Algorithm 1 第 9 行,按梯度信息对 $ {\textstyle {\mathbf{θ}}_{t}} $ 做更新的同时,对权重进行衰减。这便给出我们所提出的、使用解耦权重衰减的带 momentum SGD 变体(SGDW)。这一简单修改将 $ {\textstyle \lambda} $$ {\textstyle \alpha} $ 显式地解耦(当然,与任意两个超参数一样,也可能仍存在某种依赖于问题的隐式耦合)。为了支持对 $ {\textstyle \alpha} $$ {\textstyle \lambda} $ 的可能调度,我们引入一个由用户自定义过程 $ {\textstyle S\hspace{0pt}e\hspace{0pt}t\hspace{0pt}S\hspace{0pt}c\hspace{0pt}h\hspace{0pt}e\hspace{0pt}d\hspace{0pt}u\hspace{0pt}l\hspace{0pt}e\hspace{0pt}M\hspace{0pt}u\hspace{0pt}l\hspace{0pt}t\hspace{0pt}i\hspace{0pt}p\hspace{0pt}l\hspace{0pt}i\hspace{0pt}e\hspace{0pt}r\hspace{0pt}{(t)}} $ 提供的缩放因子 $ {\textstyle \eta_{t}} $

    现在转向像流行的优化器 Adam(Kingma 与 Ba,2014)这样的自适应梯度算法,它们会按梯度的历史幅值对其进行缩放。直观地讲,当 Adam 在损失函数 $ {\textstyle f} $ 加上 L2 正则化的目标上运行时,那些在 $ {\textstyle f} $ 中梯度通常较大的权重所受到的正则化会弱于使用解耦权重衰减时的情形,因为正则项的梯度会与 $ {\textstyle f} $ 的梯度一起被缩放。这就导致:对自适应梯度算法而言,L2 正则化与解耦权重衰减并不等价:

    命题 2(对自适应梯度而言:weight decay \neq L2 正则化)。

    $ {\textstyle O} $ 表示一个优化器,在 batch 损失函数 $ {\textstyle f_{t}\hspace{0pt}{({\mathbf{θ}})}} $ 上不使用权重衰减时其迭代为 $ {\textstyle {\mathbf{θ}}_{t + 1}\leftarrow{{\mathbf{θ}}_{t} - {\alpha\hspace{0pt}\mathbf{M}_{t}\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}}} $,使用权重衰减时其迭代为 $ {\textstyle {\mathbf{θ}}_{t + 1}\leftarrow{{{({1 - \lambda})}\hspace{0pt}{\mathbf{θ}}_{t}} - {\alpha\hspace{0pt}\mathbf{M}_{t}\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}}} $,其中 $ {\textstyle \mathbf{M}_{t} \neq {k\hspace{0pt}\mathbf{I}}} $$ {\textstyle k \in {\mathbb{R}}} $)。那么,对 $ {\textstyle O} $ 而言,不存在任何 L2 系数 $ {\textstyle \lambda^{\prime}} $,使得在 batch 损失 $ {\textstyle {f_{t}^{\text{reg}}\hspace{0pt}{({\mathbf{θ}})}} = {{f_{t}\hspace{0pt}{({\mathbf{θ}})}} + {\frac{\lambda^{\prime}}{2}\hspace{0pt}\left. \parallel{\mathbf{θ}}\parallel \right._{2}^{2}}}} $ 上不使用权重衰减地运行 $ {\textstyle O} $ 等价于在 $ {\textstyle f_{t}\hspace{0pt}{({\mathbf{θ}})}} $ 上以衰减 $ {\textstyle \lambda \in {\mathbb{R}}^{+}} $ 运行 $ {\textstyle O} $

    我们如 Algorithm 2 第 12 行所示,将 Adam 中的权重衰减与基于损失的梯度更新解耦;这便得到了我们提出的、使用解耦权重衰减的 Adam 变体(AdamW)。

    既然已证明对自适应梯度算法而言 L2 正则化与权重衰减正则化不同,那就引出了它们如何不同、又应如何理解其效果的问题。它们对标准 SGD 的等价性仍非常有助于直观理解:两种机制都以相同的速率把权重推向零。然而对自适应梯度算法二者则有所不同:在 L2 正则化下,损失函数的梯度与正则项(即权重的 L2 范数)的梯度之和会一起被自适应处理;而在解耦权重衰减下,仅对损失函数的梯度进行自适应处理(权重衰减步骤与自适应梯度机制分离)。在 L2 正则化下,两种梯度都按其典型(累计)幅值归一化,因此典型梯度幅值 $ {\textstyle s} $ 较大的权重 $ {\textstyle x} $ 所受到的相对正则化量比其它权重更小。相对地,解耦权重衰减以同一速率 $ {\textstyle \lambda} $ 正则化所有权重,这相当于对 $ {\textstyle s} $ 较大的权重 $ {\textstyle x} $ 比标准 L2 正则化更强地施加正则化。我们将对一类带固定预条件子的自适应梯度算法的简单特例形式化地证明这一点:

    命题 3(对带固定预条件子的自适应梯度算法而言:weight decay = 按尺度调整的 L_{2} 正则化)。

    $ {\textstyle O} $ 表示具有命题 2 中相同特性的算法,并使用固定的预条件子矩阵 $ {\textstyle \text{M}_{t} = {\text{diag}\hspace{0pt}{(\text{s})}^{- 1}}} $(对所有 $ {\textstyle i} $ 都有 $ {\textstyle s_{i} > 0} $)。那么,以基础学习率 $ {\textstyle \alpha} $ 运行的 $ {\textstyle O} $ 在 batch 损失函数 $ {\textstyle f_{t}\hspace{0pt}{({\mathbf{θ}})}} $ 上使用权重衰减 $ {\textstyle \lambda} $ 所执行的步骤,与其在按尺度调整的正则化 batch 损失

    $ {\displaystyle {{f_{t}^{\text{sreg}}\hspace{0pt}{({\mathbf{θ}})}} = {{f_{t}\hspace{0pt}{({\mathbf{θ}})}} + {\frac{\lambda^{\prime}}{2\hspace{0pt}\alpha}\hspace{0pt}\left. \parallel{{\mathbf{θ}} \odot \sqrt{\text{s}}}\parallel \right._{2}^{2}}}},} $ (2)

    其中 $ {\textstyle \odot} $$ {\textstyle \sqrt{\cdot}} $ 分别表示逐元素乘法与逐元素平方根,且 $ {\textstyle \lambda^{\prime} = \frac{\lambda}{\alpha}} $

    需要注意的是,这一命题并不直接适用于实际的自适应梯度算法,因为它们会在每一步都更改预条件子矩阵。尽管如此,它仍可帮助我们直观理解每一步实际上正在优化的等效损失函数:那些反向预条件子 $ {\textstyle s_{i}} $ 较大的参数 $ {\textstyle \theta_{i}} $(在实践中通常源于第 $ {\textstyle i} $ 维度上历史梯度较大)所受到的相对正则化强度大于 L2 正则化下的情形;具体而言,正则化强度与 $ {\textstyle \sqrt{s_{i}}} $ 成正比。

    3 通过将自适应梯度方法视为贝叶斯滤波来论证解耦权重衰减

    我们现在讨论 Aitchison(2018)关于自适应梯度算法的贝叶斯滤波统一理论中对解耦权重衰减的一种论证。在我们将本文的初步版本上传至 arXiv 后,Aitchison 指出,他的理论「为我们提供了一个理论框架,可以在其中理解这种权重衰减相对于 $ {\textstyle L_{2}} $ 正则化的优越性,因为通过直接应用贝叶斯滤波得到的是权重衰减,而非 $ {\textstyle L_{2}} $ 正则化」(Aitchison,2018)。虽然这一理论的全部功劳归属于 Aitchison,但我们在此进行总结,以阐明为何权重衰减可能优于 $ {\textstyle L_{2}} $ 正则化。

    Aitchison(2018)将 $ {\textstyle n} $ 个参数 $ {\textstyle \theta_{1},\ldots,\theta_{n}} $ 的随机优化视为一个贝叶斯滤波问题,其目标是:在给定其它参数当前值 $ {\textstyle {\mathbf{θ}}_{- i}\hspace{0pt}{(t)}} $ 的条件下,在时间步 $ {\textstyle t} $ 推断每个参数 $ {\textstyle \theta_{i}} $ 最优值的分布。当其它参数不变时这是一个优化问题,而当它们变化时则变成借助贝叶斯滤波「追踪」优化器的问题,具体如下:给定时间步 $ {\textstyle t} $ 时优化器的一个概率分布 $ {\textstyle P\hspace{0pt}{({{\mathbf{θ}}_{t} \mid {\mathbf{y}}_{\mathbf{1}:{\mathbf{t}}}})}} $,它考虑了前 $ {\textstyle t} $ 个 mini batch 中的数据 $ {\textstyle {\mathbf{y}}_{\mathbf{1}:{\mathbf{t}}}} $;一个状态转移先验 $ {\textstyle P\hspace{0pt}{({{\mathbf{θ}}_{t + 1} \mid {\mathbf{θ}}_{t}})}} $,它反映该分布在相邻两步间发生的(很小的)、与数据无关的变化;以及由第 $ {\textstyle t + 1} $ 步 mini batch 导出的似然 $ {\textstyle P\hspace{0pt}{({{\mathbf{y}}_{t + 1} \mid {\mathbf{θ}}_{t + 1}})}} $。然后即可(按贝叶斯滤波的常规做法)通过对 $ {\textstyle {\mathbf{θ}}_{t}} $ 边缘化得到一步预测 $ {\textstyle P\hspace{0pt}{({{\mathbf{θ}}_{t + 1} \mid {\mathbf{y}}_{\mathbf{1}:{\mathbf{t}}}})}} $,再应用贝叶斯规则纳入似然 $ {\textstyle P\hspace{0pt}{({{\mathbf{y}}_{t + 1} \mid {\mathbf{θ}}_{t + 1}})}} $,得到时间步 $ {\textstyle t + 1} $ 时优化器的后验分布 $ {\textstyle P\hspace{0pt}{({{\mathbf{θ}}_{t + 1} \mid {\mathbf{y}}_{\mathbf{1}:{{\mathbf{t}} + \mathbf{1}}}})}} $。Aitchison(2018)假设状态转移分布 $ {\textstyle P\hspace{0pt}{({{\mathbf{θ}}_{t + 1} \mid {\mathbf{θ}}_{t}})}} $ 为高斯分布、似然 $ {\textstyle P\hspace{0pt}{({{\mathbf{y}}_{t + 1} \mid {\mathbf{θ}}_{t + 1}})}} $ 为近似共轭,从而得到滤波分布均值的下述闭式更新:

    $ {\displaystyle {{\mathbf{μ}}_{p\hspace{0pt}o\hspace{0pt}s\hspace{0pt}t} = {{\mathbf{μ}}_{p\hspace{0pt}r\hspace{0pt}i\hspace{0pt}o\hspace{0pt}r} + {\mathbf{\Sigma}_{p\hspace{0pt}o\hspace{0pt}s\hspace{0pt}t} \times {\mathbf{g}}}}},} $ (3)

    其中 $ {\textstyle \mathbf{g}} $ 是时间 $ {\textstyle t} $ 处 mini batch 的对数似然梯度。这一结果暗示了一个由滤波分布的后验不确定性 $ {\textstyle \mathbf{\Sigma}_{p\hspace{0pt}o\hspace{0pt}s\hspace{0pt}t}} $ 给出的梯度预条件子:对我们越不确定的参数,更新越大;对我们越确定的参数,更新越小。Aitchison(2018)随后表明:Adam、RMSprop 等流行的自适应梯度方法以及 Kronecker 分解类方法都是该框架的特例。

    解耦权重衰减作为状态转移分布的一部分,可以非常自然地融入这一统一框架:Aitchison(2018)假设优化器按下述高斯分布缓慢变化:

    $ {\displaystyle {{P\hspace{0pt}{({{\mathbf{θ}}_{t + 1} \mid {\mathbf{θ}}_{t}})}} = {\mathcal{N}\hspace{0pt}{({{({{\mathbf{I}} - {\mathbf{A}}})}\hspace{0pt}{\mathbf{θ}}_{t}},{\mathbf{Q}})}}},} $ (4)

    其中 $ {\textstyle \mathbf{Q}} $ 是权重所受高斯扰动的协方差,$ {\textstyle \mathbf{A}} $ 是用于防止数值随时间无限增长的正则化项。当令 $ {\textstyle {\mathbf{A}} = {\lambda \times {\mathbf{I}}}} $ 时,该正则化项 $ {\textstyle \mathbf{A}} $ 所起的作用恰好就是公式 1 中描述的解耦权重衰减,因为它会在每一步将当前均值估计 $ {\textstyle {\mathbf{θ}}_{t}} $ 乘以 $ {\textstyle ({1 - \lambda})} $。值得注意的是:这种正则化是直接作用于先验上的,并不依赖于各参数的不确定性(而这恰是 $ {\textstyle L_{2}} $ 正则化所需要的)。

    Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption

    4 实验验证

    现在我们在不同训练预算与学习率调度下评估解耦权重衰减的性能。我们的实验设置遵循 Gastaldi(2017)的工作,他在 L2 正则化之外,还提出对一个 3 分支残差 DNN 应用新的 Shake-Shake 正则化,从而在 CIFAR-10 数据集(Krizhevsky,2009)上取得了 2.86 % 的新最优结果。我们使用了基于 fb.resnet.torch 的同一模型/源码 111https://github.com/xgastaldi/shake-shake。我们始终使用 128 的 batch size,并对 CIFAR 数据集应用常规的数据增强流程。基础网络分别是 26 2x64d 的 ResNet(即网络深度为 26、有 2 条残差分支、第一个残差块的宽度为 64)和 26 2x96d 的 ResNet,参数量分别为 11.6M 和 25.6M。对网络以及 Shake-Shake 方法的详细介绍,感兴趣的读者请参阅 Gastaldi(2017)。我们还在 ImageNet32x32 数据集(Chrabaszcz 等,2017)上进行了实验,该数据集是原始 ImageNet 的下采样版本,包含 120 万张 32$ {\textstyle \times} $32 像素的图像。

    4.1 在不同学习率调度下评估解耦权重衰减

    在第一个实验中,我们使用三种不同的学习率调度——固定学习率、阶梯衰减调度,以及余弦退火调度(Loshchilov 与 Hutter,2016)——比较带 $ {\textstyle L_{2}} $ 正则化的 Adam 与带解耦权重衰减的 Adam(AdamW)。由于 Adam 已经为每个参数自适应地调整学习率,所以在 Adam 上使用学习率乘子调度不像在 SGD 上那么常见;但我们的结果显示,这类调度能大幅提升 Adam 的性能,因此我们主张不应忽视它们在自适应梯度算法中的使用。

    对于每种学习率调度和每种权重衰减变体,我们分别使用不同的初始学习率 $ {\textstyle \alpha} $ 和权重衰减系数 $ {\textstyle \lambda} $,训练一个 2x64d 的 ResNet 共 100 epoch。图 1 表明,对所有学习率调度而言,解耦权重衰减都优于 $ {\textstyle L_{2}} $ 正则化,且学习率调度越好二者差距越大。我们还观察到,解耦权重衰减带来一个更可分离的超参数搜索空间,尤其是在使用 step-drop 或余弦退火等学习率调度时。该图还表明,余弦退火明显优于其它学习率调度;因此后续实验我们均采用余弦退火。

    Refer to caption           Refer to caption

    Refer to caption           Refer to caption

    Refer to caption               Refer to caption

    Refer to caption               Refer to caption

    4.2 解耦权重衰减与初始学习率参数

    为了验证关于 $ {\textstyle \alpha} $$ {\textstyle \lambda} $ 耦合的假设,我们在图 2 中比较了 SGD(SGD vs. SGDW,上排)和 Adam(Adam vs. AdamW,下排)下 L2 正则化与解耦权重衰减的性能表现。在 SGD(图 2 左上)中,L2 正则化未与学习率解耦(如 Algorithm 1 所述的常规做法),图清楚地显示:最佳超参数所在的盆地(以颜色表示,前 10 优超参数以黑圈标记)既不与 x 轴对齐,也不与 y 轴对齐,而是落在对角线上。这表明两个超参数相互依赖、必须同时调整;只改其中之一可能会显著恶化结果。例如,考虑左上黑圈处的设置($ {\textstyle \alpha = {1/2}} $$ {\textstyle \lambda = {{1/8} \ast 0.001}} $):单独改变 $ {\textstyle \alpha} $$ {\textstyle \lambda} $ 都会让结果变差,而同时调整两者仍可获得明显改进。我们注意到,初始学习率与 L2 正则化系数之间的这种耦合,可能正是 SGD 被认为对超参数高度敏感这一名声的来源之一。

    相反,图 2 右上展示的带解耦权重衰减的 SGD(SGDW)结果表明,权重衰减与初始学习率已经解耦。所提出的方法使两个超参数更可分离:即便学习率尚未调好(例如考虑图 2 右上中 1/1024 这一取值),将其固定不动而只优化权重衰减系数,仍能得到一个不错的值(1/4*0.001)。带 L2 正则化的 SGD 则不然(参见图 2 左上)。

    Adam 配 L2 正则化的结果见图 2 左下。Adam 的最佳超参数设置明显劣于 SGD 的最佳设置(对比图 2 左上)。尽管两种方法都使用了 L2 正则化,Adam 完全没有从中受益:在非零 L2 正则化系数下取得的最佳结果,与不使用 L2 正则化(即 $ {\textstyle \lambda = 0} $)时的最佳结果相当。与原始 SGD 相似,超参数地形的形状表明两个超参数相互耦合。

    相对地,图 2 右下展示的带解耦权重衰减的 Adam 新变体(AdamW)结果表明:AdamW 在很大程度上将权重衰减与学习率解耦了。其最佳超参数配置下的结果明显优于带 L2 正则化的 Adam 的最佳结果,并可与 SGD 和 SGDW 相媲美。

    综上,图 2 的结果支持我们的假设:权重衰减与学习率这两个超参数可以解耦,进而简化 SGD 的超参数调优问题,并使 Adam 的性能提升至与带 momentum 的 SGD 相竞争的水平。

    4.3 AdamW 更好的泛化能力

    虽然前一实验已经表明 AdamW 的最优超参数盆地比 Adam 更宽更深,但接下来我们考察更长的训练(1800 epoch)的结果,以比较 AdamW 与 Adam 的泛化能力。

    我们将初始学习率固定为 0.001,这既是 Adam 的默认学习率,也是在我们的实验中表现相当不错的取值。图 3 给出了 Adam 在 12 个 L2 正则化设置下与 AdamW 在 7 个归一化权重衰减设置下的结果(归一化权重衰减表示在附录 B.1 中正式定义的一种重新缩放;它相当于一个依赖于 batch 通过次数的乘性因子)。有趣的是,在训练前半段,Adam 与 AdamW 的学习曲线动态往往是一致的,但 AdamW 通常能得到更低的训练损失与测试误差(分别参见图 3 左上和右上)。重要的是,在 Adam 中使用 L2 形式的权重衰减并未取得 AdamW 中解耦权重衰减那样好的结果(同见图 3 左下)。接着,我们研究 AdamW 更好的结果是仅由更好的收敛带来的,还是也来自更好的泛化。对 Adam 和 AdamW 的最佳设置而言,图 3 右下的结果表明:AdamW 不仅在训练损失上更优,而且在相近训练损失下也展现出更好的泛化性能。ImageNet32x32 上的结果(参见附录中的 SuppFigure 4)得出相同的结论:泛化性能获得了显著改进。

    Refer to caption               Refer to caption

    4.4 带 warm restarts 的 AdamWR:更好的任意时刻性能

    为了改进 SGDW 与 AdamW 的任意时刻性能,我们以 Loshchilov 与 Hutter(2016)中提出的 warm restarts 对它们进行扩展,分别得到 SGDWR 与 AdamWR(见附录 B.2 节)。如图 4 所示,AdamWR 在 CIFAR-10 与 ImageNet32x32 上将 AdamW 大幅加速,最高可达 10 倍(参见第一次 restart 处的结果)。在默认学习率 0.001 下,AdamW 在 CIFAR-10(也参见 SuppFigure 5与 ImageNet32x32(也参见 SuppFigure 6上的测试误差相对 Adam 都获得了 15 % 的相对改进

    AdamWR 取得了同样的改进结果,但任意时刻性能要好得多。这些改进在 CIFAR-10 上大幅缩小了 Adam 与 SGDWR 之间的差距,在 ImageNet32x32 上则取得了可比的性能。

    4.5 在其他数据集与架构上使用 AdamW

    其他多个研究组已在可被引用的工作中成功采用了 AdamW。例如,Wang 等(2018)使用 AdamW 在标准 WIDER FACE 数据集(Yang 等,2016)上训练了一种新的人脸检测架构,在与之前的最优算法性能相当的同时,预测速度提升了近 10 倍。Völker 等(2018)使用带余弦退火的 AdamW 训练卷积神经网络,对从颅内脑电(EEG)记录中测得的与错误相关的脑电信号进行分类与特征刻画。虽然他们的论文未给出与 Adam 的对比,但他们友善地提供了在其表现最佳的针对该问题专门设计的网络架构 Deep4Net 以及一种 ResNet 变体上对二者的直接对比。在使用与 Adam 相同的超参数设置下,AdamW 在 Deep4Net 上取得了更高的测试集精度(73.68% 对 71.37%),在 ResNet 上则取得了具有统计显著性的更高测试集精度(72.04% 对 61.34%)。Radford 等(2018)使用 AdamW 训练 Transformer(Vaswani 等,2017)架构,在大量自然语言理解 benchmark 上取得新的最优结果。Zhang 等(2018)在 CIFAR 数据集上以 ResNet 与 VGG 架构,对 SGD、Adam 以及 Kronecker-Factored Approximate Curvature(K-FAC)优化器(Martens 与 Grosse,2015)的 L2 正则化与权重衰减进行了比较,报告称:在二者存在差异的情形下,解耦权重衰减始终优于 L2 正则化。

    5 结论与未来工作

    鉴于已有研究指出 Adam 等自适应梯度方法的泛化可能弱于带 momentum 的 SGD(Wilson 等,2017),我们识别并揭示了对 Adam 而言 L2 正则化与权重衰减的不等价性。我们通过实证表明:我们提出的带解耦权重衰减的 Adam 版本,其泛化性能大幅优于常见的带 L2 正则化的 Adam 实现。我们还提议在 Adam 中使用 warm restarts,以改善其任意时刻的性能。

    我们在图像分类数据集上获得的结果还需在更广泛的任务上加以验证,尤其是那些预计正则化起重要作用的任务。一个值得探索的方向是将我们关于权重衰减的发现整合进其它试图改进 Adam 的方法,例如归一化方向保持的 Adam(Zhang 等,2017)。虽然我们的实验分析聚焦于 Adam,但我们相信类似的结论也适用于其它自适应梯度方法,如 AdaGrad(Duchi 等,2011)和 AMSGrad(Reddi 等,2018)。

    6 致谢

    我们感谢 Patryk Chrabaszcz 在 ImageNet32x32 实验上的协助;感谢 Matthias Feurer 与 Robin Schirrmeister 在多轮迭代中对本文提出的宝贵反馈;感谢 Martin Völker、Robin Schirrmeister 与 Tonio Ball 为我们提供了在他们的 EEG 数据上对 AdamW 与 Adam 的对比。我们也感谢深度学习社区的下列成员在不同深度学习库中实现解耦权重衰减:

    本工作由欧洲研究理事会(ERC)在欧盟 Horizon 2020 研究与创新项目下编号 716721 的资助、由德国研究基金会(DFG)通过 BrainLinksBrainTools 卓越集群(资助编号 EXC 1086)以及编号为 INST 37/935-1 FUGG 的资助、并由德国巴登-符腾堡州通过 bwHPC 提供支持。

    参考文献

    • Aitchison (2018) Laurence Aitchison. A unified theory of adaptive stochastic gradient descent as Bayesian filtering. arXiv:1507.02030, 2018. / * Chrabaszcz et al. (2017) Patryk Chrabaszcz, Ilya Loshchilov, and Frank Hutter. A downsampled variant of ImageNet as an alternative to the CIFAR datasets. arXiv:1707.08819, 2017. / * Cubuk et al. (2018) Ekin D Cubuk, Barret Zoph, Dandelion Mane, Vijay Vasudevan, and Quoc V Le. Autoaugment: Learning augmentation policies from data. arXiv preprint arXiv:1805.09501, 2018. / * Dinh et al. (2017) Laurent Dinh, Razvan Pascanu, Samy Bengio, and Yoshua Bengio. Sharp minima can generalize for deep nets. arXiv:1703.04933, 2017. / * Duchi et al. (2011) John Duchi, Elad Hazan, and Yoram Singer. Adaptive subgradient methods for online learning and stochastic optimization. The Journal of Machine Learning Research, 12:2121–2159, 2011. / * Gastaldi (2017) Xavier Gastaldi. Shake-Shake regularization. arXiv preprint arXiv:1705.07485, 2017. / * Hanson & Pratt (1988) Stephen José Hanson and Lorien Y Pratt. Comparing biases for minimal network construction with back-propagation. 见 Proceedings of the 1st International Conference on Neural Information Processing Systems, pp. 177–185, 1988. / * Huang et al. (2017) Gao Huang, Yixuan Li, Geoff Pleiss, Zhuang Liu, John E Hopcroft, and Kilian Q Weinberger. Snapshot ensembles: Train 1, get m for free. arXiv:1704.00109, 2017. / * Keskar et al. (2016) Nitish Shirish Keskar, Dheevatsa Mudigere, Jorge Nocedal, Mikhail Smelyanskiy, and Ping Tak Peter Tang. On large-batch training for deep learning: Generalization gap and sharp minima. arXiv:1609.04836, 2016. / * Kingma & Ba (2014) Diederik Kingma and Jimmy Ba. Adam: A method for stochastic optimization. arXiv:1412.6980, 2014. / * Krizhevsky (2009) Alex Krizhevsky. Learning multiple layers of features from tiny images. 2009. / * Li et al. (2017) Hao Li, Zheng Xu, Gavin Taylor, and Tom Goldstein. Visualizing the loss landscape of neural nets. arXiv preprint arXiv:1712.09913, 2017. / * Loshchilov & Hutter (2016) Ilya Loshchilov and Frank Hutter. SGDR: stochastic gradient descent with warm restarts. arXiv:1608.03983, 2016. / * Martens & Grosse (2015) James Martens and Roger Grosse. Optimizing neural networks with kronecker-factored approximate curvature. 见 International conference on machine learning, pp. 2408–2417, 2015. / * Radford et al. (2015) Alec Radford, Luke Metz, and Soumith Chintala. Unsupervised representation learning with deep convolutional generative adversarial networks. arXiv:1511.06434, 2015. / * Radford et al. (2018) Alec Radford, Karthik Narasimhan, Tim Salimans, and Ilya Sutskever. Improving language understanding by generative pre-training. URL https://s3-us-west-2. amazonaws. com/openai-assets/research-covers/language-unsupervised/language_ understanding_paper. pdf, 2018. / * Reddi et al. (2018) Sashank J. Reddi, Satyen Kale, and Sanjiv Kumar. On the convergence of adam and beyond. International Conference on Learning Representations, 2018. / * Smith (2016) Leslie N Smith. Cyclical learning rates for training neural networks. arXiv:1506.01186v3, 2016. / * Tieleman & Hinton (2012) Tijmen Tieleman and Geoffrey Hinton. Lecture 6.5-rmsprop: Divide the gradient by a running average of its recent magnitude. COURSERA: Neural networks for machine learning, 4(2):26–31, 2012. / * Vaswani et al. (2017) Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is all you need. 见 Advances in Neural Information Processing Systems, pp. 5998–6008, 2017. / * Völker et al. (2018) Martin Völker, Jiří Hammer, Robin T Schirrmeister, Joos Behncke, Lukas DJ Fiederer, Andreas Schulze-Bonhage, Petr Marusič, Wolfram Burgard, and Tonio Ball. Intracranial error detection via deep learning. arXiv preprint arXiv:1805.01667, 2018. / * Wang et al. (2018) Jianfeng Wang, Ye Yuan, Gang Yu, and Sun Jian. Sface: An efficient network for face detection in large scale variations. arXiv preprint arXiv:1804.06559, 2018. / * Wilson et al. (2017) Ashia C Wilson, Rebecca Roelofs, Mitchell Stern, Nathan Srebro, and Benjamin Recht. The marginal value of adaptive gradient methods in machine learning. arXiv:1705.08292, 2017. / * Xu et al. (2015) Kelvin Xu, Jimmy Ba, Ryan Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhudinov, Rich Zemel, and Yoshua Bengio. Show, attend and tell: Neural image caption generation with visual attention. 见 International Conference on Machine Learning, pp. 2048–2057, 2015. / * Yang et al. (2016) Shuo Yang, Ping Luo, Chen-Change Loy, and Xiaoou Tang. Wider face: A face detection benchmark. 见 Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pp. 5525–5533, 2016. / * Zhang et al. (2018) Guodong Zhang, Chaoqi Wang, Bowen Xu, and Roger Grosse. Three mechanisms of weight decay regularization. arXiv preprint arXiv:1810.12281, 2018. / * Zhang et al. (2017) Zijun Zhang, Lin Ma, Zongpeng Li, and Chuan Wu. Normalized direction-preserving adam. arXiv:1709.04546, 2017. / * Zoph et al. (2017) Barret Zoph, Vijay Vasudevan, Jonathon Shlens, and Quoc V. Le. Learning transferable architectures for scalable image recognition. 见 arXiv:1707.07012 [cs.CV], 2017.

    Appendix

    附录 A 权重衰减与 L2 正则化的形式化分析

    命题 1 的证明
    / 这一已知事实的证明非常直接。SGD 不使用权重衰减时,在 $ {\textstyle {f_{t}^{\text{reg}}\hspace{0pt}{({\mathbf{θ}})}} = {{f_{t}\hspace{0pt}{({\mathbf{θ}})}} + {\frac{\lambda^{\prime}}{2}\hspace{0pt}\left. \parallel{\mathbf{θ}}\parallel \right._{2}^{2}}}} $ 上的迭代为:

    $ {\displaystyle {{\mathbf{θ}}_{t + 1}\leftarrow{{\mathbf{θ}}_{t} - {\alpha\hspace{0pt}{\nabla f_{t}^{\text{reg}}}\hspace{0pt}{({\mathbf{θ}}_{t})}}} = {{\mathbf{θ}}_{t} - {\alpha\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}} - {\alpha\hspace{0pt}\lambda^{\prime}\hspace{0pt}{\mathbf{θ}}_{t}}}}.} $ (5)

    SGD 使用权重衰减时,在 $ {\textstyle f_{t}\hspace{0pt}{({\mathbf{θ}})}} $ 上的迭代为:

    $ {\displaystyle {{\mathbf{θ}}_{t + 1}\leftarrow{{{({1 - \lambda})}\hspace{0pt}{\mathbf{θ}}_{t}} - {\alpha\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}}}.} $ (6)

    由于 $ {\textstyle \lambda^{\prime} = \frac{\lambda}{\alpha}} $,这两组迭代完全相同。∎

    命题 2 的证明
    / 与命题 1 的证明类似,$ {\textstyle O} $$ {\textstyle {f_{t}^{\text{reg}}\hspace{0pt}{({\mathbf{θ}})}} = {{f_{t}\hspace{0pt}{({\mathbf{θ}})}} + {\frac{1}{2}\hspace{0pt}\lambda^{\prime}\hspace{0pt}\left. \parallel{\mathbf{θ}}\parallel \right._{2}^{2}}}} $ 上不使用权重衰减、以及 $ {\textstyle O} $$ {\textstyle f_{t}} $ 上使用权重衰减 $ {\textstyle \lambda} $ 时的迭代分别为:

    $ {\textstyle {\mathbf{θ}}_{t + 1}} $ $ {\textstyle \leftarrow} $ $ {\textstyle {{\mathbf{θ}}_{t} - {\alpha\hspace{0pt}\lambda^{\prime}\hspace{0pt}\mathbf{M}_{t}\hspace{0pt}{\mathbf{θ}}_{t}} - {\alpha\hspace{0pt}\mathbf{M}_{t}\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}}.} $ (7)
    $ {\textstyle {\mathbf{θ}}_{t + 1}} $ $ {\textstyle \leftarrow} $ $ {\textstyle {{{({1 - \lambda})}\hspace{0pt}{\mathbf{θ}}_{t}} - {\alpha\hspace{0pt}\mathbf{M}_{t}\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}}.} $ (8)

    若两组迭代对所有 $ {\textstyle {\mathbf{θ}}_{t}} $ 都相等,则有 $ {\textstyle {\lambda\hspace{0pt}{\mathbf{θ}}_{t}} = {\alpha\hspace{0pt}\lambda^{\prime}\hspace{0pt}\mathbf{M}_{t}\hspace{0pt}{\mathbf{θ}}_{t}}} $。这只有在 $ {\textstyle \mathbf{M}_{t} = {k\hspace{0pt}\mathbf{I}}} $$ {\textstyle k \in {\mathbb{R}}} $)时才能对所有 $ {\textstyle {\mathbf{θ}}_{t}} $ 都成立,但 $ {\textstyle O} $ 并非如此。因此不存在任何 L2 正则化项 $ {\textstyle \lambda^{\prime}\hspace{0pt}\left. \parallel{\mathbf{θ}}\parallel \right._{2}^{2}} $ 能使两组迭代等价。∎

    命题 3 的证明
    / $ {\textstyle O} $ 不使用权重衰减时,在 $ {\textstyle {f_{t}^{\text{sreg}}\hspace{0pt}{({\mathbf{θ}})}} = {{f_{t}\hspace{0pt}{({\mathbf{θ}})}} + {\frac{\lambda^{\prime}}{2}\hspace{0pt}\left. \parallel{{\mathbf{θ}} \odot \sqrt{\text{s}}}\parallel \right._{2}^{2}}}} $ 上的迭代为:

    $ {\textstyle {\mathbf{θ}}_{t + 1}} $ $ {\textstyle \leftarrow} $ $ {\textstyle {\mathbf{θ}}_{t} - {{\alpha\hspace{0pt}{\nabla f_{t}^{\text{sreg}}}\hspace{0pt}{({\mathbf{θ}}_{t})}}/\text{s}}} $ (9)
    $ {\textstyle =} $ $ {\textstyle {\mathbf{θ}}_{t} - {{\alpha\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}/\text{s}} - {{{\alpha\hspace{0pt}\lambda^{\prime}\hspace{0pt}{\mathbf{θ}}_{t}} \odot \text{s}}/\text{s}}} $ (10)
    $ {\textstyle =} $ $ {\textstyle {{\mathbf{θ}}_{t} - {{\alpha\hspace{0pt}{\nabla f_{t}}\hspace{0pt}{({\mathbf{θ}}_{t})}}/\text{s}} - {\alpha\hspace{0pt}\lambda^{\prime}\hspace{0pt}{\mathbf{θ}}_{t}}},} $ (11)

    其中除以 s 是逐元素的。$ {\textstyle O} $ 使用权重衰减时,在 $ {\textstyle f_{t}\hspace{0pt}{({\mathbf{θ}})}} $ 上的迭代为:

    $ {\textstyle {\mathbf{θ}}_{t + 1}} $ $ {\textstyle \leftarrow} $ $ {\textstyle {{({1 - \lambda})}\hspace{0pt}{\mathbf{θ}}_{t}} - {{\alpha\hspace{0pt}{\nabla f}\hspace{0pt}{({\mathbf{θ}}_{t})}}/\text{s}}} $ (12)
    $ {\textstyle =} $ $ {\textstyle {{\mathbf{θ}}_{t} - {{\alpha\hspace{0pt}{\nabla f}\hspace{0pt}{({\mathbf{θ}}_{t})}}/\text{s}} - {\lambda\hspace{0pt}{\mathbf{θ}}_{t}}},} $ (13)

    由于 $ {\textstyle \lambda^{\prime} = \frac{\lambda}{\alpha}} $,这两组迭代完全相同。∎

    附录 B Adam 的其他实践改进

    在讨论了用于改善 Adam 泛化能力的解耦权重衰减之后,本节再介绍两个用于改善 Adam 实际表现的组件。

    B.1 归一化权重衰减

    我们的初步实验表明:不同的计算预算(以 batch 通过次数定义)下,最优的权重衰减系数也不同。与此相关,Li 等(2017)证明:在总 epoch 数相同的情况下,较小的 batch size 会使权重衰减的收缩效应更为显著。这里我们提议通过对权重衰减取值进行归一化来减弱这种依赖。具体而言,我们用一个新的(更稳健的)归一化权重衰减超参数 $ {\textstyle \lambda_{n\hspace{0pt}o\hspace{0pt}r\hspace{0pt}m}} $ 取代原来的超参数 $ {\textstyle \lambda} $,并按 $ {\textstyle \lambda = {\lambda_{n\hspace{0pt}o\hspace{0pt}r\hspace{0pt}m}\hspace{0pt}\sqrt{\frac{b}{B\hspace{0pt}T}}}} $ 来设置 $ {\textstyle \lambda} $,其中 $ {\textstyle b} $ 是 batch size,$ {\textstyle B} $ 是训练样本总数,$ {\textstyle T} $ 是总 epoch 数。222在我们 B.2 节讨论的 AdamWR 变体语境中,$ {\textstyle T} $ 表示当前 restart 内的总 epoch 数。因此 $ {\textstyle \lambda_{n\hspace{0pt}o\hspace{0pt}r\hspace{0pt}m}} $ 可解释为只允许一次 batch 通过时所使用的权重衰减。我们强调,所选用的这种归一化方式只是基于少量实验得出的一种可能;我们得出的更具持久性的结论是:使用某种形式的归一化能够大幅改善结果。

    B.2 带余弦退火与 warm restarts 的 Adam

    我们现在按照近期工作(Loshchilov 与 Hutter,2016)将余弦退火和 warm restarts 应用到 Adam 上。在该工作中,我们提出了带 warm restarts 的随机梯度下降(SGDR),通过按余弦调度快速冷却学习率并周期性地将其升高,来改善 SGD 的任意时刻性能。SGDR 已被成功采用,以在多个流行的图像分类 benchmark 上取得新的最优结果(Huang 等,2017;Gastaldi,2017;Zoph 等,2017),因此我们在提出后不久便尝试将其推广到 Adam。然而,尽管我们最初版本的带 warm restarts 的 Adam 任意时刻性能优于普通 Adam,但与带 warm restarts 的 SGD 相比并不具竞争力——原因正是 L2 正则化在它身上不像在 SGD 中那样有效。如今,通过原始权重衰减正则化(第 2 节)解决了这一问题,并引入了归一化权重衰减(B.1 节),我们关于余弦退火与 warm restarts 的原始工作便可以直接迁移到 Adam 上。

    为保持论述自洽,我们简要说明 SGDR 如何调度有效学习率的变化,以加速 DNN 的训练。这里,我们将初始学习率 $ {\textstyle \alpha} $ 与用于在迭代 $ {\textstyle t} $ 时得到实际学习率的乘子 $ {\textstyle \eta_{t}} $ 解耦(例如参见 Algorithm 1 第 8 行)。在 SGDR 中,每完成 $ {\textstyle T_{i}} $ 个 epoch 我们就模拟一次新的 warm-start SGD 运行/restart,其中 $ {\textstyle i} $ 是该次运行的索引。需要强调的是,这种 restart 并非从零开始,而是通过提高 $ {\textstyle \eta_{t}} $ 同时使用旧的 $ {\textstyle {\mathbf{θ}}_{t}} $ 作为初始解来模拟。$ {\textstyle \eta_{t}} $ 的提升幅度控制了之前获取的信息(例如 momentum)被使用的程度。在第 $ {\textstyle i} $ 次运行内部,$ {\textstyle \eta_{t}} $ 按照余弦退火的学习率(Loshchilov 与 Hutter,2016)逐 batch 衰减,如下式所示:

    $ {\textstyle {\eta_{t} = {\eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)} + {0.5\hspace{0pt}{({\eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)} - \eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}})}\hspace{0pt}{({1 + {\cos{({{\pi\hspace{0pt}T_{c\hspace{0pt}u\hspace{0pt}r}}/T_{i}})}}})}}}},} $ (14)

    其中 $ {\textstyle \eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}} $$ {\textstyle \eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)}} $ 是乘子的取值范围,$ {\textstyle T_{c\hspace{0pt}u\hspace{0pt}r}} $ 表示自上次 restart 以来已完成的 epoch 数。$ {\textstyle T_{c\hspace{0pt}u\hspace{0pt}r}} $ 在每个 batch 迭代 $ {\textstyle t} $ 处更新,因此不限于整数值。在每次第 $ {\textstyle i} $ 次 restart 时调整(例如减小)$ {\textstyle \eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)}} $$ {\textstyle \eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)}} $(亦可参见 Smith(2016))有可能进一步改善性能,但此处我们不考虑这一选项,以避免引入额外的超参数。当 $ {\textstyle \eta_{m\hspace{0pt}a\hspace{0pt}x}^{(i)} = 1} $$ {\textstyle \eta_{m\hspace{0pt}i\hspace{0pt}n}^{(i)} = 0} $ 时,可将公式(14)简化为

    $ {\textstyle {\eta_{t} = {0.5 + {0.5\hspace{0pt}{\cos{({{\pi\hspace{0pt}T_{c\hspace{0pt}u\hspace{0pt}r}}/T_{i}})}}}}}.} $ (15)

    为获得良好的任意时刻性能,可以从一个初始较小的 $ {\textstyle T_{i}} $(例如总预算的 1 % 到 10 %)开始,并在每次 restart 时将其乘以 $ {\textstyle T_{m\hspace{0pt}u\hspace{0pt}l\hspace{0pt}t}} $(例如 $ {\textstyle T_{m\hspace{0pt}u\hspace{0pt}l\hspace{0pt}t} = 2} $)。当 $ {\textstyle T_{c\hspace{0pt}u\hspace{0pt}r} = T_{i}} $ 时通过将 $ {\textstyle T_{c\hspace{0pt}u\hspace{0pt}r}} $ 置 0 触发第 $ {\textstyle ({i + 1})} $ 次 restart。调度乘子的一个示例设置见附录 C

    我们提出的 AdamWR 算法即为 AdamW(参见 Algorithm 2)配合按公式(15)取值的 $ {\textstyle \eta_{t}} $,并在每次迭代时按 B.1 节描述的归一化权重衰减计算 $ {\textstyle \lambda} $。我们注意到,归一化权重衰减使我们能够在 AdamWR 与 SGDWR(带 warm restarts 的 SGDW)的短运行与长运行中使用恒定的参数设置。

    附录 C 调度乘子设置示例

    在 SuppFigure 1 中给出了 $ {\textstyle T_{i = 0} = 100} $$ {\textstyle T_{m\hspace{0pt}u\hspace{0pt}l\hspace{0pt}t} = 2} $ 时乘子 $ {\textstyle \eta_{t}} $ 的一个调度示例。在最初的 100 个 epoch 之后,由于 $ {\textstyle \eta_{t = 100} = 0} $,学习率会降为 0。之后由于 $ {\textstyle T_{c\hspace{0pt}u\hspace{0pt}r} = T_{i = 0}} $,我们通过将 $ {\textstyle T_{c\hspace{0pt}u\hspace{0pt}r}} $ 重置为 0 来 restart,按公式(15)将乘子 $ {\textstyle \eta_{t}} $ 重置为 1。该乘子随后会再次从 1 减小到 0,但这次跨越 200 个 epoch,因为 $ {\textstyle T_{i = 1} = {T_{i = 0}\hspace{0pt}T_{m\hspace{0pt}u\hspace{0pt}l\hspace{0pt}t}} = 200} $。在每次 restart 之前 $ {\textstyle \eta_{t} = 0} $ 时所得的解(例如 SuppFigure 1 中所示的第 100、300、700 与 1500 epoch 处)被优化器推荐为可选解,并以最新的解优先。

    Refer to caption

    附录 D 其他结果

    我们考察了:让「标准 Adam」(带 L2 正则化和固定学习率的 Adam)执行更长的训练(1800 epoch)是否会让余弦退火变得不必要。SuppFigure 2 在 4×4 的对数超参数网格上给出了标准 Adam 的结果(网格之所以稀疏,是因为 1800 epoch 的运行计算开销很大)。即使考虑到网格分辨率较低,其结果至多也只能与 AdamW 在 epoch 数减少为 1/18、网络更小的情形下取得的结果相当(参见 SuppFigure 3 上排中间)。考虑到正文图 1(同时展示了使用余弦退火等学习率调度可能带来的改进以及解耦权重衰减的有效性)的内容,这一结论并不令人意外。

    我们对 Adam 与 SGD 的实验结果表明:以 epoch 数衡量的总运行时长会影响最优超参数所在的盆地(参见 SuppFigure 3)。更具体地说,总 epoch 数越多,权重衰减的取值就应越小。SuppFigure 4 表明:我们针对该问题的解决方案——公式(15)所定义的归一化权重衰减——简化了超参数选择,因为在短训练运行下观察到的最优值与远更长运行下的最优值相近。我们用 CIFAR-10 上的初步实验来提出公式(15)中给出的平方根归一化方法,并在 ImageNet32x32 数据集(Chrabaszcz 等,2017)上再次验证它并非偶然——该数据集是原始 ImageNet 的下采样版本,包含 120 万张 32$ {\textstyle \times} $32 像素的图像,其中一个 epoch 比 CIFAR-10 上的长 24 倍。这一实验同样支持平方根缩放:在 CIFAR-10 上观察到的最优归一化权重衰减取值,对 ImageNet32x32 而言也几乎是最优的(参见 SuppFigure 3)。相反,如果我们对 ImageNet32x32 和 CIFAR-10 在相同 epoch 数下使用相同的原始权重衰减取值 $ {\textstyle \lambda} $那么在没有所提归一化的情形下,$ {\textstyle \lambda} $ 对 ImageNet32x32 而言会大约偏大 5 倍,从而导致性能显著变差。SGDW 与 AdamW 之间的最优归一化权重衰减取值也十分接近(例如 $ {\textstyle \lambda_{n\hspace{0pt}o\hspace{0pt}r\hspace{0pt}m} = 0.025} $$ {\textstyle \lambda_{n\hspace{0pt}o\hspace{0pt}r\hspace{0pt}m} = 0.05} $)。这些结果清楚地表明:归一化权重衰减可大幅改善性能;尽管平方根缩放在我们的实验中表现非常好,但我们也强调:这些实验并非非常全面,很可能还存在更好的缩放规则。

    SuppFigure 4 是正文图 3 在 ImageNet32x32 上的对应版本(而非 CIFAR-10)。定性结论一致:权重衰减能取得比 L2 正则化更优的训练损失(cross-entropy),并在测试误差上带来更大的改善。

    SuppFigure 5 与 SuppFigure 6 是正文图 4 的对应版本,并在底排补充了训练损失曲线。结果表明:在 CIFAR-10 上,Adam 及其带解耦权重衰减的变体在训练损失上的收敛比对应的 SGD 变体更快(在 ImageNet32x32 上差异较小)。如正文所述,当训练损失值相同时,AdamW 的测试误差比 Adam 更低。有趣的是,SuppFigure 5 与 SuppFigure 6 表明:带 restart 的变体 AdamWR 与 SGDWR 在泛化方面也分别优于 AdamW 与 SGDW。

    Refer to caption

    Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption Refer to caption

    Refer to caption    Refer to caption
      
    Refer to caption    Refer to caption

    Refer to caption Refer to caption

    Refer to caption Refer to caption