Answers.Body: <blockquote>
  <p>Does the popular packages like PyTorch, Tensorflow, Keras, etc., use this or a variant of this algorithm to find the gradients at a particular point?</p>
  </blockquote>
  <p>Yes. This is effectively what back-propagation is. However, there are a couple of important details:</p>
  <ul>
  <li><p>Using a loss function flattens the matrix form you have to a vector, because with a loss function the dimension of the output <span class="math-container">$n=1$</span>. This is important, since it is not possible to directly search for a maximum or minimum value for multi-dimensional output.</p>
  </li>
  <li><p>Back-propagation is resolved as follows:</p>
  <ul>
  <li>Set up by constructing a <a href="https://www.tutorialspoint.com/python_deep_learning/python_deep_learning_computational_graphs.htm" rel="nofollow noreferrer">computation graph</a> that composes multiple functions. Some libraries set this graph up directly e.g. TensorFlow, whilst others like PyTorch can resolve it dynamically at run time from requested computations (this is convenient and easier to write, but there is a performance cost).</li>
  <li>Gradients are calculated <em>numerically</em> by resolving function composition in reverse using <a href="https://en.wikipedia.org/wiki/Chain_rule" rel="nofollow noreferrer">the chain rule</a>.</li>
  <li>There is an implied analytic form for the forward calculation, but it never used directly.</li>
  </ul>
  </li>
  </ul>
  <blockquote>
  <p>If yes, will those packages be able to write the analytical forms of all the output variables in terms of input variables?</p>
  </blockquote>
  <p>In theory, yes, as the data to do so is in the computational graphs that are used to run neural networks forward, and is the same data that is used to resolve back propagation. In practice I have not seen this done. There may be an add-on or library that could print out the analytic forms of neural network outputs at any layer, or for the loss function. If there is not one, it would not be too hard to write one.</p>
  <p><em>However</em>, other than as a teaching aid for very small networks, then the analytical view of the outputs or loss function are not a practical or useful description of what is going on. The graph view showing how the linear algebra and non-linear activations are composed into a sequence of computations (and reversed step-by-step for gradient calculations) is much easier to comprehend. Many neural network libraries can generate views of this compuational graph.</p>
  <p>One important take-away though, is that despite the apparent complexity of back-propagation, it is doing exactly what you expect in the question: Calculating the gradient components in a Jacobian.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-20T06:29:57.913Z
Answers.LastActivityDate: 2021-08-20T06:42:29.880Z
Answers.LastEditDate: 2021-08-20T06:42:29.880Z
Answers.UserDisplayName: Neil Slater
Body: <p>Most of the neural network models in contemporary deep learning packages are trained based on gradients.</p>
  <p>Let <span class="math-container">$f: \mathbb{R}^m \rightarrow \mathbb{R}^n$</span> be a function for which we want to find a gradient, then the gradient is generally represented by a Jacobian matrix that looks like below</p>
  <p><span class="math-container">$$J = \begin{bmatrix}
      \dfrac{\partial y_1}{\partial x_1}       &amp;     \dfrac{\partial y_1}{\partial x_2} &amp;     \dfrac{\partial y_1}{\partial x_3} &amp;\dots &amp;     \dfrac{\partial y_1}{\partial x_m} \\
       \dfrac{\partial y_2}{\partial x_1}       &amp;     \dfrac{\partial y_2}{\partial x_2} &amp;     \dfrac{\partial y_2}{\partial x_3} &amp;\dots &amp;     \dfrac{\partial y_2}{\partial x_m} \\
      \cdots &amp; \cdots &amp; \cdots &amp; \cdots &amp; \cdots \\
          \dfrac{\partial y_n}{\partial x_1}       &amp;     \dfrac{\partial y_n}{\partial x_2} &amp;     \dfrac{\partial y_n}{\partial x_3} &amp;\dots &amp;     \dfrac{\partial y_n}{\partial x_m} \\
  \end{bmatrix}
  $$</span></p>
  <p>For example: If <span class="math-container">$f(x_1, x_2) = \begin{bmatrix}
             x_1 + x_2 \\
             x_1x_2 
           \end{bmatrix}$</span> then <span class="math-container">$J = \begin{bmatrix}
             1 &amp; 1 \\
             x_2 &amp; x_1 
           \end{bmatrix}$</span></p>
  <p>After calculating the Jacobian matrix, we can substitute the co-ordinate values of a particular point so that we can obtain a real matrix which is a gradient at a particular point.</p>
  <p><span class="math-container">$$
  J_{(4, 5)} = \begin{bmatrix}
             1 &amp; 1 \\
             5 &amp; 4 
           \end{bmatrix}
  $$</span></p>
  <p>In order to perform the gradient of a function at a point, the algorithm I know is as follows:</p>
  <ol>
  <li>Write each output of the function in the analytical form in terms of input;</li>
  <li>Apply partial derivative on each output w.r.t each input;</li>
  <li>Substitute the values of the input point at which we want to find the gradient.</li>
  </ol>
  <p>Thus, finally, we will get the gradient.</p>
  <p>Do the popular packages like PyTorch, Tensorflow, Keras, etc., use this or a variant of this algorithm to find the gradients at a particular point?</p>
  <p>If yes, will those packages be able to write the analytical forms of all the output variables in terms of input variables?</p>
  <p>If not, what is the high-level algorithm for calculating gradients? Is it based on <em>geometrical slope version</em> of gradient?</p>
  
