[{"data":1,"prerenderedAt":933},["ShallowReactive",2],{"content-query-PmF9uQFZ1z":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":927,"_id":928,"_source":929,"_file":930,"_stem":931,"_extension":932},"/technology-blogs/zh/1540","zh",false,"","【自动微分原理二】自动微分的正反模式","AI框架离不开核心功能自动微分，而自动微分则是分为前向微分和后向微分两种实现模式，不同的实现模式有不同的机制和计算逻辑","2022-06-10","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/13/b6192e6350484012bc9cff852714319e.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":907},"root",[17,25,40,51,57,62,67,101,106,111,116,121,126,135,145,150,174,179,184,189,196,208,213,220,225,232,237,244,249,254,261,266,271,276,283,301,308,313,320,326,331,336,343,348,355,360,367,372,379,384,389,400,405,415,420,425,430,437,442,449,454,459,465,470,477,482,487,492,505,510,523,529,534,539,544,549,554,559,566,571,576,581,588,593,600,612,619,629,634,639,652,657,662,667,674,679,686,691,698,703,708,713,718,731,736,749,754,759,765,770,775,780,785,790,802,807,812,817,822,827,832,837,842,847,852,857,862,873,884,895],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"自动微分原理二自动微分的正反模式",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29,31],{"type":24,"value":30},"作者：",{"type":18,"tag":32,"props":33,"children":37},"a",{"href":34,"rel":35},"https://www.zhihu.com/people/ZOMII",[36],"nofollow",[38],{"type":24,"value":39},"ZOMI酱",{"type":18,"tag":26,"props":41,"children":42},{},[43,45],{"type":24,"value":44},"来源：",{"type":18,"tag":32,"props":46,"children":49},{"href":47,"rel":48},"https://zhuanlan.zhihu.com/p/518296942",[36],[50],{"type":24,"value":47},{"type":18,"tag":52,"props":53,"children":55},"h2",{"id":54},"自动微分的两种模式",[56],{"type":24,"value":54},{"type":18,"tag":26,"props":58,"children":59},{},[60],{"type":24,"value":61},"上一篇文章我们简单了解了计算机中常用的几种微分方式。",{"type":18,"tag":26,"props":63,"children":64},{},[65],{"type":24,"value":66},"本章将深入介绍AI框架离不开的核心功能自动微分，而自动微分则是分为前向微分和后向微分两种实现模式，不同的实现模式有不同的机制和计算逻辑，而无论哪种模式都离不开雅克比矩阵，所以我们也会深入了解一下雅克比矩阵的原理。",{"type":18,"tag":68,"props":69,"children":70},"ul",{},[71,82,91],{"type":18,"tag":72,"props":73,"children":74},"li",{},[75],{"type":18,"tag":32,"props":76,"children":79},{"href":77,"rel":78},"https://zhuanlan.zhihu.com/p/518198564",[36],[80],{"type":24,"value":81},"【自动微分原理】01. 自动微分的原理介绍",{"type":18,"tag":72,"props":83,"children":84},{},[85],{"type":18,"tag":32,"props":86,"children":88},{"href":47,"rel":87},[36],[89],{"type":24,"value":90},"【自动微分原理】02. 自动微分的正反模式",{"type":18,"tag":72,"props":92,"children":93},{},[94],{"type":18,"tag":32,"props":95,"children":98},{"href":96,"rel":97},"https://zhuanlan.zhihu.com/p/520065656",[36],[99],{"type":24,"value":100},"【自动微分原理】03. 自动微分的具体实现",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":24,"value":105},"雅克比矩阵",{"type":18,"tag":26,"props":107,"children":108},{},[109],{"type":24,"value":110},"在向量微积分中，Jacobian矩阵是一阶偏导数以一定方式排列成的矩阵，其行列式称为Jacobian行列式。Jacobian矩阵的重要性在于它体现了一个可微方程与给出点的最优线性逼近。",{"type":18,"tag":26,"props":112,"children":113},{},[114],{"type":24,"value":115},"Jacobian矩阵表示两个向量所有可能的偏导数。它是一个向量相对于另一个向量的梯度，其实现的是 n维向量 到 m 维向量的映射。",{"type":18,"tag":26,"props":117,"children":118},{},[119],{"type":24,"value":120},"在矢量运算中，Jacobian矩阵是基于函数对所有变量一阶偏导数的数值矩阵，当输入个数等于输出个数时又称为Jacobian行列式。",{"type":18,"tag":26,"props":122,"children":123},{},[124],{"type":24,"value":125},"假设输入向量 ∈ ，而输出向量 ∈ ，则Jacobian矩阵定义为：",{"type":18,"tag":26,"props":127,"children":128},{},[129],{"type":18,"tag":130,"props":131,"children":134},"img",{"alt":132,"src":133},"[公式]","https://www.zhihu.com/equation?tex=+J_f%3D+%5Cleft%5B++%5Cbegin%7Bmatrix%7D++++%5Cdfrac%7B%5Cdelta+y_1%7D%7B%5Cdelta+x_1%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cdelta+y_1%7D%7B%5Cdelta+x_n%7D+%5C%5C++++%5Cvdots+%26+%5Cddots+%26+%5Cvdots+%5C%5C++++%5Cdfrac%7B%5Cdelta+y_m%7D%7B%5Cdelta+x_1%7D+%26+%5Cvdots+%26+%5Cdfrac%7B%5Cdelta+y_m%7D%7B%5Cdelta+x_n%7D+++%5Cend%7Bmatrix%7D+++%5Cright%5D+%5Ctag%7B1%7D+",[],{"type":18,"tag":136,"props":137,"children":139},"h3",{"id":138},"微分计算模式",[140],{"type":18,"tag":141,"props":142,"children":143},"strong",{},[144],{"type":24,"value":138},{"type":18,"tag":26,"props":146,"children":147},{},[148],{"type":24,"value":149},"根据对分解后的基本操作求导和链式规则组合顺序的不同，自动微分可以分为两种模式：",{"type":18,"tag":68,"props":151,"children":152},{},[153,164],{"type":18,"tag":72,"props":154,"children":155},{},[156,162],{"type":18,"tag":157,"props":158,"children":159},"em",{},[160],{"type":24,"value":161},"前向模式",{"type":24,"value":163},"（Forward Automatic Differentiation，也叫做 tangent mode AD）或者前向累积梯度（前向模式）",{"type":18,"tag":72,"props":165,"children":166},{},[167,172],{"type":18,"tag":157,"props":168,"children":169},{},[170],{"type":24,"value":171},"反向模式",{"type":24,"value":173},"（Reverse Automatic Differentiation，也叫做 adjoint mode AD）或者说反向累计梯度（反向模式）。",{"type":18,"tag":136,"props":175,"children":177},{"id":176},"计算模式区别",[178],{"type":24,"value":176},{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":24,"value":183},"两种自动微分模式都通过递归方式来求 dy/dx，只不过根据链式法则展开的形式不太一样。",{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":188},"前向梯度累积会指定从内到外的链式法则遍历路径，即先计算 dw_1/dx，再计算 dw_2/dw_1，最后计算 dy/dw_2，即，前向模式是在计算图前向传播的同时计算微分。因此前向模式的一次正向传播就可以计算出输出值和导数值。",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":18,"tag":130,"props":193,"children":195},{"alt":132,"src":194},"https://www.zhihu.com/equation?tex=+%5Cfrac%7Bdw_i%7D%7Bdx%7D%3D%5Cfrac%7Bdw_i%7D%7Bdw_%7Bi-1%7D%7D%5Cfrac%7Bdw_%7Bi-1%7D%7D%7Bdx%7D+%5Ctag%7B2%7D+",[],{"type":18,"tag":26,"props":197,"children":198},{},[199,201,206],{"type":24,"value":200},"反向梯度累积正好相反，它会先计算dy/dw_2，然后计算 dw_2/dw_1，最后计算dw_1/dx。这是最为熟悉的反向传播模式，它非常符合",{"type":18,"tag":141,"props":202,"children":203},{},[204],{"type":24,"value":205},"沿模型误差反向传播",{"type":24,"value":207},"这一直观思路。",{"type":18,"tag":26,"props":209,"children":210},{},[211],{"type":24,"value":212},"即，反向模式需要对计算图进行一次正向计算， 得出输出值，再进行反向传播。反向模式需要保存正向传播的中间变量值（比如 ），这些中间变量数值在反向传播时候被用来计算导数，所以反向模式的内存开销要大。",{"type":18,"tag":26,"props":214,"children":215},{},[216],{"type":18,"tag":130,"props":217,"children":219},{"alt":132,"src":218},"https://www.zhihu.com/equation?tex=+%5Cfrac%7Bdy%7D%7Bdw_i%7D%3D%5Cfrac%7Bdy%7D%7Bdw_%7Bi%2B1%7D%7D%5Cfrac%7Bdw_%7Bi%2B1%7D%7D%7Bdw_i%7D+%5Ctag%7B3%7D+",[],{"type":18,"tag":26,"props":221,"children":222},{},[223],{"type":24,"value":224},"即如图所示，前向自动微分（tangent mode AD）和后向自动微分（adjoint mode AD）分别计算了Jacobian矩阵的一列和一行。",{"type":18,"tag":26,"props":226,"children":227},{},[228],{"type":18,"tag":130,"props":229,"children":231},{"alt":7,"src":230},"https://pic1.zhimg.com/80/v2-67026f652f1c00ad662fd9240c4e8e30_720w.jpg",[],{"type":18,"tag":26,"props":233,"children":234},{},[235],{"type":24,"value":236},"前向自动微分（tangent mode AD）可以在一次程序计算中通过链式法则，得到：",{"type":18,"tag":26,"props":238,"children":239},{},[240],{"type":18,"tag":130,"props":241,"children":243},{"alt":132,"src":242},"https://www.zhihu.com/equation?tex=+%5Cfrac%7B%5Cdelta+x%5Ek%7D%7B%5Cdelta+x%5E0_j%7D%3D+%5Cfrac%7B%5Cdelta+x%5Ek%7D%7B%5Cdelta+x%5E%7Bk-1%7D%7D+%5Cfrac%7B%5Cdelta+%5E%7Bk-1%7D%7D%7B%5Cdelta+x%5E0_j%7D+%5Ctag%7B4%7D+",[],{"type":18,"tag":26,"props":245,"children":246},{},[247],{"type":24,"value":248},"递推得到Jacobian矩阵中与单个输入有关的参数，即Jacobian矩阵的一列。",{"type":18,"tag":26,"props":250,"children":251},{},[252],{"type":24,"value":253},"后向自动微分（adjoint mode AD）利用链式法则，得到：",{"type":18,"tag":26,"props":255,"children":256},{},[257],{"type":18,"tag":130,"props":258,"children":260},{"alt":132,"src":259},"https://www.zhihu.com/equation?tex=+%5Cfrac%7B%5Cdelta+x%5EL_i%7D%7B%5Cdelta+x%5Ek%7D%3D+%5Cfrac%7B%5Cdelta+x%5EL_i%7D%7B%5Cdelta+x%5E%7Bk%2B1%7D%7D+%5Cfrac%7B%5Cdelta+%5E%7Bk%2B1%7D%7D%7B%5Cdelta+x%5Ek%7D+%5Ctag%7B5%7D+",[],{"type":18,"tag":26,"props":262,"children":263},{},[264],{"type":24,"value":265},"可以仅通过一次对计算过程的遍历得到Jacobian矩阵的一行。但它的导数链式法则传递方向和程序执行方向相反，所以需要在程序计算过程中记录一些额外的信息来辅助求导，这些辅助信息包括计算图和计算过程的中间变量。",{"type":18,"tag":136,"props":267,"children":269},{"id":268},"样例",[270],{"type":24,"value":268},{"type":18,"tag":26,"props":272,"children":273},{},[274],{"type":24,"value":275},"我们以公式为例，首先把它转换成一个计算图：",{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":18,"tag":130,"props":280,"children":282},{"alt":132,"src":281},"https://www.zhihu.com/equation?tex=+f%28x1%2Cx2%29%3Dln%28x1%29%2Bx1x2%E2%88%92sin%28x2%29+%5Ctag%7B5%7D+",[],{"type":18,"tag":68,"props":284,"children":285},{},[286,291,296],{"type":18,"tag":72,"props":287,"children":288},{},[289],{"type":24,"value":290},"输入变量 ：自变量维度为 n，这里 n = 2，输入变量就是 x1, x2",{"type":18,"tag":72,"props":292,"children":293},{},[294],{"type":24,"value":295},"中间变量 ：中间变量这里是 v-1到 v5，在计算过程中，只需要针对这些中间变量做处理即可。将符号微分法应用于最基本的算子，然后代入数值，保留中间结果，最后再应用于整个函数",{"type":18,"tag":72,"props":297,"children":298},{},[299],{"type":24,"value":300},"输出变量 ：假设输出变量维度为 m，这里 m = 1，输出变量就是 y1，也就是f(x1,x2)",{"type":18,"tag":26,"props":302,"children":303},{},[304],{"type":18,"tag":130,"props":305,"children":307},{"alt":7,"src":306},"https://pic4.zhimg.com/80/v2-fb6ff95bc882ec75bdb6c35e2f9e370f_720w.jpg",[],{"type":18,"tag":26,"props":309,"children":310},{},[311],{"type":24,"value":312},"转化成如上DAG（有向无环图）结构之后，我们可以很容易分步计算函数的值，并求取它每一步的导数值，然后，我们把 df/dx_1 求导过程利用链式法则表示成如下的形式：",{"type":18,"tag":26,"props":314,"children":315},{},[316],{"type":18,"tag":130,"props":317,"children":319},{"alt":132,"src":318},"https://www.zhihu.com/equation?tex=+%5Cdfrac%7Bdf%7D%7Bdx_1%7D%3D+%5Cdfrac%7Bdv_%7B-1%7D%7D%7Bdx_1%7D+%5Ccdot+%28%5Cdfrac%7Bdv_%7B1%7D%7D%7Bdv_%7B-1%7D%7D+%5Ccdot+%5Cdfrac%7Bdv_%7B4%7D%7D%7Bdv_%7B1%7D%7D+%2B+%5Cdfrac%7Bdv_%7B2%7D%7D%7Bdv_%7B-1%7D%7D+%5Ccdot+%5Cdfrac%7Bdv_%7B4%7D%7D%7Bdx_%7B2%7D%7D%29+%5Ccdot+%5Cdfrac%7Bdv_%7B5%7D%7D%7Bdv_%7B4%7D%7D+%5Ccdot+%5Cdfrac%7Bdf%7D%7Bdv_%7B5%7D%7D+%5Ctag%7B6%7D+",[],{"type":18,"tag":136,"props":321,"children":323},{"id":322},"前向模式-foward-mode",[324],{"type":24,"value":325},"前向模式 Foward Mode",{"type":18,"tag":26,"props":327,"children":328},{},[329],{"type":24,"value":330},"前向模式从计算图的起点开始，沿着计算图边的方向依次向前计算，最终到达计算图的终点。它根据自变量的值计算出计算图中每个节点的值 以及其导数值，并保留中间结果。一直得到整个函数的值和其导数值。整个过程对应于一元复合函数求导时从最内层逐步向外层求导。",{"type":18,"tag":26,"props":332,"children":333},{},[334],{"type":24,"value":335},"同样，以公式为例子：",{"type":18,"tag":26,"props":337,"children":338},{},[339],{"type":18,"tag":130,"props":340,"children":342},{"alt":132,"src":341},"https://www.zhihu.com/equation?tex=+f%28x1%2Cx2%29%3Dln%28x1%29%2Bx1x2%E2%88%92sin%28x2%29+%5Ctag%7B7%7D+",[],{"type":18,"tag":26,"props":344,"children":345},{},[346],{"type":24,"value":347},"面是前向模式的计算过程，下表中，左半部分是从左往右每个图节点的求值结果和计算过程，右半部分是每个节点对 x1的求导结果和计算过程。这里 ˙ 表示 对 1的偏导数。即：",{"type":18,"tag":26,"props":349,"children":350},{},[351],{"type":18,"tag":130,"props":352,"children":354},{"alt":132,"src":353},"https://www.zhihu.com/equation?tex=+%5Cdot%7Bv_i%7D%3D%5Cdfrac%7B%5Cdelta+v_i%7D%7B%5Cdelta+x_1%7D+%5Ctag%7B8%7D+",[],{"type":18,"tag":26,"props":356,"children":357},{},[358],{"type":24,"value":359},"在该示例中，我们希望计算函数在x_1=2, x_2=5处的导数dy/dx1，即：",{"type":18,"tag":26,"props":361,"children":362},{},[363],{"type":18,"tag":130,"props":364,"children":366},{"alt":132,"src":365},"https://www.zhihu.com/equation?tex=+%5Cdot%7By_j%7D%3D%5Cdfrac%7B%5Cdelta+y_j%7D%7B%5Cdelta+x_i%7D+%5Ctag%7B9%7D+",[],{"type":18,"tag":26,"props":368,"children":369},{},[370],{"type":24,"value":371},"可以看出，左侧是源程序分解后得到的基本操作集合，而右侧则是每一个基本操作根据已知的求导规则和链式法则由上至下计算的求导结果。",{"type":18,"tag":26,"props":373,"children":374},{},[375],{"type":18,"tag":130,"props":376,"children":378},{"alt":7,"src":377},"https://pic4.zhimg.com/80/v2-3bad6b9ebc5bed4703afb831482b9bcb_720w.jpg",[],{"type":18,"tag":136,"props":380,"children":382},{"id":381},"计算过程",[383],{"type":24,"value":381},{"type":18,"tag":26,"props":385,"children":386},{},[387],{"type":24,"value":388},"根据上图左边的Forward Primal Trace直接计算公式，对于节点数值的计算如下：",{"type":18,"tag":390,"props":391,"children":392},"ol",{},[393],{"type":18,"tag":72,"props":394,"children":395},{},[396],{"type":18,"tag":130,"props":397,"children":399},{"alt":7,"src":398},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/13/4f1b71c960664392b32514c022811852.png",[],{"type":18,"tag":26,"props":401,"children":402},{},[403],{"type":24,"value":404},"此时，已经得到了图中所有节点的数值。自动微分正向模式中（上图右边Forward Tangent Trace），在计算节点数值的同时，也一起计算导数，假设求 $\\delta y/ \\delta x_1$，则是从输入开始计算。",{"type":18,"tag":390,"props":406,"children":407},{},[408],{"type":18,"tag":72,"props":409,"children":410},{},[411],{"type":18,"tag":130,"props":412,"children":414},{"alt":7,"src":413},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/13/2599b3becea44bde9619c22db9ccf419.png",[],{"type":18,"tag":26,"props":416,"children":417},{},[418],{"type":24,"value":419},"从计算过程来看啦，自动微分的前向模式实际上与我们在微积分里所学的求导过程一致。",{"type":18,"tag":136,"props":421,"children":423},{"id":422},"雅克比-向量矩阵",[424],{"type":24,"value":422},{"type":18,"tag":26,"props":426,"children":427},{},[428],{"type":24,"value":429},"把上述过程当做雅克比矩阵求解问题，假设一个函数有 n 个输入变量 $x_i$，m个输入变量 $y_j$，即输入向量 $x \\in R^n, y \\in R^m$，则这个的映射是：",{"type":18,"tag":26,"props":431,"children":432},{},[433],{"type":18,"tag":130,"props":434,"children":436},{"alt":132,"src":435},"https://www.zhihu.com/equation?tex=+f%3AR%5En+%5Cto+R%5Em+%5Ctag%7B10%7D+",[],{"type":18,"tag":26,"props":438,"children":439},{},[440],{"type":24,"value":441},"在这种情况下，每个自动微分的前向传播计算时候，初始输入被设置为 $\\dot{x_i}=1$，其余被设置为 0。对应Jacobian矩阵定义为：",{"type":18,"tag":26,"props":443,"children":444},{},[445],{"type":18,"tag":130,"props":446,"children":448},{"alt":132,"src":447},"https://www.zhihu.com/equation?tex=+J_f%3D+%5Cleft%5B++%5Cbegin%7Bmatrix%7D++++%5Cdfrac%7B%5Cdelta+y_1%7D%7B%5Cdelta+x_1%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cdelta+y_1%7D%7B%5Cdelta+x_n%7D+%5C%5C++++%5Cvdots+%26+%5Cddots+%26+%5Cvdots+%5C%5C++++%5Cdfrac%7B%5Cdelta+y_m%7D%7B%5Cdelta+x_1%7D+%26+%5Cvdots+%26+%5Cdfrac%7B%5Cdelta+y_m%7D%7B%5Cdelta+x_n%7D+++%5Cend%7Bmatrix%7D+++%5Cright%5D+%5Ctag%7B11%7D+",[],{"type":18,"tag":26,"props":450,"children":451},{},[452],{"type":24,"value":453},"一次前向计算，可以求出Jacobian矩阵的一列数据，如 $\\dot{x_3}=1$ 对应就可以求出来第3列。tangent mode AD可以在一次程序计算中，通过链式法则递推得到Jacobian矩阵中与单个输入有关的部分，即Jacobian矩阵的一列。",{"type":18,"tag":26,"props":455,"children":456},{},[457],{"type":24,"value":458},"如下图所示，如果想用正向模式求对所有输入的导数，需要计算 n 次才能求出所有列。",{"type":18,"tag":26,"props":460,"children":461},{},[462],{"type":18,"tag":130,"props":463,"children":464},{"alt":7,"src":230},[],{"type":18,"tag":26,"props":466,"children":467},{},[468],{"type":24,"value":469},"进一步，设置 $\\dot{x}=r$，可以在一次前向传播中直接计算 Jacobian–vector 乘积：",{"type":18,"tag":26,"props":471,"children":472},{},[473],{"type":18,"tag":130,"props":474,"children":476},{"alt":132,"src":475},"https://www.zhihu.com/equation?tex=+J_f+%5Ccdot+r%3D+%5Cleft%5B++%5Cbegin%7Bmatrix%7D++++%5Cdfrac%7B%5Cdelta+y_1%7D%7B%5Cdelta+x_1%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cdelta+y_1%7D%7B%5Cdelta+x_n%7D+%5C%5C++++%5Cvdots+%26+%5Cddots+%26+%5Cvdots+%5C%5C++++%5Cdfrac%7B%5Cdelta+y_m%7D%7B%5Cdelta+x_1%7D+%26+%5Cvdots+%26+%5Cdfrac%7B%5Cdelta+y_m%7D%7B%5Cdelta+x_n%7D+++%5Cend%7Bmatrix%7D+++%5Cright%5D+%5Cleft%5B+%5Cbegin%7Bmatrix%7D+++++r_1+%5C%5C+++++%5Cvdots+%5C%5C++++++r_n+%5C%5C+++%5Cend%7Bmatrix%7D+++%5Cright%5D+%5Ctag%7B12%7D+",[],{"type":18,"tag":26,"props":478,"children":479},{},[480],{"type":24,"value":481},"最终我们可以递归的得到本次迭代的计算目标：雅克比矩阵中的第 i 行。",{"type":18,"tag":136,"props":483,"children":485},{"id":484},"优缺点",[486],{"type":24,"value":484},{"type":18,"tag":26,"props":488,"children":489},{},[490],{"type":24,"value":491},"前向模式的优点：",{"type":18,"tag":68,"props":493,"children":494},{},[495,500],{"type":18,"tag":72,"props":496,"children":497},{},[498],{"type":24,"value":499},"实现起来很简单",{"type":18,"tag":72,"props":501,"children":502},{},[503],{"type":24,"value":504},"也不需要很多额外的内存空间",{"type":18,"tag":26,"props":506,"children":507},{},[508],{"type":24,"value":509},"向前模式的缺点：",{"type":18,"tag":68,"props":511,"children":512},{},[513,518],{"type":18,"tag":72,"props":514,"children":515},{},[516],{"type":24,"value":517},"每次前向计算只能计算对一个自变量的偏导数，对于一元函数求导是高效的，但是机器学习模型的自参数（入参）数量级大。",{"type":18,"tag":72,"props":519,"children":520},{},[521],{"type":24,"value":522},"如果有一个函数，其输入有 n 个，输出有 m个，对于每个输入来说，前向模式都需要遍历计算过程以得到当前输入的导数，求解整个函数梯度需要 n 遍如上计算过程。",{"type":18,"tag":136,"props":524,"children":526},{"id":525},"反向模式-reverse-mode",[527],{"type":24,"value":528},"反向模式 Reverse Mode",{"type":18,"tag":26,"props":530,"children":531},{},[532],{"type":24,"value":533},"反向自动微分同样是基于链式法则。仅需要一个前向过程和反向过程，就可以计算所有参数的导数或者梯度。",{"type":18,"tag":26,"props":535,"children":536},{},[537],{"type":24,"value":538},"因为需要结合前向和后向两个过程，因此反向自动微分会使用一个特殊的数据结构，来存储计算过程。",{"type":18,"tag":26,"props":540,"children":541},{},[542],{"type":24,"value":543},"而这个特殊的数据结构例如Tensorflow或者MindSpore，则是把所有的操作以一张图的方式存储下来，这张图可以是一个有向无环（DAG）的计算图；而Pytroch则是使用Tape来记录每一个操作，他们都表达了函数和变量的关系。",{"type":18,"tag":26,"props":545,"children":546},{},[547],{"type":24,"value":548},"反向模式根据从后向前计算，依次得到对每个中间变量节点的偏导数，直到到达自变量节点处，这样就得到了每个输入的偏导数。在每个节点处，根据该节点的后续节点（前向传播中的后续节点）计算其导数值。",{"type":18,"tag":26,"props":550,"children":551},{},[552],{"type":24,"value":553},"整个过程对应于多元复合函数求导时从最外层逐步向内侧求导。这样可以有效地把各个节点的梯度计算解耦开，每次只需要关注计算图中当前节点的梯度计算。",{"type":18,"tag":26,"props":555,"children":556},{},[557],{"type":24,"value":558},"从下图可以看出来，reverse mode和forward mode是一对相反过程，reverse mode从最终结果开始求导，利用最终输出对每一个节点进行求导。下图虚线就是反向模式。",{"type":18,"tag":26,"props":560,"children":561},{},[562],{"type":18,"tag":130,"props":563,"children":565},{"alt":7,"src":564},"https://pic2.zhimg.com/80/v2-b4c5a1e546ba272b03e40dc523109a01_720w.jpg",[],{"type":18,"tag":136,"props":567,"children":569},{"id":568},"计算过程-1",[570],{"type":24,"value":381},{"type":18,"tag":26,"props":572,"children":573},{},[574],{"type":24,"value":575},"前向和后向两种模式的过程表达如下，表的左列浅色为前向计算函数值的过程，与前向计算时相同，右面列深色为反向计算导数值的过程。",{"type":18,"tag":26,"props":577,"children":578},{},[579],{"type":24,"value":580},"反向模式的计算过程如图所示，其中：",{"type":18,"tag":26,"props":582,"children":583},{},[584],{"type":18,"tag":130,"props":585,"children":587},{"alt":132,"src":586},"https://www.zhihu.com/equation?tex=+%5Coverline%7Bv_i%7D%3D%5Cdfrac%7B%5Cdelta+y%7D%7B%5Cdelta+v_i%7D+",[],{"type":18,"tag":26,"props":589,"children":590},{},[591],{"type":24,"value":592},"根据链式求导法则展开有：",{"type":18,"tag":26,"props":594,"children":595},{},[596],{"type":18,"tag":130,"props":597,"children":599},{"alt":132,"src":598},"https://www.zhihu.com/equation?tex=+%5Cfrac%7B%5Cpartial+f%7D%7B%5Cpartial+x%7D%3D%5Csum_%7Bk%3D1%7D%5E%7BN%7D+%5Cfrac%7B%5Cpartial+f%7D%7B%5Cpartial+v_%7Bk%7D%7D+%5Cfrac%7B%5Cpartial+v_%7Bk%7D%7D%7B%5Cpartial+%5Cboldsymbol%7Bx%7D%7D+",[],{"type":18,"tag":26,"props":601,"children":602},{},[603,605,610],{"type":24,"value":604},"可以看出，左侧是源程序分解后得到的基本操作集合，而右侧则是每一个基本操作根据已知的求导规则和链式法则",{"type":18,"tag":141,"props":606,"children":607},{},[608],{"type":24,"value":609},"由下至上",{"type":24,"value":611},"计算的求导结果。",{"type":18,"tag":26,"props":613,"children":614},{},[615],{"type":18,"tag":130,"props":616,"children":618},{"alt":7,"src":617},"https://pic3.zhimg.com/80/v2-28083c68a49350488254a8654c134826_720w.jpg",[],{"type":18,"tag":390,"props":620,"children":621},{},[622],{"type":18,"tag":72,"props":623,"children":624},{},[625],{"type":18,"tag":130,"props":626,"children":628},{"alt":7,"src":627},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/13/165daa8bc5534787944f12ba932d3cb0.png",[],{"type":18,"tag":26,"props":630,"children":631},{},[632],{"type":24,"value":633},"到目前为止，我们已经计算出来了所有步骤的偏导数的数值。现在需要计算 $\\overline{v}_1$ 和 $\\overline{v}_2$ 。计算 $\\overline{v}_1$ 从最后的位置往前到自变量 x_1，有多条路径，需要将这个路径上的数值连乘起来得到一个乘积数值，然后将这多条路径的乘积数值相加起来得到最后的结果。",{"type":18,"tag":26,"props":635,"children":636},{},[637],{"type":24,"value":638},"从 y 到 x_1 的路径有两条，分别是：",{"type":18,"tag":390,"props":640,"children":641},{},[642,647],{"type":18,"tag":72,"props":643,"children":644},{},[645],{"type":24,"value":646},"$v_5 \\to v_4 \\to v_1 \\to v_{-1}$，其数值乘积是 1∗1∗0.5=0.5",{"type":18,"tag":72,"props":648,"children":649},{},[650],{"type":24,"value":651},"$v_5 \\to v_4 \\to v_2 \\to v_{-1}$，其数值乘积是 1∗1∗ 5= 5",{"type":18,"tag":26,"props":653,"children":654},{},[655],{"type":24,"value":656},"因此，$\\overline{v}_1=0.5+5=5.5$，同理有 $\\overline{v}_2=2.0-0.284=1.716$",{"type":18,"tag":136,"props":658,"children":660},{"id":659},"向量-雅克比矩阵",[661],{"type":24,"value":659},{"type":18,"tag":26,"props":663,"children":664},{},[665],{"type":24,"value":666},"对于函数 $\\overline{y}=f(\\overline{x})$，其中 $f: \\mathbb{R}^{n} \\rightarrow \\mathbb{R}^{m}$，那么 $\\overline{y}$ 中关于 $\\overline{x}$ 的梯度可以表示为 Jacobian 矩阵：",{"type":18,"tag":26,"props":668,"children":669},{},[670],{"type":18,"tag":130,"props":671,"children":673},{"alt":132,"src":672},"https://www.zhihu.com/equation?tex=+J_%7Bf%7D%3D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bccc%7D+%5Cdfrac%7B%5Cpartial+y%7D%7B%5Cpartial+x_%7B1%7D%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cpartial+y%7D%7B%5Cpartial+x_%7B1%7D%7D+%5Cend%7Barray%7D%5Cright%5D%3D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bccc%7D+%5Cdfrac%7B%5Cpartial+y_%7B1%7D%7D%7B%5Cpartial+x_%7B1%7D%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cpartial+y_%7B1%7D%7D%7B%5Cpartial+x_%7Bn%7D%7D+%5C%5C+%5Cvdots+%26+%5Cddots+%26+%5Cvdots+%5C%5C+%5Cdfrac%7B%5Cpartial+y_%7Bm%7D%7D%7B%5Cpartial+x_%7B1%7D%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cpartial+y_%7Bm%7D%7D%7B%5Cpartial+x_%7Bn%7D%7D+%5Cend%7Barray%7D%5Cright%5D+",[],{"type":18,"tag":26,"props":675,"children":676},{},[677],{"type":24,"value":678},"设置 $\\overline{v}$ 是关于函数 $l=g(\\overline{y})$ 的梯度：",{"type":18,"tag":26,"props":680,"children":681},{},[682],{"type":18,"tag":130,"props":683,"children":685},{"alt":132,"src":684},"https://www.zhihu.com/equation?tex=+%5Coverline%7Bv%7D%3D+%5Cleft%5B%5Cbegin%7Barray%7D%7Blll%7D+%5Cdfrac%7B%5Cpartial+l%7D%7B%5Cpartial+y_%7B1%7D%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cpartial+l%7D%7B%5Cpartial+y_%7Bm%7D%7D+%5Cend%7Barray%7D%5Cright%5D%5E%7B%5Cmathrm%7BT%7D%7D+",[],{"type":18,"tag":26,"props":687,"children":688},{},[689],{"type":24,"value":690},"Jacobian-vector 积就是函数 l 中关于 x_1 的梯度：",{"type":18,"tag":26,"props":692,"children":693},{},[694],{"type":18,"tag":130,"props":695,"children":697},{"alt":132,"src":696},"https://www.zhihu.com/equation?tex=+%5Cboldsymbol%7BJ%7D+%5Ccdot+%5Coverline%7Bv%7D%3D+%5Cleft%5B%5Cbegin%7Barray%7D%7Bccc%7D+%5Cdfrac%7B%5Cpartial+y_%7B1%7D%7D%7B%5Cpartial+x_%7B1%7D%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cpartial+y_%7B1%7D%7D%7B%5Cpartial+x_%7Bn%7D%7D+%5C%5C+%5Cvdots+%26+%5Cddots+%26+%5Cvdots+%5C%5C+%5Cdfrac%7B%5Cpartial+y_%7Bm%7D%7D%7B%5Cpartial+x_%7B1%7D%7D+%26+%5Ccdots+%26+%5Cdfrac%7B%5Cpartial+y_%7Bm%7D%7D%7B%5Cpartial+x_%7Bn%7D%7D+%5Cend%7Barray%7D%5Cright%5D+%5Ccdot%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D+%5Cdfrac%7B%5Cpartial+l%7D%7B%5Cpartial+y_%7B1%7D%7D+%5C%5C+%5Cvdots+%5C%5C+%5Cdfrac%7B%5Cpartial+l%7D%7B%5Cpartial+y_%7Bm%7D%7D+%5Cend%7Barray%7D%5Cright%5D%3D%5Cleft%5B%5Cbegin%7Barray%7D%7Bc%7D+%5Cdfrac%7B%5Cpartial+y_%7B1%7D%7D%7B%5Cpartial+x_%7B1%7D%7D+%5C%5C+%5Cvdots+%5C%5C+%5Cdfrac%7B%5Cpartial+y_%7Bm%7D%7D%7B%5Cpartial+x_%7B1%7D%7D+%5Cend%7Barray%7D%5Cright%5D+",[],{"type":18,"tag":26,"props":699,"children":700},{},[701],{"type":24,"value":702},"即通过雅克比矩阵转置与后续节点梯度值的乘积，可以得到当前节点的梯度值。",{"type":18,"tag":136,"props":704,"children":706},{"id":705},"优缺点-1",[707],{"type":24,"value":484},{"type":18,"tag":26,"props":709,"children":710},{},[711],{"type":24,"value":712},"前向模式在计算之中，计算图各个节点的数值和该节点的导数可同步求出，但是代价就是对于多个输入需要多次计算才行。",{"type":18,"tag":26,"props":714,"children":715},{},[716],{"type":24,"value":717},"反向模式的优点：",{"type":18,"tag":68,"props":719,"children":720},{},[721,726],{"type":18,"tag":72,"props":722,"children":723},{},[724],{"type":24,"value":725},"通过一次反向传输，就计算出所有偏导数，中间的偏导数计算只需计算一次",{"type":18,"tag":72,"props":727,"children":728},{},[729],{"type":24,"value":730},"减少了重复计算的工作量，在多参数的时候后向自动微分的时间复杂度更低",{"type":18,"tag":26,"props":732,"children":733},{},[734],{"type":24,"value":735},"反向模式的缺点：",{"type":18,"tag":68,"props":737,"children":738},{},[739,744],{"type":18,"tag":72,"props":740,"children":741},{},[742],{"type":24,"value":743},"需要额外的数据结构记录正向过程的计算操作，用于反向使用",{"type":18,"tag":72,"props":745,"children":746},{},[747],{"type":24,"value":748},"带来了大量内存占用，为了减少内存操作，需要深度学习框架进行各种优化，也带来了额外限制和副作用",{"type":18,"tag":136,"props":750,"children":752},{"id":751},"正反向模式的比较",[753],{"type":24,"value":751},{"type":18,"tag":26,"props":755,"children":756},{},[757],{"type":24,"value":758},"前向自动微分（tangent mode AD和后向自动微分（adjoint mode AD）分别计算了Jacobian矩阵的一列和一行。",{"type":18,"tag":26,"props":760,"children":761},{},[762],{"type":18,"tag":130,"props":763,"children":764},{"alt":7,"src":230},[],{"type":18,"tag":26,"props":766,"children":767},{},[768],{"type":24,"value":769},"前向模式和反向模式的不同之处在于矩阵相乘的起始之处不同。",{"type":18,"tag":26,"props":771,"children":772},{},[773],{"type":24,"value":774},"当输出维度小于输入维度，反向模式的乘法次数要小于前向模式。因此，当输出的维度大于输入的时候，适宜使用前向模式微分；当输出维度远远小于输入的时候，适宜使用反向模式微分。",{"type":18,"tag":26,"props":776,"children":777},{},[778],{"type":24,"value":779},"即，后向自动微分更加适合多参数的情况，多参数的时候后向自动微分的时间复杂度更低，只需要一遍reverse mode的计算过程，便可以求出输出对于各个输入的导数，从而轻松求取梯度用于后续优化更新。",{"type":18,"tag":26,"props":781,"children":782},{},[783],{"type":24,"value":784},"因此，目前大部分AI框架都会优先采用反向模式，但是也有例如MindSpore等AI框架同事支持正反向的实现模式。",{"type":18,"tag":136,"props":786,"children":788},{"id":787},"参考",[789],{"type":24,"value":787},{"type":18,"tag":26,"props":791,"children":792},{},[793,795],{"type":24,"value":794},"[1] Automatic Differentiation in Machine Learning: a Survey: ",{"type":18,"tag":32,"props":796,"children":799},{"href":797,"rel":798},"https://link.zhihu.com/?target=https%3A//arxiv.org/abs/1502.05767",[36],[800],{"type":24,"value":801},"https://arxiv.org/abs/1502.05767",{"type":18,"tag":26,"props":803,"children":804},{},[805],{"type":24,"value":806},"[2] Rirchard L. Burden and J. Douglas Faires. Numerical Analysis. Brooks/Cole, 2001.",{"type":18,"tag":26,"props":808,"children":809},{},[810],{"type":24,"value":811},"[3] Max E. Jerrell. Automatic differentiation and interval arithmetic for estimation of disequilibrium models. Computational Economics, 10(3):295–316, 1997.",{"type":18,"tag":26,"props":813,"children":814},{},[815],{"type":24,"value":816},"[4] Johannes Grabmeier and Erich Kaltofen. Computer Algebra Handbook: Foundations, Applications, Systems. Springer, 2003.",{"type":18,"tag":26,"props":818,"children":819},{},[820],{"type":24,"value":821},"[5] G. W. Leibniz. Machina arithmetica in qua non additio tantum et subtractio sed et multiplicatio nullo, diviso vero paene nullo animi labore peragantur. Hannover, 1685.",{"type":18,"tag":26,"props":823,"children":824},{},[825],{"type":24,"value":826},"[6] George F. Corliss. Application of differentiation arithmetic, volume 19 of Perspectives in Computing, pages 127–48. Academic Press, Boston, 1988.",{"type":18,"tag":26,"props":828,"children":829},{},[830],{"type":24,"value":831},"[7] Arun Verma. An introduction to automatic differentiation. Current Science, 78(7):804–7, 2000.",{"type":18,"tag":26,"props":833,"children":834},{},[835],{"type":24,"value":836},"[8] Andreas Griewank and Andrea Walther. Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation. Society for Industrial and Applied Mathematics, Philadelphia, 2008. doi: 10.1137/1.9780898717761.",{"type":18,"tag":26,"props":838,"children":839},{},[840],{"type":24,"value":841},"[9] John F. Nolan. Analytical differentiation on a digital computer. Master’s thesis, Massachusetts Institute of Technology, 1953.",{"type":18,"tag":26,"props":843,"children":844},{},[845],{"type":24,"value":846},"[10] L. M. Beda, L. N. Korolev, N. V. Sukkikh, and T. S. Frolova. Programs for automatic differentiation for the machine BESM (in Russian). Technical report, Institute for Precise Mechanics and Computation Techniques, Academy of Science, Moscow, USSR, 1959.",{"type":18,"tag":26,"props":848,"children":849},{},[850],{"type":24,"value":851},"[11] Robert E. Wengert. A simple automatic derivative evaluation program. Communications of the ACM, 7:463–4, 1964.",{"type":18,"tag":26,"props":853,"children":854},{},[855],{"type":24,"value":856},"[12] Andreas Griewank. On automatic differentiation. pages 83–108, 1989.",{"type":18,"tag":26,"props":858,"children":859},{},[860],{"type":24,"value":861},"[13] Hascoet, Laurent, and Valérie Pascual. \"The Tapenade automatic differentiation tool: principles, model, and specification.\" ACM Transactions on Mathematical Software (TOMS) 39.3 (2013): 1-43.",{"type":18,"tag":26,"props":863,"children":864},{},[865,867],{"type":24,"value":866},"[14] 知乎专栏：自动微分（Automatic Differentiation）: ",{"type":18,"tag":32,"props":868,"children":871},{"href":869,"rel":870},"https://zhuanlan.zhihu.com/p/61103504",[36],[872],{"type":24,"value":869},{"type":18,"tag":26,"props":874,"children":875},{},[876,878],{"type":24,"value":877},"[15] 知乎专栏：数值计算方法 第六章 数值积分和数值微分: ",{"type":18,"tag":32,"props":879,"children":882},{"href":880,"rel":881},"https://zhuanlan.zhihu.com/p/14",[36],[883],{"type":24,"value":880},{"type":18,"tag":26,"props":885,"children":886},{},[887,889],{"type":24,"value":888},"[16] 知乎专栏：技术分享 | 从自动微分到可微编程语言设计 ",{"type":18,"tag":32,"props":890,"children":893},{"href":891,"rel":892},"https://zhuanlan.zhihu.com/p/393160344",[36],[894],{"type":24,"value":891},{"type":18,"tag":26,"props":896,"children":897},{},[898,900],{"type":24,"value":899},"[17] 博客园：深度学习利器之自动微分 ",{"type":18,"tag":32,"props":901,"children":904},{"href":902,"rel":903},"https://link.zhihu.com/?target=https%3A//www.cnblogs.com/rossiXYZ/p/15395742.html",[36],[905],{"type":24,"value":906},"https://www.cnblogs.com/rossiXYZ/p/",{"title":7,"searchDepth":908,"depth":908,"links":909},4,[910],{"id":54,"depth":911,"text":54,"children":912},2,[913,915,916,917,918,919,920,921,922,923,924,925,926],{"id":138,"depth":914,"text":138},3,{"id":176,"depth":914,"text":176},{"id":268,"depth":914,"text":268},{"id":322,"depth":914,"text":325},{"id":381,"depth":914,"text":381},{"id":422,"depth":914,"text":422},{"id":484,"depth":914,"text":484},{"id":525,"depth":914,"text":528},{"id":568,"depth":914,"text":381},{"id":659,"depth":914,"text":659},{"id":705,"depth":914,"text":484},{"id":751,"depth":914,"text":751},{"id":787,"depth":914,"text":787},"markdown","content:technology-blogs:zh:1540.md","content","technology-blogs/zh/1540.md","technology-blogs/zh/1540","md",1776506113214]