How does autograd work?

MrParosk: <p>Hi,</p>
<p>I have been wondering how autograd actually works, i.e. how does it compute the gradients? I understand that it does not use any numerical methods (i.e. finite element methods etc).</p>
<p>Are all method’s derivative pre-defined / calculated, i.e. d/dx (x^p) = p * x^(p-1) inside Autograd or is it using some symbolic methods?</p>
<p>Also, do all deep learning framework (i.e. PyTorch, Tensorflow, MxNet etc) use the same method or is there different between them?</p>
<p>Thanks.</p>

dejanbatanjac: <aside class="quote no-group" data-username="MrParosk" data-post="1" data-topic="61702">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/9de0a6/48.png" class="avatar"> MrParosk:</div>
<blockquote>
<p>symbolic methods</p>
</blockquote>
</aside>
<p>No symbolic methods. That would be insane. It uses calculus based on FORTRAN style and GRAPH it creates.</p>
<p>If you have to calculate derivative of a function <code>y = f(a,b,c)</code> at point <code>a</code>, then you will measure <code>y</code> changes with respect to the change of the variable <code>a</code>.</p>
<pre><code class="lang-auto">da = (f(a+e,b,c)-f(a,b,c))/e
print(da)
</code></pre>
<p>where e is some small number.</p>

MrParosk: <p>Okay but isn’t that numerical differentiation (i.e. <a href="https://en.wikipedia.org/wiki/Numerical_differentiation" rel="nofollow noopener">https://en.wikipedia.org/wiki/Numerical_differentiation</a>)?</p>
<p>Or is it more like Automatic differentiation using dual numbers (<a href="https://en.wikipedia.org/wiki/Automatic_differentiation" rel="nofollow noopener">https://en.wikipedia.org/wiki/Automatic_differentiation</a>)?</p>

tcapelle: <p>This video explains very well:<br>
</p><div class="lazyYT" data-youtube-id="MswxJw-8PvE" data-youtube-title="PyTorch Autograd Explained - In-depth Tutorial" data-width="480" data-height="270" data-parameters="feature=oembed&amp;wmode=opaque&amp;start=617"></div>

MrParosk: <p>Thanks, the video was great!</p>
<p>However, I don’t think it answers my question, i.e. how are the actual gradients calculated (i.e. how do we calculate the gradient of log(x) in an automatic way).</p>

albanD: <p>Hi,</p>
<p>Yes automatic differentiation is used.<br>
This basically computes gradients with the chain rule: if you have <code>y = f(x)</code> and <code>z = g(y)</code>.<br>
Then you compute <code>dz/dx = dz/dy * dy/dx</code> (with * being a matrix multiply).</p>
<p>So the only thing needed to compute any gradient is to have the formula for all the elementary functions and some mechanic to know how to apply them.<br>
You can find (most) of the formulas in <a href="https://github.com/pytorch/pytorch/blob/master/tools/autograd/derivatives.yaml">this</a> file that we use to define the mapping between a function and its derivative.<br>
And the order is given by an acyclic graph that we create during the forward.</p>

MrParosk: <p>Hi,</p>
<p>Thanks!, that clarifies the approach. Do you know if that is the common way to do it (i.e. do you think Tensorflow, MxNet etc have the same approach or is it PyTorch specific)?</p>

albanD: <p>The chain rule-based method is also known as backpropagation and is clearly used by most DL framework. I am not sure about MxNet, but Tensorflow use it for sure.<br>
The main difference with pytorch used to be that the acyclic graph in Tensorflow was created statically. While pytorch recreates it at each forward. But that is not true anymore with tensorflow eager mode by default.</p>

MrParosk: <p>Hi,</p>
<p>I meant the “hard-coding” of gradients.</p>

dejanbatanjac: <blockquote>
<p>Do you know if that is the common way to do it (i.e. do you think Tensorflow, MxNet etc have the same approach or is it PyTorch specific.</p>
</blockquote>
<p>This <a href="https://arxiv.org/pdf/1502.05767.pdf" rel="noopener nofollow ugc">paper</a> may provide further information for you.</p>
<blockquote>
<p>Or is it more like Automatic differentiation using dual numbers (<a href="https://en.wikipedia.org/wiki/Automatic_differentiation" rel="noopener nofollow ugc">https://en.wikipedia.org/wiki/Automatic_differentiation </a>)?</p>
</blockquote>
<p>Yes, AD (Automatic differentiation) first appeared in <a href="https://www.mcs.anl.gov/research/projects/adifor/" rel="noopener nofollow ugc">Fortran project</a>. Fact from the paper I sent you the link.</p>
<p>Fairly the most outstanding articles explaining AD I found is <a href="https://alexey.radul.name/ideas/2013/introduction-to-automatic-differentiation/" rel="noopener nofollow ugc">this one</a> from 2013.</p>

albanD: <p>Ho,</p>
<p>Yes you have to. The formula has to come from somewhere since we don’t do neither symbolic differentiation or finite differences.</p>

MrParosk: <p>Hi,</p>
<p>Thank you for the answers!</p>

MrParosk: <p>Thanks, will take a look at the papers.</p>