[{"data":1,"prerenderedAt":872},["ShallowReactive",2],{"content-query-EnTaKzcZT3":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":866,"_id":867,"_source":868,"_file":869,"_stem":870,"_extension":871},"/technology-blogs/zh/350","zh",false,"","MindSpore大V博文之高阶优化器系列（一）","本文先跟大家分享下常用的一二阶优化器。","2020-12-18","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/12/18/a7d5e24782f841d399404461c9e18de5.png","technology-blogs","大V博文",{"type":15,"children":16,"toc":851},"root",[17,25,31,44,55,62,67,78,83,93,98,107,148,155,160,168,177,205,210,215,223,229,234,247,281,286,303,310,317,324,331,373,381,389,394,399,406,455,508,540,573,581,586,591,596,601,608,613,618,625,630,637,642,649,654,659,664,669,674,726,737,742,752,762,772,782,792,799,804,811,816,821,826,831,836,841,846],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore大v博文之高阶优化器系列一",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":30},"作者：于璠",{"type":18,"tag":26,"props":32,"children":33},{},[34,36],{"type":24,"value":35},"作者主页：",{"type":18,"tag":37,"props":38,"children":42},"a",{"href":39,"rel":40},"https://www.zhihu.com/people/yu-fan-42-9",[41],"nofollow",[43],{"type":24,"value":39},{"type":18,"tag":26,"props":45,"children":46},{},[47,49],{"type":24,"value":48},"原文链接：",{"type":18,"tag":37,"props":50,"children":53},{"href":51,"rel":52},"https://zhuanlan.zhihu.com/p/337187451",[41],[54],{"type":24,"value":51},{"type":18,"tag":26,"props":56,"children":57},{},[58],{"type":18,"tag":37,"props":59,"children":61},{"href":51,"rel":60},[41],[],{"type":18,"tag":26,"props":63,"children":64},{},[65],{"type":24,"value":66},"创新算法系列已经介绍完啦，接下来给大家分享下高阶优化器系列，想要看前一个系列的小伙伴们戳官网新闻的技术博客页面哦。",{"type":18,"tag":26,"props":68,"children":69},{},[70,72],{"type":24,"value":71},"地址：",{"type":18,"tag":37,"props":73,"children":76},{"href":74,"rel":75},"https://www.mindspore.cn/news/technologyBlog",[41],[77],{"type":24,"value":74},{"type":18,"tag":26,"props":79,"children":80},{},[81],{"type":24,"value":82},"本文先跟大家分享下常用的一二阶优化器。深度学习训练过程可以看成是loss下降过程，而每一次loss如何下降，就是优化器做的事情。合适的优化器可以让深度学习训练时间大大减少。优化器可以分为一阶优化器和二阶优化器，深度学习中常用的优化器是一阶优化器，如SGD（Stochastic Gradient Descent）和其算法变种，二阶优化器近年来也被用于深度学习训练，并取得了不错的结果。接下来我们就具体展开来看一下。",{"type":18,"tag":84,"props":85,"children":87},"h2",{"id":86},"背景",[88],{"type":18,"tag":89,"props":90,"children":91},"strong",{},[92],{"type":24,"value":86},{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":24,"value":97},"首先我们来看一下深度学习训练过程是在做什么事。先假设训练样本数据集：",{"type":18,"tag":26,"props":99,"children":100},{},[101],{"type":18,"tag":102,"props":103,"children":106},"img",{"alt":104,"src":105},"[公式]","https://www.zhihu.com/equation?tex=D+%3D+%7B%28x_1%2Cy_1%29%2C...%2C%28x_i%2Cy_i%29%2C...%2C%28x_N%2Cy_N%29%7D%2Cx_i+%5Cin+X%2Cy_i%5Cin+Y%5C%5C",[],{"type":18,"tag":26,"props":108,"children":109},{},[110,112,116,118,122,124,128,130,134,136,140,142,146],{"type":24,"value":111},"参数 ",{"type":18,"tag":102,"props":113,"children":115},{"alt":104,"src":114},"https://www.zhihu.com/equation?tex=%5Ctheta",[],{"type":24,"value":117}," 表述的深度神经网络模型为： ",{"type":18,"tag":102,"props":119,"children":121},{"alt":104,"src":120},"https://www.zhihu.com/equation?tex=%5Chat+y+%3D+f%28x%3B%5Ctheta%29%2Cx%5Cin+X%2C++",[],{"type":24,"value":123}," 定义在模型输出 ",{"type":18,"tag":102,"props":125,"children":127},{"alt":104,"src":126},"https://www.zhihu.com/equation?tex=%5Chat+y",[],{"type":24,"value":129}," 和真实标签 ",{"type":18,"tag":102,"props":131,"children":133},{"alt":104,"src":132},"https://www.zhihu.com/equation?tex=y",[],{"type":24,"value":135}," 之间的损失函数为： ",{"type":18,"tag":102,"props":137,"children":139},{"alt":104,"src":138},"https://www.zhihu.com/equation?tex=L%28y%2C%5Chat+y%29%2Cy%5Cin+Y%2C",[],{"type":24,"value":141}," 网络参数学习的过程是最小化损失函数的过程： ",{"type":18,"tag":102,"props":143,"children":145},{"alt":104,"src":144},"https://www.zhihu.com/equation?tex=%5Cunderset%7B%5Ctheta%7D+%7B%5Cmathrm+min%7DL%28y%2C%5Chat+y%29.",[],{"type":24,"value":147}," 给定数据集、模型、损失函数后，深度学习训练问题归结为优化问题，深度神经网络训练优化问题参数规模巨大，需要显著大量的计算，难以计算出解析解。因此该过程也常常被比喻成下山，如下图所示，一个人站在山顶的时候如何在有限视距内寻找最快路径下山呢？",{"type":18,"tag":26,"props":149,"children":150},{},[151],{"type":18,"tag":102,"props":152,"children":154},{"alt":7,"src":153},"https://pic3.zhimg.com/80/v2-b41babb510cdc022a1837ed1649ab2f2_720w.jpg",[],{"type":18,"tag":26,"props":156,"children":157},{},[158],{"type":24,"value":159},"这个时候就是优化器大展身手了，好的优化器可以以更少的时间下山。业界的优化算法可分为一阶优化算法和二阶优化算法。下面我们就一起来看下常用的一阶优化器和二阶优化器。",{"type":18,"tag":84,"props":161,"children":163},{"id":162},"一阶优化器",[164],{"type":18,"tag":89,"props":165,"children":166},{},[167],{"type":24,"value":162},{"type":18,"tag":169,"props":170,"children":172},"h3",{"id":171},"简介",[173],{"type":18,"tag":89,"props":174,"children":175},{},[176],{"type":24,"value":171},{"type":18,"tag":26,"props":178,"children":179},{},[180,182,186,188,191,193,197,199,203],{"type":24,"value":181},"梯度下降算法（Gradient Descent, GD）是机器学习中最经典的一阶优化算法，也是众多机器学习算法中最常用的优化算法。常用的一阶优化算法（比如SGD算法）中对参数的更新采用如下规则： ",{"type":18,"tag":102,"props":183,"children":185},{"alt":104,"src":184},"https://www.zhihu.com/equation?tex=%5Ctheta+%3D+%5Ctheta+-%5Ceta+%5Cnabla+L_%5Ctheta%2C",[],{"type":24,"value":187}," 其中 ",{"type":18,"tag":102,"props":189,"children":190},{"alt":104,"src":114},[],{"type":24,"value":192}," 是需要更新的参数， ",{"type":18,"tag":102,"props":194,"children":196},{"alt":104,"src":195},"https://www.zhihu.com/equation?tex=%5Ceta",[],{"type":24,"value":198}," 是学习率， ",{"type":18,"tag":102,"props":200,"children":202},{"alt":104,"src":201},"https://www.zhihu.com/equation?tex=%5Cnabla+L_%5Ctheta",[],{"type":24,"value":204}," 是损失函数对于参数的梯度。",{"type":18,"tag":26,"props":206,"children":207},{},[208],{"type":24,"value":209},"但是主流随机梯度下降方法有以下问题：太小的学习率会导致网络收敛过于缓慢；学习率太大可能会影响收敛，并导致损失函数在最小值上波动，甚至出现发散，对超参比较敏感；容易收敛到局部最优，难以跳出鞍点。",{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":24,"value":214},"因此业界提出了很多随机梯度下降方法的改良算法，例如Momentum、Nesterov、AdaGrad、RMSprop、Adadelta和Adam等。这些改进后的优化算法可以利用随机梯度的历史信息来自适应地更新步长，使得它们更容易调参，而且方便使用。下面简单介绍下现在业界常用的一阶优化器Momentum和Adam。",{"type":18,"tag":169,"props":216,"children":218},{"id":217},"常用一阶优化器",[219],{"type":18,"tag":89,"props":220,"children":221},{},[222],{"type":24,"value":217},{"type":18,"tag":169,"props":224,"children":226},{"id":225},"momentum",[227],{"type":24,"value":228},"Momentum",{"type":18,"tag":26,"props":230,"children":231},{},[232],{"type":24,"value":233},"Momentum是一阶优化算法，它对SGD进行了改进，在梯度下降的过程中加入了惯性，使得梯度在方向不变的维度上速度变快，梯度方向有所改变的维度上的更新速度变慢，与SGD相比可以加快收敛并减少震荡。其更新规则如下：",{"type":18,"tag":26,"props":235,"children":236},{},[237,241,243],{"type":18,"tag":102,"props":238,"children":240},{"alt":104,"src":239},"https://www.zhihu.com/equation?tex=v_t+%3D+%5Cgamma+v_%7Bt-1%7D%2B%5Ceta_t%5Cnabla+L_%5Ctheta+%5C%5C",[],{"type":24,"value":242}," ",{"type":18,"tag":102,"props":244,"children":246},{"alt":104,"src":245},"https://www.zhihu.com/equation?tex=%5Ctheta_t+%3D+%5Ctheta_%7Bt-1%7D-v_t+%5C%5C",[],{"type":18,"tag":26,"props":248,"children":249},{},[250,254,256,260,261,265,267,271,273,276,277,280],{"type":18,"tag":102,"props":251,"children":253},{"alt":104,"src":252},"https://www.zhihu.com/equation?tex=%5Cgamma",[],{"type":24,"value":255}," 是动量超参， ",{"type":18,"tag":102,"props":257,"children":259},{"alt":104,"src":258},"https://www.zhihu.com/equation?tex=0%5Cleq%5Cgamma+%3C1%2C",[],{"type":24,"value":242},{"type":18,"tag":102,"props":262,"children":264},{"alt":104,"src":263},"https://www.zhihu.com/equation?tex=%5Ceta_t",[],{"type":24,"value":266},"为在第 ",{"type":18,"tag":102,"props":268,"children":270},{"alt":104,"src":269},"https://www.zhihu.com/equation?tex=t",[],{"type":24,"value":272}," 个step时的学习率， ",{"type":18,"tag":102,"props":274,"children":275},{"alt":104,"src":114},[],{"type":24,"value":192},{"type":18,"tag":102,"props":278,"children":279},{"alt":104,"src":201},[],{"type":24,"value":204},{"type":18,"tag":26,"props":282,"children":283},{},[284],{"type":24,"value":285},"Adam",{"type":18,"tag":26,"props":287,"children":288},{},[289,291,295,297,301],{"type":24,"value":290},"Adam也是一种自适应学习率的方法，该算法使用了动量变量 ",{"type":18,"tag":102,"props":292,"children":294},{"alt":104,"src":293},"https://www.zhihu.com/equation?tex=v_t",[],{"type":24,"value":296}," 和一阶梯度的指数加权移动平均变量 ",{"type":18,"tag":102,"props":298,"children":300},{"alt":104,"src":299},"https://www.zhihu.com/equation?tex=s_t%2C",[],{"type":24,"value":302}," 并在初始时将他们置为0，其更新规则如下：",{"type":18,"tag":26,"props":304,"children":305},{},[306],{"type":18,"tag":102,"props":307,"children":309},{"alt":104,"src":308},"https://www.zhihu.com/equation?tex=v_t+%3D+%5Cbeta_1+v_%7Bt-1%7D%2B%281-%5Cbeta_1%29%5Cnabla+L_%7B%5Ctheta%7D%5C%5C",[],{"type":18,"tag":26,"props":311,"children":312},{},[313],{"type":18,"tag":102,"props":314,"children":316},{"alt":104,"src":315},"https://www.zhihu.com/equation?tex=s_t%3D%5Cbeta_2s_%7Bt-1%7D%2B%281-%5Cbeta_2%29%28%5Cnabla+L_%7B%5Ctheta%7D%29%5E2%5C%5C",[],{"type":18,"tag":26,"props":318,"children":319},{},[320],{"type":18,"tag":102,"props":321,"children":323},{"alt":104,"src":322},"https://www.zhihu.com/equation?tex=%5Chat%7Bv%7D_t+%3D+%5Cfrac%7Bv_t%7D%7B1-%5Cbeta_1%5Et%7D%2C%5Chat%7Bs%7D_t+%3D+%5Cfrac%7Bs_t%7D%7B1-%5Cbeta_2%5Et%7D%5C%5C",[],{"type":18,"tag":26,"props":325,"children":326},{},[327],{"type":18,"tag":102,"props":328,"children":330},{"alt":104,"src":329},"https://www.zhihu.com/equation?tex=%5Ctheta_t+%3D+%5Ctheta_%7Bt-1%7D-%5Cfrac%7B%5Ceta_t%5Chat+v_t%7D%7B%5Csqrt%7B%5Chat+s_t%7D%2B%5Cvarepsilon%7D%5C%5C",[],{"type":18,"tag":26,"props":332,"children":333},{},[334,336,340,342,346,348,351,353,356,357,360,361,365,367,371],{"type":24,"value":335},"其中 ",{"type":18,"tag":102,"props":337,"children":339},{"alt":104,"src":338},"https://www.zhihu.com/equation?tex=%5Cbeta_1",[],{"type":24,"value":341}," 和 ",{"type":18,"tag":102,"props":343,"children":345},{"alt":104,"src":344},"https://www.zhihu.com/equation?tex=%5Cbeta_2",[],{"type":24,"value":347}," 为超参，通常取为0.9和0.999， ",{"type":18,"tag":102,"props":349,"children":350},{"alt":104,"src":263},[],{"type":24,"value":352}," 为在第 ",{"type":18,"tag":102,"props":354,"children":355},{"alt":104,"src":269},[],{"type":24,"value":272},{"type":18,"tag":102,"props":358,"children":359},{"alt":104,"src":114},[],{"type":24,"value":192},{"type":18,"tag":102,"props":362,"children":364},{"alt":104,"src":363},"https://www.zhihu.com/equation?tex=%5Cnabla+L_%7B%5Ctheta%7D",[],{"type":24,"value":366}," 是损失函数对于参数的梯度， ",{"type":18,"tag":102,"props":368,"children":370},{"alt":104,"src":369},"https://www.zhihu.com/equation?tex=%5Cvarepsilon",[],{"type":24,"value":372}," 是一个很小的数，一般为1e-8，为了避免分母为0。",{"type":18,"tag":84,"props":374,"children":376},{"id":375},"二阶优化器",[377],{"type":18,"tag":89,"props":378,"children":379},{},[380],{"type":24,"value":375},{"type":18,"tag":169,"props":382,"children":384},{"id":383},"简介-1",[385],{"type":18,"tag":89,"props":386,"children":387},{},[388],{"type":24,"value":171},{"type":18,"tag":26,"props":390,"children":391},{},[392],{"type":24,"value":393},"二阶优化算法利用目标函数的二阶导数进行曲率校正来加速一阶梯度下降。与一阶优化器相比，其收敛速度更快，能高度逼近最优值，几何上下降路径也更符合真实的最优下降路径。",{"type":18,"tag":26,"props":395,"children":396},{},[397],{"type":24,"value":398},"举个栗子，二阶优化算法中的牛顿法就是用一个二次曲面去拟合你当前所处位置的局部曲面，而梯度下降法是用一个平面去拟合当前的局部曲面，通常情况下，二次曲面的拟合会比平面更好，所以牛顿法选择的下降路径会更符合真实的最优下降路径。如下图所示，红线表示牛顿法的下降曲线，绿线表示一阶梯度的下降曲线，二阶算法与一阶算法先比，可以更快的走到目的地，从而加速收敛。",{"type":18,"tag":26,"props":400,"children":401},{},[402],{"type":18,"tag":102,"props":403,"children":405},{"alt":7,"src":404},"https://pic1.zhimg.com/80/v2-788b64f380462a2a3cd96368050ca00c_720w.jpg",[],{"type":18,"tag":26,"props":407,"children":408},{},[409,411,414,416,420,422,426,427,431,433,436,438,441,443,447,449,453],{"type":24,"value":410},"从数学公式上来看，与一阶优化算法相比，二阶优化算法则是先将 ",{"type":18,"tag":102,"props":412,"children":413},{"alt":104,"src":363},[],{"type":24,"value":415}," 与一个矩阵 ",{"type":18,"tag":102,"props":417,"children":419},{"alt":104,"src":418},"https://www.zhihu.com/equation?tex=G%5E%7B-1%7D",[],{"type":24,"value":421}," 相乘，参数更新规则如下： ",{"type":18,"tag":102,"props":423,"children":425},{"alt":104,"src":424},"https://www.zhihu.com/equation?tex=%5Ctheta+%3D+%5Ctheta+-%5Ceta+G%5E%7B-1%7D%5Cnabla+L_%7B%5Ctheta%7D%2C",[],{"type":24,"value":187},{"type":18,"tag":102,"props":428,"children":430},{"alt":104,"src":429},"https://www.zhihu.com/equation?tex=G",[],{"type":24,"value":432}," 即为二阶信息矩阵，不同的二阶优化算法中的 ",{"type":18,"tag":102,"props":434,"children":435},{"alt":104,"src":429},[],{"type":24,"value":437}," 定义是不同的，常见的二阶优化算法有牛顿法，自然梯度法等，分别对应的二阶信息矩阵 ",{"type":18,"tag":102,"props":439,"children":440},{"alt":104,"src":429},[],{"type":24,"value":442}," 为 ",{"type":18,"tag":102,"props":444,"children":446},{"alt":104,"src":445},"https://www.zhihu.com/equation?tex=%5Cmathrm+Hessian",[],{"type":24,"value":448}," 矩阵， ",{"type":18,"tag":102,"props":450,"children":452},{"alt":104,"src":451},"https://www.zhihu.com/equation?tex=%5Cmathrm+Fisher",[],{"type":24,"value":454}," 矩阵。",{"type":18,"tag":26,"props":456,"children":457},{},[458,460,464,466,470,472,476,478,481,483,486,488,491,493,497,498,501,503,506],{"type":24,"value":459},"牛顿法有着很好的局部收敛性质，当函数 ",{"type":18,"tag":102,"props":461,"children":463},{"alt":104,"src":462},"https://www.zhihu.com/equation?tex=L",[],{"type":24,"value":465}," 在最优值点 ",{"type":18,"tag":102,"props":467,"children":469},{"alt":104,"src":468},"https://www.zhihu.com/equation?tex=%5Ctheta%5E%2A",[],{"type":24,"value":471}," 满足 ",{"type":18,"tag":102,"props":473,"children":475},{"alt":104,"src":474},"https://www.zhihu.com/equation?tex=%5Cnabla+L_%7B%5Ctheta%5E%2A%7D%3D0%2C%5Cnabla+%5E2L_%7B%5Ctheta%5E%2A%7D",[],{"type":24,"value":477},"是正定矩阵且 ",{"type":18,"tag":102,"props":479,"children":480},{"alt":104,"src":445},[],{"type":24,"value":482}," 矩阵在极值点附近是李普希兹连续时，牛顿法二次收敛到最优值点。 ",{"type":18,"tag":102,"props":484,"children":485},{"alt":104,"src":445},[],{"type":24,"value":487}," 矩阵是一个由多变量实值函数的所有二阶偏导数组成的方块矩阵。 ",{"type":18,"tag":102,"props":489,"children":490},{"alt":104,"src":445},[],{"type":24,"value":492}," 矩阵可以表示为: ",{"type":18,"tag":102,"props":494,"children":496},{"alt":104,"src":495},"https://www.zhihu.com/equation?tex=H_%7Bij%7D+%3D+%5Cfrac%7B%5Cpartial%5E2L%7D%7B%5Cpartial+%5Ctheta_i+%5Cpartial+%5Ctheta_j%7D",[],{"type":24,"value":187},{"type":18,"tag":102,"props":499,"children":500},{"alt":104,"src":462},[],{"type":24,"value":502}," 即为损失函数， ",{"type":18,"tag":102,"props":504,"children":505},{"alt":104,"src":114},[],{"type":24,"value":507}," 是需要更新的参数。",{"type":18,"tag":26,"props":509,"children":510},{},[511,513,516,518,522,523,527,529,533,535,538],{"type":24,"value":512},"在SGD中，参数空间和函数空间的度量用的都是欧式距离，但欧式距离在一些情况下不能作为函数空间准确的距离度量。例如神经网络中，参数引起的目标函数变化是概率的变化，这并不适合在欧几里得空间度量，它不是概率属性变化的合理表征。KL散度是分布之间距离的合理度量。当使用KL散度作为概率分布之间距离的度量时。此时参数更新时，用到的梯度就是自然梯度。自然梯度法中的 ",{"type":18,"tag":102,"props":514,"children":515},{"alt":104,"src":451},[],{"type":24,"value":517}," 矩阵可以表示为： ",{"type":18,"tag":102,"props":519,"children":521},{"alt":104,"src":520},"https://www.zhihu.com/equation?tex=F%3D%5Cmathrm%7BE%7D%5B%5Cfrac%7B%5Cpartial+%5Cmathrm+%7Blog%7D+p%28y%7Cx%2C%5Ctheta%29%7D%7B%5Cpartial+%5Ctheta%7D%7B%5Cfrac%7B%5Cpartial+%5Cmathrm+%7Blog%7D+p%28y%7Cx%2C%5Ctheta%29%7D%7B%5Cpartial+%5Ctheta%7D%7D%5ET%5D%2C",[],{"type":24,"value":187},{"type":18,"tag":102,"props":524,"children":526},{"alt":104,"src":525},"https://www.zhihu.com/equation?tex=P%28y%7Cx%2C%5Ctheta%29",[],{"type":24,"value":528}," 是网络模型的预测分布， ",{"type":18,"tag":102,"props":530,"children":532},{"alt":104,"src":531},"https://www.zhihu.com/equation?tex=p%28y%7Cx%2C%5Ctheta%29",[],{"type":24,"value":534}," 是其概率密度， ",{"type":18,"tag":102,"props":536,"children":537},{"alt":104,"src":114},[],{"type":24,"value":539}," 是需要网络模型的参数。",{"type":18,"tag":26,"props":541,"children":542},{},[543,545,549,550,554,556,560,562,566,568,571],{"type":24,"value":544},"二阶优化算法虽然收敛速度快，但是计算二阶矩阵的逆的时间复杂度为 ",{"type":18,"tag":102,"props":546,"children":548},{"alt":104,"src":547},"https://www.zhihu.com/equation?tex=%5Cmathrm+O%28n%5E3%29%2C",[],{"type":24,"value":187},{"type":18,"tag":102,"props":551,"children":553},{"alt":104,"src":552},"https://www.zhihu.com/equation?tex=n",[],{"type":24,"value":555}," 为二阶信息矩阵维度，当模型参数量为 ",{"type":18,"tag":102,"props":557,"children":559},{"alt":104,"src":558},"https://www.zhihu.com/equation?tex=n_%5Ctheta",[],{"type":24,"value":561}," 时，对应的二阶信息矩阵的大小为 ",{"type":18,"tag":102,"props":563,"children":565},{"alt":104,"src":564},"https://www.zhihu.com/equation?tex=n_%5Ctheta+%5Ctimes+n_%5Ctheta",[],{"type":24,"value":567}," 。在深度学习模型中， ",{"type":18,"tag":102,"props":569,"children":570},{"alt":104,"src":558},[],{"type":24,"value":572}," 常常在数百万的量级，此时二阶信息矩阵的逆无法计算。因此如何降低二阶信息矩阵求逆的计算复杂度成为关键问题。",{"type":18,"tag":169,"props":574,"children":576},{"id":575},"深度学习中的二阶优化器",[577],{"type":18,"tag":89,"props":578,"children":579},{},[580],{"type":24,"value":575},{"type":18,"tag":26,"props":582,"children":583},{},[584],{"type":24,"value":585},"KFAC",{"type":18,"tag":26,"props":587,"children":588},{},[589],{"type":24,"value":590},"KFAC（Kronecker-factored Approximate Curvature）是由Martens等学者于2015年提出的基于自然梯度法的二阶优化算法，该算法高效地近似了自然梯度法中的Fisher矩阵，并且给出了充分的理论证明，给后续许多二阶优化算法带来了启发。这里重点跟大家分享下他是如何做近似的，具体的理论推导建议大家看论文哦。",{"type":18,"tag":26,"props":592,"children":593},{},[594],{"type":24,"value":595},"第一步，KFAC假设不同层之间的参数是独立的，从而将二阶信息矩阵按网络层解耦，近似成块对角矩阵，然后对二阶信息矩阵求逆就变为了对这些块对角矩阵求逆。",{"type":18,"tag":26,"props":597,"children":598},{},[599],{"type":24,"value":600},"第二步将按层解耦后的每个块矩阵又近似为两个小得多的矩阵的Kronecker乘积。我们一起来回顾下Kronecker乘积公式：",{"type":18,"tag":26,"props":602,"children":603},{},[604],{"type":18,"tag":102,"props":605,"children":607},{"alt":104,"src":606},"https://www.zhihu.com/equation?tex=%5Cbegin%7Bbmatrix%7D+++b_%7B11%7D%26b_%7B12%7D%5C%5C++b_%7B21%7D%26b_%7B22%7D%5C%5C++%5Cend%7Bbmatrix%7D%5Cotimes%5Cbegin%7Bbmatrix%7D+++c_%7B11%7D%26c_%7B12%7D%26c_%7B13%7D%5C%5C++c_%7B21%7D%26c_%7B22%7D%26c_%7B23%7D%5C%5C++c_%7B31%7D%26c_%7B32%7D%26c_%7B33%7D%5C%5C+%5Cend%7Bbmatrix%7D%3D%5Cleft+%5B%5Cbegin%7Barray%7D+%7Bccc%7Cccc%7D++b_%7B11%7Dc_%7B11%7D%26b_%7B11%7Dc_%7B12%7D%26b_%7B11%7Dc_%7B13%7D%26+b_%7B12%7Dc_%7B11%7D%26b_%7B12%7Dc_%7B12%7D%26b_%7B12%7Dc_%7B13%7D%5C%5C++b_%7B11%7Dc_%7B21%7D%26b_%7B11%7Dc_%7B22%7D%26b_%7B11%7Dc_%7B23%7D%26+b_%7B12%7Dc_%7B21%7D%26b_%7B12%7Dc_%7B22%7D%26b_%7B12%7Dc_%7B23%7D%5C%5C++b_%7B11%7Dc_%7B31%7D%26b_%7B11%7Dc_%7B32%7D%26b_%7B11%7Dc_%7B33%7D%26+b_%7B12%7Dc_%7B31%7D%26b_%7B12%7Dc_%7B32%7D%26b_%7B12%7Dc_%7B33%7D%5C%5C++%5Chline+b_%7B21%7Dc_%7B11%7D%26b_%7B21%7Dc_%7B12%7D%26b_%7B21%7Dc_%7B13%7D%26+b_%7B22%7Dc_%7B11%7D%26b_%7B22%7Dc_%7B12%7D%26b_%7B22%7Dc_%7B13%7D%5C%5C++b_%7B21%7Dc_%7B21%7D%26b_%7B21%7Dc_%7B22%7D%26b_%7B21%7Dc_%7B23%7D%26+b_%7B22%7Dc_%7B21%7D%26b_%7B22%7Dc_%7B22%7D%26b_%7B22%7Dc_%7B23%7D%5C%5C++b_%7B21%7Dc_%7B31%7D%26b_%7B21%7Dc_%7B32%7D%26b_%7B21%7Dc_%7B33%7D%26+b_%7B22%7Dc_%7B31%7D%26b_%7B22%7Dc_%7B32%7D%26b_%7B22%7Dc_%7B33%7D%5C%5C+%5Cend%7Barray%7D%5Cright%5D+%5C%5C",[],{"type":18,"tag":26,"props":609,"children":610},{},[611],{"type":24,"value":612},"由于两个矩阵的Kronecker乘积的逆等于它们逆的Kronecker乘积，且这两个较小矩阵会比整个块矩阵更容易计算和求逆，通过这样的近似和分解，KFAC大大简化了Fisher矩阵的计算。",{"type":18,"tag":26,"props":614,"children":615},{},[616],{"type":24,"value":617},"上面这段话还不够直观，不急，我们通过一个栗子来看一下。比如对于一个3-4-2的神经网络，",{"type":18,"tag":26,"props":619,"children":620},{},[621],{"type":18,"tag":102,"props":622,"children":624},{"alt":7,"src":623},"https://pic2.zhimg.com/80/v2-f6737423e515f657486614bad4d608d1_720w.jpg",[],{"type":18,"tag":26,"props":626,"children":627},{},[628],{"type":24,"value":629},"原始二阶信息矩阵维度为20*20，经过第一步近似变为了一个12*12和8*8的矩阵，再经过第二步近似，分别变为了3*3和4*4；4*4和2*2维度的矩阵。如下图所示：",{"type":18,"tag":26,"props":631,"children":632},{},[633],{"type":18,"tag":102,"props":634,"children":636},{"alt":7,"src":635},"https://pic3.zhimg.com/80/v2-04164be7a631eaa60a6ec224ad04084a_720w.jpg",[],{"type":18,"tag":26,"props":638,"children":639},{},[640],{"type":24,"value":641},"该算法在CIFAR-10上做了实验，使用cuda-convnet网络，该网络包含3层卷积层和一层全连接层，下图中的蓝线为SGD with Momentum，红线为KFAC（图中的KFC-pre即为KFAC），实线是测试误差，虚线是训练误差。KFAC达到19%的测试误差仅需3分钟，SGD需要9分钟；在训练误差上KFAC的优势更加明显，KFAC达到6%仅需4分钟，SGD需要30分钟。",{"type":18,"tag":26,"props":643,"children":644},{},[645],{"type":18,"tag":102,"props":646,"children":648},{"alt":7,"src":647},"https://pic1.zhimg.com/80/v2-9086c576380c63e2edf155c45ba653fc_720w.jpg",[],{"type":18,"tag":26,"props":650,"children":651},{},[652],{"type":24,"value":653},"KFAC使自然梯度法应用到了深度学习训练中，并取得了很好的结果，但是KFAC未考虑大型神经网络计算复杂度，例如对于大型神经网络ResNet50，全连接层维度为2048*1000，按照KFAC算法分解后，仍然要计算一个2048维和一个1000维的逆矩阵，求逆时间虽然大大降低，但是依然很慢，在ResNet50模型，ImageNet全集上，单次迭代所需时间是秒级别，单迭代所需时间是一阶的33倍。",{"type":18,"tag":26,"props":655,"children":656},{},[657],{"type":24,"value":658},"SP-NGD",{"type":18,"tag":26,"props":660,"children":661},{},[662],{"type":24,"value":663},"SP-NGD(Scalable and Practical Natural Gradient Descent)是由Osawa等学者于2020年提出的基于自然梯度法的二阶优化器，并在大数据集ImageNet上训练ResNet50，使用1024块V100，仅需5.5min即可收敛到75.4%。",{"type":18,"tag":26,"props":665,"children":666},{},[667],{"type":24,"value":668},"SP-NGD受KFAC启发，在KFAC基础上主要做了以下几点改进。",{"type":18,"tag":26,"props":670,"children":671},{},[672],{"type":24,"value":673},"第一点，SP-NGD使用了过时的二阶信息矩阵，核心思想是，在训练过程中不是每个迭代都更新二阶信息矩阵的逆，而是使用上一次更新保存下来的二阶信息矩阵的逆，当二阶信息矩阵变化量超过一定阈值时减少二阶信息矩阵逆的更新间隔，当变化量持续不变，增大二阶信息矩阵逆的更新间隔从而减少计算量。",{"type":18,"tag":26,"props":675,"children":676},{},[677,679,683,685,689,691,695,697,701,702,706,708,712,714,718,720,724],{"type":24,"value":678},"大致过程是判断当前更新的二阶信息矩阵 ",{"type":18,"tag":102,"props":680,"children":682},{"alt":104,"src":681},"https://www.zhihu.com/equation?tex=X",[],{"type":24,"value":684}," 与上一次的 ",{"type":18,"tag":102,"props":686,"children":688},{"alt":104,"src":687},"https://www.zhihu.com/equation?tex=X_%7B-1%7D",[],{"type":24,"value":690}," 是否相似，其中文中定义两个矩阵 ",{"type":18,"tag":102,"props":692,"children":694},{"alt":104,"src":693},"https://www.zhihu.com/equation?tex=A%2CB",[],{"type":24,"value":696}," 相似为： ",{"type":18,"tag":102,"props":698,"children":700},{"alt":104,"src":699},"https://www.zhihu.com/equation?tex=%7C%7CA-B%7C%7C_F%2F%7C%7CB%7C%7C_F+%3C+%5Calpha%2C",[],{"type":24,"value":187},{"type":18,"tag":102,"props":703,"children":705},{"alt":104,"src":704},"https://www.zhihu.com/equation?tex=%7C%7C%5Ccdot%7C%7C_F++",[],{"type":24,"value":707}," 是 ",{"type":18,"tag":102,"props":709,"children":711},{"alt":104,"src":710},"https://www.zhihu.com/equation?tex=%5Cmathrm%7BFrobenius%7D",[],{"type":24,"value":713}," 范数， ",{"type":18,"tag":102,"props":715,"children":717},{"alt":104,"src":716},"https://www.zhihu.com/equation?tex=%5Calpha+",[],{"type":24,"value":719}," 是设定的阈值，若相似再判断与上上次的二阶信息矩阵 ",{"type":18,"tag":102,"props":721,"children":723},{"alt":104,"src":722},"https://www.zhihu.com/equation?tex=X_%7B-2%7D",[],{"type":24,"value":725}," 是否相似，若不相似，更新间隔保持不变，若相似则增大下次更新间隔。具体计算规则见下图：",{"type":18,"tag":26,"props":727,"children":728},{},[729,733],{"type":18,"tag":102,"props":730,"children":732},{"alt":7,"src":731},"https://pic3.zhimg.com/80/v2-f6eecfeeff35531b97e4de14ba1ed65e_720w.jpg",[],{"type":18,"tag":102,"props":734,"children":736},{"alt":7,"src":735},"https://pic3.zhimg.com/80/v2-a5a0810166184b24fc8027883be1f3d6_720w.jpg",[],{"type":18,"tag":26,"props":738,"children":739},{},[740],{"type":24,"value":741},"第二点，为了更高效的进行分布式训练，提出了数据和模型混合并行，减少了通讯时间，具体做法是：",{"type":18,"tag":26,"props":743,"children":744},{},[745,750],{"type":18,"tag":89,"props":746,"children":747},{},[748],{"type":24,"value":749},"Stage1.",{"type":24,"value":751}," 给每块GPU分配不同的数据集，做数据并行，每块GPU根据分配到的数据集计算前向过程和前向过程中二阶信息矩阵需要的信息A。",{"type":18,"tag":26,"props":753,"children":754},{},[755,760],{"type":18,"tag":89,"props":756,"children":757},{},[758],{"type":24,"value":759},"Stage2.",{"type":24,"value":761}," 对A进行归并，归并时使用ReduceScatterV，而不是常规使用的AllReduce，这个操作是指每个GPU只归并指定层中的A，将数据并行转为模型并行，并且在A通讯的时候同时计算反向过程，进一步减少计算时间。",{"type":18,"tag":26,"props":763,"children":764},{},[765,770],{"type":18,"tag":89,"props":766,"children":767},{},[768],{"type":24,"value":769},"Stage3.",{"type":24,"value":771}," 对反向过程中计算得到的相关信息也使用ReduceScatterV做归并。这时每块GPU有指定层的完整信息。",{"type":18,"tag":26,"props":773,"children":774},{},[775,780],{"type":18,"tag":89,"props":776,"children":777},{},[778],{"type":24,"value":779},"Stage4.",{"type":24,"value":781}," 每块GPU分别计算指定层的二阶信息矩阵的逆，并更新网络参数。",{"type":18,"tag":26,"props":783,"children":784},{},[785,790],{"type":18,"tag":89,"props":786,"children":787},{},[788],{"type":24,"value":789},"Stage5.",{"type":24,"value":791}," 最后对网络参数进行AllGatherV，同步所有GPU中的参数信息。",{"type":18,"tag":26,"props":793,"children":794},{},[795],{"type":18,"tag":102,"props":796,"children":798},{"alt":7,"src":797},"https://pic4.zhimg.com/80/v2-029fbc9cf80b943ace1aac63f8c080ef_720w.jpg",[],{"type":18,"tag":26,"props":800,"children":801},{},[802],{"type":24,"value":803},"文中还有其他的改进点，比如对BN层的二阶梯度的特殊处理，如何做数据增强，如何更好地调参等， 这里我就不具体展开啦，有兴趣的小伙伴可以去看论文哦。接下来，我们来看下这个算法的实验结果，在ImageNet上训练ResNet50，使用1024块V100，仅需5.5min即可收敛到75.4%，该算法使二阶优化器在大数据集和大型网络中与一阶优化器比较也取得了有竞争力的结果。",{"type":18,"tag":26,"props":805,"children":806},{},[807],{"type":18,"tag":102,"props":808,"children":810},{"alt":7,"src":809},"https://pic2.zhimg.com/80/v2-4bcb8ac980952d910f714a88dd386c0d_720w.jpg",[],{"type":18,"tag":26,"props":812,"children":813},{},[814],{"type":24,"value":815},"到这里，这篇文章想要分享的内容就结束啦，如果有什么不对之处欢迎大家批评指正。",{"type":18,"tag":26,"props":817,"children":818},{},[819],{"type":24,"value":820},"最后再做个预告，下一篇跟大家分享MindSpore自研二阶优化器THOR，该优化器在8块昇腾芯片下仅需66.7分钟就可以让ImageNet+ResNet50收敛到75.9%，当使用256块时，仅需2.7分钟。",{"type":18,"tag":26,"props":822,"children":823},{},[824],{"type":24,"value":825},"参考文献：",{"type":18,"tag":26,"props":827,"children":828},{},[829],{"type":24,"value":830},"[1] Qian N. On the momentum term in gradient descent learning algorithms[J]. Neural networks, 1999, 12(1): 145-151.",{"type":18,"tag":26,"props":832,"children":833},{},[834],{"type":24,"value":835},"[2] Kingma D P, Ba J. Adam: A method for stochastic optimization[J]. arXiv preprint arXiv:1412.6980, 2014.",{"type":18,"tag":26,"props":837,"children":838},{},[839],{"type":24,"value":840},"[3] Martens J, Grosse R. Optimizing neural networks with kronecker-factored approximate curvature[C]//International conference on machine learning. 2015: 2408-2417.",{"type":18,"tag":26,"props":842,"children":843},{},[844],{"type":24,"value":845},"[4] Grosse R, Martens J. A kronecker-factored approximate fisher matrix for convolution layers[C]//International Conference on Machine Learning. 2016: 573-582.",{"type":18,"tag":26,"props":847,"children":848},{},[849],{"type":24,"value":850},"[5] Osawa K, Tsuji Y, Ueno Y, et al. Scalable and Practical Natural Gradient for Large-Scale Deep Learning[J]. arXiv preprint arXiv:2002.06015, 2020.",{"title":7,"searchDepth":852,"depth":852,"links":853},4,[854,856,862],{"id":86,"depth":855,"text":86},2,{"id":162,"depth":855,"text":162,"children":857},[858,860,861],{"id":171,"depth":859,"text":171},3,{"id":217,"depth":859,"text":217},{"id":225,"depth":859,"text":228},{"id":375,"depth":855,"text":375,"children":863},[864,865],{"id":383,"depth":859,"text":171},{"id":575,"depth":859,"text":575},"markdown","content:technology-blogs:zh:350.md","content","technology-blogs/zh/350.md","technology-blogs/zh/350","md",1776506130285]