Tags: <algorithm><implementation><gradient>
Title: What is the high-level algorithm followed by contemporary packages for the calculation of gradient?
UserDisplayName: hanugm
ThreadText: # What is the high-level algorithm followed by contemporary packages for the calculation of gradient?
  
  (Asked by: username_0 on 2021-08-18)
  
  Most of the neural network models in contemporary deep learning packages are trained based on gradients.
  
  Let $f: \mathbb{R}^m \rightarrow \mathbb{R}^n$ be a function for which we want to find a gradient, then the gradient is generally represented by a Jacobian matrix that looks like below
  
  $$J = \begin{bmatrix}
  \dfrac{\partial y\_1}{\partial x\_1} & \dfrac{\partial y\_1}{\partial x\_2} & \dfrac{\partial y\_1}{\partial x\_3} &\dots & \dfrac{\partial y\_1}{\partial x\_m} \\
  \dfrac{\partial y\_2}{\partial x\_1} & \dfrac{\partial y\_2}{\partial x\_2} & \dfrac{\partial y\_2}{\partial x\_3} &\dots & \dfrac{\partial y\_2}{\partial x\_m} \\
  \cdots & \cdots & \cdots & \cdots & \cdots \\
  \dfrac{\partial y\_n}{\partial x\_1} & \dfrac{\partial y\_n}{\partial x\_2} & \dfrac{\partial y\_n}{\partial x\_3} &\dots & \dfrac{\partial y\_n}{\partial x\_m} \\
  \end{bmatrix}
  $$
  
  For example: If $f(x\_1, x\_2) = \begin{bmatrix}
  x\_1 + x\_2 \\
  x\_1x\_2
  \end{bmatrix}$ then $J = \begin{bmatrix}
  1 & 1 \\
  x\_2 & x\_1
  \end{bmatrix}$
  
  After calculating the Jacobian matrix, we can substitute the co-ordinate values of a particular point so that we can obtain a real matrix which is a gradient at a particular point.
  
  $$
  J\_{(4, 5)} = \begin{bmatrix}
  1 & 1 \\
  5 & 4
  \end{bmatrix}
  $$
  
  In order to perform the gradient of a function at a point, the algorithm I know is as follows:
  
  1. Write each output of the function in the analytical form in terms of input;
  2. Apply partial derivative on each output w.r.t each input;
  3. Substitute the values of the input point at which we want to find the gradient.
  
  Thus, finally, we will get the gradient.
  
  Do the popular packages like PyTorch, Tensorflow, Keras, etc., use this or a variant of this algorithm to find the gradients at a particular point?
  
  If yes, will those packages be able to write the analytical forms of all the output variables in terms of input variables?
  
  If not, what is the high-level algorithm for calculating gradients? Is it based on *geometrical slope version* of gradient?
  
  **Tags:** algorithm, implementation, gradient
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-20. Score: 1 | Accepted answer
  
  > Does the popular packages like PyTorch, Tensorflow, Keras, etc., use this or a variant of this algorithm to find the gradients at a particular point?
  
  Yes. This is effectively what back-propagation is. However, there are a couple of important details:
  
  * Using a loss function flattens the matrix form you have to a vector, because with a loss function the dimension of the output $n=1$. This is important, since it is not possible to directly search for a maximum or minimum value for multi-dimensional output.
  * Back-propagation is resolved as follows:
  
    + Set up by constructing a [computation graph](https://www.tutorialspoint.com/python_deep_learning/python_deep_learning_computational_graphs.htm) that composes multiple functions. Some libraries set this graph up directly e.g. TensorFlow, whilst others like PyTorch can resolve it dynamically at run time from requested computations (this is convenient and easier to write, but there is a performance cost).
    + Gradients are calculated *numerically* by resolving function composition in reverse using [the chain rule](https://en.wikipedia.org/wiki/Chain_rule).
    + There is an implied analytic form for the forward calculation, but it never used directly.
  
  > If yes, will those packages be able to write the analytical forms of all the output variables in terms of input variables?
  
  In theory, yes, as the data to do so is in the computational graphs that are used to run neural networks forward, and is the same data that is used to resolve back propagation. In practice I have not seen this done. There may be an add-on or library that could print out the analytic forms of neural network outputs at any layer, or for the loss function. If there is not one, it would not be too hard to write one.
  
  *However*, other than as a teaching aid for very small networks, then the analytical view of the outputs or loss function are not a practical or useful description of what is going on. The graph view showing how the linear algebra and non-linear activations are composed into a sequence of computations (and reversed step-by-step for gradient calculations) is much easier to comprehend. Many neural network libraries can generate views of this compuational graph.
  
  One important take-away though, is that despite the apparent complexity of back-propagation, it is doing exactly what you expect in the question: Calculating the gradient components in a Jacobian.
  
  
  ---
  
  