[{"data":1,"prerenderedAt":941},["ShallowReactive",2],{"content-query-L59fSTh8Pk":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":935,"_id":936,"_source":937,"_file":938,"_stem":939,"_extension":940},"/technology-blogs/zh/1524","zh",false,"","【自动微分原理】自动微分的原理介绍","自动微分的精髓在于它发现了微分计算的本质：微分计算就是一系列有限的可微算子的组合。","2022-05-30","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/a61dd49b79744352ad168ffaa430e6d7.png","technology-blogs","基础知识",{"type":15,"children":16,"toc":932},"root",[17,25,34,43,48,56,61,66,78,88,96,101,109,114,122,127,135,140,147,155,160,165,180,185,192,197,202,209,221,234,241,246,257,264,269,280,287,292,297,302,307,314,321,329,334,339,344,352,357,362,367,377,387,395,400,405,412,417,424,429,434,439,444,449,456,463,468,475,480,485,492,497,502,516,521,529,534,565,570,575,580,588,593,598,603,610,615,622,627,634,639,646,653,657,662,667,672,677,684,689,694,699,707,712,720,725,733,746,751,756,761,766,771,776,781,786,791,796,801,806,817,828,839,850,857,865,875,891,901,917],{"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],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/88eca5766647484a83b0f991992b53de.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37],{"type":18,"tag":38,"props":39,"children":40},"strong",{},[41],{"type":24,"value":42},"自动微分原理",{"type":18,"tag":26,"props":44,"children":45},{},[46],{"type":24,"value":47},"写这篇文章存粹是为了梳理MindSpore的最核心的自动微分原理的时候，网上看了很多文章，基本上都是很零散，当然 Automatic Differentiation in Machine Learning: a Survey[1] 这篇文章是目前我觉得比较好关于自动微分的文章。",{"type":18,"tag":26,"props":49,"children":50},{},[51],{"type":18,"tag":38,"props":52,"children":53},{},[54],{"type":24,"value":55},"自动微分（Automatic Differentiation，AD）",{"type":18,"tag":26,"props":57,"children":58},{},[59],{"type":24,"value":60},"是一种对计算机程序进行高效准确求导的技术，一直被广泛应用于计算流体力学、大气科学、工业设计仿真优化等领域。而近年来，机器学习技术的兴起也驱动着对自动微分技术的研究进入一个新的阶段。",{"type":18,"tag":26,"props":62,"children":63},{},[64],{"type":24,"value":65},"随着自动微分和其他微分技术研究的深入，其与编程语言、计算框架、编译器等领域的联系愈发紧密，从而衍生扩展出更通用的可微编程概念。",{"type":18,"tag":26,"props":67,"children":68},{},[69,71,76],{"type":24,"value":70},"本章将从",{"type":18,"tag":38,"props":72,"children":73},{},[74],{"type":24,"value":75},"常见的微分方法",{"type":24,"value":77},"开始介绍，然后深入自动微分基本概念。",{"type":18,"tag":26,"props":79,"children":80},{},[81,86],{"type":18,"tag":38,"props":82,"children":83},{},[84],{"type":24,"value":85},"常见计算机求导方法",{"type":24,"value":87},"：对计算机程序求导的方法可以归纳为以下四种：",{"type":18,"tag":26,"props":89,"children":90},{},[91],{"type":18,"tag":38,"props":92,"children":93},{},[94],{"type":24,"value":95},"1.手动求解法 (Manual Differentiation)",{"type":18,"tag":26,"props":97,"children":98},{},[99],{"type":24,"value":100},"完全手动完成，手工求导并编写对应的结果程序，依据链式法则解出梯度公式，带入数值，得到梯度。",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":18,"tag":38,"props":105,"children":106},{},[107],{"type":24,"value":108},"2.数值微分法 (Numerical Differentiation)",{"type":18,"tag":26,"props":110,"children":111},{},[112],{"type":24,"value":113},"利用导数的原始定义，通过有限差分近似方法完成求导，直接求解微分值。",{"type":18,"tag":26,"props":115,"children":116},{},[117],{"type":18,"tag":38,"props":118,"children":119},{},[120],{"type":24,"value":121},"3.发布符号微分法 (Symbolic Differentiation)",{"type":18,"tag":26,"props":123,"children":124},{},[125],{"type":24,"value":126},"基于数学规则和程序表达式变换完成求导。利用求导规则对表达式进行自动计算，其计算结果是导函数的表达式而非具体的数值。即，先求解析解，然后转换为程序，再通过程序计算出函数的梯度。",{"type":18,"tag":26,"props":128,"children":129},{},[130],{"type":18,"tag":38,"props":131,"children":132},{},[133],{"type":24,"value":134},"4.自动微分法 (Automatic Differentiation)",{"type":18,"tag":26,"props":136,"children":137},{},[138],{"type":24,"value":139},"介于数值微分和符号微分之间的方法，采用类似有向图的计算来求解微分值，介于数值微分和符号微分之间的一种求导方法，也是本文介绍的重点。",{"type":18,"tag":26,"props":141,"children":142},{},[143],{"type":18,"tag":30,"props":144,"children":146},{"alt":7,"src":145},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/6010c2a7b44f4880b2f819dcd4893435.jpg",[],{"type":18,"tag":26,"props":148,"children":149},{},[150],{"type":18,"tag":38,"props":151,"children":152},{},[153],{"type":24,"value":154},"1. 手动微分",{"type":18,"tag":26,"props":156,"children":157},{},[158],{"type":24,"value":159},"手动微分就是对每一个目标函数都需要利用求导公式手动写出求导公式，然后依照公式编写代码，带入数值，求出最终梯度。",{"type":18,"tag":26,"props":161,"children":162},{},[163],{"type":24,"value":164},"这种方法准确有效，但是不适合工程实现，因为通用性和灵活性很差，每一次我们修改算法模型，都要修改对应的梯度求解算法。如果模型复杂或者项目频繁反复迭代，那么工作量将会是巨大的。",{"type":18,"tag":26,"props":166,"children":167},{},[168,173,175],{"type":18,"tag":38,"props":169,"children":170},{},[171],{"type":24,"value":172},"2.",{"type":24,"value":174}," ",{"type":18,"tag":38,"props":176,"children":177},{},[178],{"type":24,"value":179},"数值微分",{"type":18,"tag":26,"props":181,"children":182},{},[183],{"type":24,"value":184},"数值微分方式应该是最直接而且简单的一种自动求导方式，使用差分近似方法完成，其本质是根据导数的定义推导而来。",{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":18,"tag":30,"props":189,"children":191},{"alt":7,"src":190},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/38991d9bd5744ebeb924af9b4a552164.png",[],{"type":18,"tag":26,"props":193,"children":194},{},[195],{"type":24,"value":196},"当 $h$ 取很小的数值，比如 0.000001 时，导数是可以利用差分来近似计算出来的。只需要给出函数值以及自变量的差值，数值微分算法就可计算出导数值。单侧差分公式根据导数的定义直接近似计算某一点处的导数值。",{"type":18,"tag":26,"props":198,"children":199},{},[200],{"type":24,"value":201},"观察导数的定义容易想到，当 $h$ 充分小时，可以用差商 $\\frac{f(x+h)-f(x)}{h}$ 近似导数结果。而近似的一部分误差（截断误差，Truncation Error）可以由泰勒公式中的二阶及二阶后的所有余项给出：",{"type":18,"tag":26,"props":203,"children":204},{},[205],{"type":18,"tag":30,"props":206,"children":208},{"alt":7,"src":207},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/41fe4389e84948ef92547bdddbb151fa.png",[],{"type":18,"tag":26,"props":210,"children":211},{},[212,214,219],{"type":24,"value":213},"因此数值微分中常用的三种计算方式及其对应的截断误差可以归纳为",{"type":18,"tag":38,"props":215,"children":216},{},[217],{"type":24,"value":218},"三种",{"type":24,"value":220},"。",{"type":18,"tag":222,"props":223,"children":224},"ul",{},[225],{"type":18,"tag":226,"props":227,"children":228},"li",{},[229],{"type":18,"tag":38,"props":230,"children":231},{},[232],{"type":24,"value":233},"向前差商（Forward Difference）****：",{"type":18,"tag":26,"props":235,"children":236},{},[237],{"type":18,"tag":30,"props":238,"children":240},{"alt":7,"src":239},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/a367ccab77754e9ea5494dba883ccb02.png",[],{"type":18,"tag":26,"props":242,"children":243},{},[244],{"type":24,"value":245},"其中Forward Difference的阶段误差为$O(h)$。",{"type":18,"tag":222,"props":247,"children":248},{},[249],{"type":18,"tag":226,"props":250,"children":251},{},[252],{"type":18,"tag":38,"props":253,"children":254},{},[255],{"type":24,"value":256},"向后差商（Reverse Difference）：",{"type":18,"tag":26,"props":258,"children":259},{},[260],{"type":18,"tag":30,"props":261,"children":263},{"alt":7,"src":262},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/e16748a296424934aa06baf51feaa307.png",[],{"type":18,"tag":26,"props":265,"children":266},{},[267],{"type":24,"value":268},"其中Reverse Difference的阶段误差为$O(h)$。",{"type":18,"tag":222,"props":270,"children":271},{},[272],{"type":18,"tag":226,"props":273,"children":274},{},[275],{"type":18,"tag":38,"props":276,"children":277},{},[278],{"type":24,"value":279},"中心差商（Center Difference）",{"type":18,"tag":26,"props":281,"children":282},{},[283],{"type":18,"tag":30,"props":284,"children":286},{"alt":7,"src":285},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/ac9e87afdb3b400d88a23972998e8867.png",[],{"type":18,"tag":26,"props":288,"children":289},{},[290],{"type":24,"value":291},"其中Center Difference的阶段误差为 $O(h^2)$。",{"type":18,"tag":26,"props":293,"children":294},{},[295],{"type":24,"value":296},"可以看出来，数值微分中的截断误差与步长 $h$ 有关，$h$ 越小则截断误差越小，近似程序越高。",{"type":18,"tag":26,"props":298,"children":299},{},[300],{"type":24,"value":301},"但实际情况数值微分的精确度并不会随着 $h$ 的减小而无限减小，因为计算机系统中对于浮点数的运算由于其表达方式存在另外一种误差（舍入误差，Round-off Error），而舍入误差则会随着 $h$ 变小而逐渐增大。因此在截断误差和舍入误差的共同作用下，数值微分的精度将会形成一个变化的函数并在某一个 $h$值处达到最小值。",{"type":18,"tag":26,"props":303,"children":304},{},[305],{"type":24,"value":306},"为了缓解截断错误，提出了中心微分近似（Center Difference Approximation），这方法仍然无法解决舍入误差，只是减少误差，但是它比单侧差分公式有更小的误差和更好的稳定性：",{"type":18,"tag":26,"props":308,"children":309},{},[310],{"type":18,"tag":30,"props":311,"children":313},{"alt":7,"src":312},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/bcfd89b305e546bfa0f414d8dbf86f2b.png",[],{"type":18,"tag":26,"props":315,"children":316},{},[317],{"type":18,"tag":30,"props":318,"children":320},{"alt":7,"src":319},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/80c42f3b621640889afed1be60d63c05.jpg",[],{"type":18,"tag":26,"props":322,"children":323},{},[324],{"type":18,"tag":38,"props":325,"children":326},{},[327],{"type":24,"value":328},"数值微分的优点",{"type":18,"tag":26,"props":330,"children":331},{},[332],{"type":24,"value":333},"(1) 具有计算适用性，对大部分表达式适用",{"type":18,"tag":26,"props":335,"children":336},{},[337],{"type":24,"value":338},"(2) 对用于显示地隐藏了求导过程",{"type":18,"tag":26,"props":340,"children":341},{},[342],{"type":24,"value":343},"(3) 简单容易实现",{"type":18,"tag":26,"props":345,"children":346},{},[347],{"type":18,"tag":38,"props":348,"children":349},{},[350],{"type":24,"value":351},"数值微分的缺点",{"type":18,"tag":26,"props":353,"children":354},{},[355],{"type":24,"value":356},"(1) 计算量大，求解速度最慢，因为每计算一个参数的导数，都需要重新计算。",{"type":18,"tag":26,"props":358,"children":359},{},[360],{"type":24,"value":361},"(2) 引入误差，因为是数值逼近，所有会不可靠，不稳定的情况，无法获得一个相对准确的导数值。如果 h 选取不当，可能会得到与符号相反的结果，导致误差增大。",{"type":18,"tag":26,"props":363,"children":364},{},[365],{"type":24,"value":366},"尤其是两个严重问题：",{"type":18,"tag":26,"props":368,"children":369},{},[370,375],{"type":18,"tag":38,"props":371,"children":372},{},[373],{"type":24,"value":374},"截断错误（Truncation error）",{"type":24,"value":376},"：在数值计算中 h 无法真正取零导致的近似误差。",{"type":18,"tag":26,"props":378,"children":379},{},[380,385],{"type":18,"tag":38,"props":381,"children":382},{},[383],{"type":24,"value":384},"舍入误差（Round-off Error）",{"type":24,"value":386},"：在计算过程中出现的对小数位数的不断舍入会导致求导过程中的误差不断累积",{"type":18,"tag":26,"props":388,"children":389},{},[390],{"type":18,"tag":38,"props":391,"children":392},{},[393],{"type":24,"value":394},"3. 符号微分",{"type":18,"tag":26,"props":396,"children":397},{},[398],{"type":24,"value":399},"符号微分（Symbolic Differentiation）属符号计算的范畴，利用求导规则对表达式进行自动计算，其计算结果是导函数的表达式。符号计算用于求解数学中的公式解，得到的是解的表达式而非具体的数值。",{"type":18,"tag":26,"props":401,"children":402},{},[403],{"type":24,"value":404},"符号微分适合符号表达式的自动求导，符号微分的原理是用下面的简单求导规则，对计算机程序中的表达式进行递归变换来完成求导替代手动微分：",{"type":18,"tag":26,"props":406,"children":407},{},[408],{"type":18,"tag":30,"props":409,"children":411},{"alt":7,"src":410},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/1cff47487762410f813e70fb436a6226.png",[],{"type":18,"tag":26,"props":413,"children":414},{},[415],{"type":24,"value":416},"另外有：",{"type":18,"tag":26,"props":418,"children":419},{},[420],{"type":18,"tag":30,"props":421,"children":423},{"alt":7,"src":422},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/ccb6c6406101455f90056815cc153389.png",[],{"type":18,"tag":26,"props":425,"children":426},{},[427],{"type":24,"value":428},"由于变换过程中并不涉及计算且是严格等价，因此其可以大大减小微分结果的误差（仅存在变换完成后计算过程中的舍入误差）。除此之外，符号微分的计算方式使其还能用于类似极值 $\\frac{\\delta}{\\delta x}f(x)=0$ 的数学问题求解。",{"type":18,"tag":26,"props":430,"children":431},{},[432],{"type":24,"value":433},"从某种角度看，这种递归思想和严格的程序变换让符号微分看上去是一种“完美”的计算过程。",{"type":18,"tag":26,"props":435,"children":436},{},[437],{"type":24,"value":438},"符号微分利用代数软件，实现微分的一些公式，然后根据基本函数的求导公式以及四则运算、复合函数的求导法则，将公式的计算过程转化成微分过程，这样就可以对用户提供的具有closed form的数学表达式进行\"自动微分\"求解。就是先求解析解，然后转换为程序，再通过程序计算出函数的梯度。",{"type":18,"tag":26,"props":440,"children":441},{},[442],{"type":24,"value":443},"符号微分计算出的表达式需要用字符串或其他数据结构存储，如表达式树。因为符号微分的这些优点，其也在包括 Mathematica、Maple、matlab、Maxima 等现代代数系统工具软件中使用。",{"type":18,"tag":26,"props":445,"children":446},{},[447],{"type":24,"value":448},"但符号微分的最大弊病在于其对表达式的严格展开和变换也导致了所谓的**表达式膨胀（expression swell）**问题。以递归表达式为例：",{"type":18,"tag":26,"props":450,"children":451},{},[452],{"type":18,"tag":30,"props":453,"children":455},{"alt":7,"src":454},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/52da595668e3415fbf60ca4a74497c62.png",[],{"type":18,"tag":26,"props":457,"children":458},{},[459],{"type":18,"tag":30,"props":460,"children":462},{"alt":7,"src":461},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/104f7666dd2a40ba960f18ca63705d95.jpg",[],{"type":18,"tag":26,"props":464,"children":465},{},[466],{"type":24,"value":467},"可以看到在不同的迭代中其符号微分的结果相比人工简化后的结果复杂很多，且随着迭代次数而增大。",{"type":18,"tag":26,"props":469,"children":470},{},[471],{"type":18,"tag":38,"props":472,"children":473},{},[474],{"type":24,"value":328},{"type":18,"tag":26,"props":476,"children":477},{},[478],{"type":24,"value":479},"(1) 精度高，可适用于更复杂的数学问题求解等场景",{"type":18,"tag":26,"props":481,"children":482},{},[483],{"type":24,"value":484},"(2) 简单容易实现",{"type":18,"tag":26,"props":486,"children":487},{},[488],{"type":18,"tag":38,"props":489,"children":490},{},[491],{"type":24,"value":351},{"type":18,"tag":26,"props":493,"children":494},{},[495],{"type":24,"value":496},"(1) 表达式必须是闭包（closed form），也就是必须能写成完整数学表达式的，不能有编程语言中的循环结构，条件结构等，这样才能将整个问题转换为一个纯数学符号问题",{"type":18,"tag":26,"props":498,"children":499},{},[500],{"type":24,"value":501},"(2)表达式复杂时候，求导结果存在表达式膨胀问题",{"type":18,"tag":26,"props":503,"children":504},{},[505,510,511],{"type":18,"tag":38,"props":506,"children":507},{},[508],{"type":24,"value":509},"4.",{"type":24,"value":174},{"type":18,"tag":38,"props":512,"children":513},{},[514],{"type":24,"value":515},"自动微分",{"type":18,"tag":26,"props":517,"children":518},{},[519],{"type":24,"value":520},"其实，对于机器学习中的应用，不需要得到导数的表达式，而只需计算函数在某一点处的导数值。",{"type":18,"tag":26,"props":522,"children":523},{},[524],{"type":18,"tag":38,"props":525,"children":526},{},[527],{"type":24,"value":528},"基本原理",{"type":18,"tag":26,"props":530,"children":531},{},[532],{"type":24,"value":533},"自动微分是介于数值微分和符号微分之间的方法，采用类似有向图的计算来求解微分值。",{"type":18,"tag":222,"props":535,"children":536},{},[537,546,556],{"type":18,"tag":226,"props":538,"children":539},{},[540,544],{"type":18,"tag":38,"props":541,"children":542},{},[543],{"type":24,"value":179},{"type":24,"value":545},"：一开始就直接代入数值近似求解",{"type":18,"tag":226,"props":547,"children":548},{},[549,554],{"type":18,"tag":38,"props":550,"children":551},{},[552],{"type":24,"value":553},"符号微分",{"type":24,"value":555},"：直接对代数表达式求解析解，最后才代入数值进行计算",{"type":18,"tag":226,"props":557,"children":558},{},[559,563],{"type":18,"tag":38,"props":560,"children":561},{},[562],{"type":24,"value":515},{"type":24,"value":564},"：首先对基本算子（函数）应用符号微分方法，其次带入数值进行计算，保留中间结果，最后通过链式求导法将中间结果应用于整个函数，这样可以做到完全向用户隐藏微分求解过程，也可以灵活于编程语言的循环结构、条件结构等结合起来",{"type":18,"tag":26,"props":566,"children":567},{},[568],{"type":24,"value":569},"关于解析解我们还要做一些说明。几乎所有机器学习算法在训练或预测时都可以归结为求解最优化问题，如果目标函数可导，则问题就变为求训练函数的驻点。但是通常情况下我们无法得到驻点的解析解，因此只能采用数值优化算法，如梯度下降法，牛顿法，拟牛顿法等等。",{"type":18,"tag":26,"props":571,"children":572},{},[573],{"type":24,"value":574},"这些数值优化算法都依赖于函数的一阶导数值或二阶导数值（包括梯度与Hessian矩阵）。因此需要解决如何求一个复杂函数的导数问题，自动微分技术是解决此问题的一种通用方法。",{"type":18,"tag":26,"props":576,"children":577},{},[578],{"type":24,"value":579},"由于自动微分法只对基本函数或常数运用符号微分法则，所以它可以灵活结合编程语言的循环结构，条件结构等。使用自动微分和不使用自动微分对代码总体改动非常小，由于它实际是一种图计算，可以对其做很多优化，所以该方法在现代深度学习系统中得到广泛应用。",{"type":18,"tag":26,"props":581,"children":582},{},[583],{"type":18,"tag":38,"props":584,"children":585},{},[586],{"type":24,"value":587},"数学基础",{"type":18,"tag":26,"props":589,"children":590},{},[591],{"type":24,"value":592},"在计算链式法则之前，我们先回顾一下复合函数。复合函数在本质上就是有关函数的函数（function of functions）。它将一个函数的返回值作为参数传递给另一个函数，并且将另一个函数的返回值作为参数再传递给下一个函数，也就是 函数套函数，把几个简单的函数复合为一个较为复杂的函数。",{"type":18,"tag":26,"props":594,"children":595},{},[596],{"type":24,"value":597},"链式法则是微积分中的求导法则，用于求一个复合函数的导数，是在微积分的求导运算中一种常用的方法。复合函数的导数将是构成复合这有限个函数在相应点的 导数的乘积，就像锁链一样一环套一环，故称链式法则。",{"type":18,"tag":26,"props":599,"children":600},{},[601],{"type":24,"value":602},"自动微分的思想则是将计算机程序中的运算操作分解为一个有限的基本操作集合，且集合中基本操作的求导规则均为已知在完成每一个基本操作的求导后，使用链式法则将结果组合得到整体程序的求导结果。",{"type":18,"tag":26,"props":604,"children":605},{},[606],{"type":18,"tag":30,"props":607,"children":609},{"alt":7,"src":608},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/71a6dc7e93674b3db21138c2984c33b5.png",[],{"type":18,"tag":26,"props":611,"children":612},{},[613],{"type":24,"value":614},"比如求导：",{"type":18,"tag":26,"props":616,"children":617},{},[618],{"type":18,"tag":30,"props":619,"children":621},{"alt":7,"src":620},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/1a87c3fd3cb8441db11c9441139544f4.png",[],{"type":18,"tag":26,"props":623,"children":624},{},[625],{"type":24,"value":626},"链式求导，令：",{"type":18,"tag":26,"props":628,"children":629},{},[630],{"type":18,"tag":30,"props":631,"children":633},{"alt":7,"src":632},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/ff264eedc31a4bf6952dbce97dd5f9de.png",[],{"type":18,"tag":26,"props":635,"children":636},{},[637],{"type":24,"value":638},"有：",{"type":18,"tag":26,"props":640,"children":641},{},[642],{"type":18,"tag":30,"props":643,"children":645},{"alt":7,"src":644},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/a8275b11b96c419995041d45d335158d.png",[],{"type":18,"tag":26,"props":647,"children":648},{},[649],{"type":18,"tag":38,"props":650,"children":651},{},[652],{"type":24,"value":515},{"type":18,"tag":26,"props":654,"children":655},{},[656],{"type":24,"value":9},{"type":18,"tag":26,"props":658,"children":659},{},[660],{"type":24,"value":661},"自动微分法被认为是对计算机程序进行非标准的解释。自动微分基于一个事实，即每一个计算机程序，不论它有多么复杂，都是在执行加减乘除这一系列基本算数运算，以及指数、对数、三角函数这类初等函数运算。",{"type":18,"tag":26,"props":663,"children":664},{},[665],{"type":24,"value":666},"于是自动微分先将符号微分法应用于最基本的算子，比如常数，幂函数，指数函数，对数函数，三角函数等，然后代入数值，保留中间结果，最后再通过链式求导法则应用于整个函数。",{"type":18,"tag":26,"props":668,"children":669},{},[670],{"type":24,"value":671},"通过将链式求导法则应用到这些运算上，我们能以任意精度自动地计算导数，而且最多只比原始程序多一个常数级的运算。",{"type":18,"tag":26,"props":673,"children":674},{},[675],{"type":24,"value":676},"我们以如下为例，这是原始公式：",{"type":18,"tag":26,"props":678,"children":679},{},[680],{"type":18,"tag":30,"props":681,"children":683},{"alt":7,"src":682},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/0c13ef1b81f94d579406851c12e9c2bb.png",[],{"type":18,"tag":26,"props":685,"children":686},{},[687],{"type":24,"value":688},"然后把这些新变量作为节点，依据运算逻辑把公式整理出一张有向无环图（DAG）。即，原始函数建立计算图，数据正向传播，计算出中间节点 xi，并记录计算图中的节点依赖关系。",{"type":18,"tag":26,"props":690,"children":691},{},[692],{"type":24,"value":693},"因此，自动微分可以被认为是将一个复杂的数学运算过程分解为一系列简单的基本运算， 其中每一项基本运算都可以通过查表得出来。",{"type":18,"tag":26,"props":695,"children":696},{},[697],{"type":24,"value":698},"因此自动微分的优缺点可以简单总结如下：",{"type":18,"tag":26,"props":700,"children":701},{},[702],{"type":18,"tag":38,"props":703,"children":704},{},[705],{"type":24,"value":706},"优点",{"type":18,"tag":26,"props":708,"children":709},{},[710],{"type":24,"value":711},"精度高，无表达式膨胀问题",{"type":18,"tag":26,"props":713,"children":714},{},[715],{"type":18,"tag":38,"props":716,"children":717},{},[718],{"type":24,"value":719},"缺点",{"type":18,"tag":26,"props":721,"children":722},{},[723],{"type":24,"value":724},"需要存储一些中间求导结果，内存占用会增加",{"type":18,"tag":26,"props":726,"children":727},{},[728],{"type":18,"tag":38,"props":729,"children":730},{},[731],{"type":24,"value":732},"参考",{"type":18,"tag":26,"props":734,"children":735},{},[736,738],{"type":24,"value":737},"[1] Automatic Differentiation in Machine Learning: a Survey: ",{"type":18,"tag":739,"props":740,"children":744},"a",{"href":741,"rel":742},"https://arxiv.org/abs/1502.05767",[743],"nofollow",[745],{"type":24,"value":741},{"type":18,"tag":26,"props":747,"children":748},{},[749],{"type":24,"value":750},"[2] Rirchard L. Burden and J. Douglas Faires. Numerical Analysis. Brooks/Cole, 2001.",{"type":18,"tag":26,"props":752,"children":753},{},[754],{"type":24,"value":755},"[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":757,"children":758},{},[759],{"type":24,"value":760},"[4] Johannes Grabmeier and Erich Kaltofen. Computer Algebra Handbook: Foundations, Applications, Systems. Springer, 2003.",{"type":18,"tag":26,"props":762,"children":763},{},[764],{"type":24,"value":765},"[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":767,"children":768},{},[769],{"type":24,"value":770},"[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":772,"children":773},{},[774],{"type":24,"value":775},"[7] Arun Verma. An introduction to automatic differentiation. Current Science, 78(7):804–7, 2000.",{"type":18,"tag":26,"props":777,"children":778},{},[779],{"type":24,"value":780},"[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":782,"children":783},{},[784],{"type":24,"value":785},"[9] John F. Nolan. Analytical differentiation on a digital computer. Master’s thesis, Massachusetts Institute of Technology, 1953.",{"type":18,"tag":26,"props":787,"children":788},{},[789],{"type":24,"value":790},"[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":792,"children":793},{},[794],{"type":24,"value":795},"[11] Robert E. Wengert. A simple automatic derivative evaluation program. Communications of the ACM, 7:463–4, 1964.",{"type":18,"tag":26,"props":797,"children":798},{},[799],{"type":24,"value":800},"[12] Andreas Griewank. On automatic differentiation. pages 83–108, 1989.",{"type":18,"tag":26,"props":802,"children":803},{},[804],{"type":24,"value":805},"[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":807,"children":808},{},[809,811],{"type":24,"value":810},"[14] 知乎专栏：自动微分（Automatic Differentiation）: ",{"type":18,"tag":739,"props":812,"children":815},{"href":813,"rel":814},"https://zhuanlan.zhihu.com/p/61103504",[743],[816],{"type":24,"value":813},{"type":18,"tag":26,"props":818,"children":819},{},[820,822],{"type":24,"value":821},"[15] 知乎专栏：数值计算方法 第六章 数值积分和数值微分: ",{"type":18,"tag":739,"props":823,"children":826},{"href":824,"rel":825},"https://zhuanlan.zhihu.com/p/14",[743],[827],{"type":24,"value":824},{"type":18,"tag":26,"props":829,"children":830},{},[831,833],{"type":24,"value":832},"[16] 知乎专栏：技术分享 | 从自动微分到可微编程语言设计 ",{"type":18,"tag":739,"props":834,"children":837},{"href":835,"rel":836},"https://zhuanlan.zhihu.com/p/393160344",[743],[838],{"type":24,"value":835},{"type":18,"tag":26,"props":840,"children":841},{},[842,844],{"type":24,"value":843},"[17] 博客园：深度学习利器之自动微分 ",{"type":18,"tag":739,"props":845,"children":848},{"href":846,"rel":847},"https://www.cnblogs.com/rossiXYZ/p/",[743],[849],{"type":24,"value":846},{"type":18,"tag":26,"props":851,"children":852},{},[853],{"type":18,"tag":30,"props":854,"children":856},{"alt":7,"src":855},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/06/b41f70765c5b413ab2732e0dcccb2f7b.jpg",[],{"type":18,"tag":26,"props":858,"children":859},{},[860],{"type":18,"tag":38,"props":861,"children":862},{},[863],{"type":24,"value":864},"MindSpore官方资料",{"type":18,"tag":26,"props":866,"children":867},{},[868,873],{"type":18,"tag":38,"props":869,"children":870},{},[871],{"type":24,"value":872},"官方QQ群",{"type":24,"value":874}," : 486831414",{"type":18,"tag":26,"props":876,"children":877},{},[878,883,885],{"type":18,"tag":38,"props":879,"children":880},{},[881],{"type":24,"value":882},"官网",{"type":24,"value":884},"：",{"type":18,"tag":739,"props":886,"children":889},{"href":887,"rel":888},"https://www.mindspore.cn/",[743],[890],{"type":24,"value":887},{"type":18,"tag":26,"props":892,"children":893},{},[894,899],{"type":18,"tag":38,"props":895,"children":896},{},[897],{"type":24,"value":898},"Gitee",{"type":24,"value":900}," : https : //gitee.com/mindspore/mindspore",{"type":18,"tag":26,"props":902,"children":903},{},[904,909,911],{"type":18,"tag":38,"props":905,"children":906},{},[907],{"type":24,"value":908},"GitHub",{"type":24,"value":910}," : ",{"type":18,"tag":739,"props":912,"children":915},{"href":913,"rel":914},"https://github.com/mindspore-ai/mindspore",[743],[916],{"type":24,"value":913},{"type":18,"tag":26,"props":918,"children":919},{},[920,925,926],{"type":18,"tag":38,"props":921,"children":922},{},[923],{"type":24,"value":924},"论坛",{"type":24,"value":884},{"type":18,"tag":739,"props":927,"children":930},{"href":928,"rel":929},"https://bbs.huaweicloud.com/forum/forum-1076-1.html",[743],[931],{"type":24,"value":928},{"title":7,"searchDepth":933,"depth":933,"links":934},4,[],"markdown","content:technology-blogs:zh:1524.md","content","technology-blogs/zh/1524.md","technology-blogs/zh/1524","md",1776506113130]