Gradient between two different tensor

Sam3: <p>Hi,</p>
<p>can i caluclate the gradient between two tensor or is there gradient between two tensor?</p>

albanD: <p>Hi,</p>
<p>Could you clarify your question?<br>
You can get the partial derivatives of multi-variate functions.</p>

Sam3: <p>I mean i have two tensor. every tensor represent the gradient between two images (output image  w.t.r to input image ) and i want to calulate the gradient between these tensors.</p>

albanD: <blockquote>
<p>these tensors</p>
</blockquote>
<p>Is quite vague here. You mean the input/output images? Some gradients?</p>
<p>Can you write a mathematical formula of what you want?</p>

Sam3: <p>i have two tensor as output from cnn and i want to calculate the gradient between them.<br>
is there agradient between two different tensors?</p>

albanD: <blockquote>
<p>is there agradient between two different tensors?</p>
</blockquote>
<p>Not really. You have the gradient of a function usually <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

Sam3: <pre><code class="lang-auto">np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float))
</code></pre>
<p>that is gradient between two arrays</p>

albanD: <p>This is a single numpy array.</p>
<p>Also this array is interpreted as containing multiple evaluation of a function at different values. And it computes the gradient for the function these values represent.<br>
Is that what you want?</p>

Sam3: <p>I want to convert the two tensors to two arrays and calculate the gradient between them</p>

albanD: <p>I am sorry what do you mean by gradient here?</p>
<ul>
<li>You want finite difference approximation of the gradient? How do you read the tensors? Two different evaluations of the same functions? Or each Tensor corresponds to a single function?</li>
<li>You want gradients with autodiff like we usually do and one tensor was created in a differentiable manner from the other? And you want the gradient for the function that did the mapping from one to the other.</li>
</ul>

Sam3: <p>each Tensor corresponds to a single function. I  want  to calculate gradients and one tensor was created in a differentiable manner from the othe</p>

albanD: <p>Ho,</p>
<p>So it has nothing to do with <code>np.gradient()</code>.</p>
<p>In that case if you create <code>y = f(x)</code>. Then you can get the gradient of y wrt to x by doing:</p>
<pre><code class="lang-python">x.requires_grad_()
y = f(x)
grad = torch.autograd.grad(y, x)[0]
</code></pre>
<p>Note that if <code>y</code> contains more than one element, you will need to provide a grad_output=v and it will compute the dot product between v and the Jacobian of f. See <a href="https://suzyahyah.github.io/calculus/machine%20learning/2018/04/04/Jacobian-and-Backpropagation.html">here</a> for what the Jacobian is.</p>

Sam3: <p>that is great …Thank you so much… but can i set weight for these two tensors?</p>

albanD: <p>What do you mean by weight? Like how much each entry in <code>y</code> should contribute to the gradient? If so, then it is what <code>v</code> is doing in some sense.</p>

Sam3: <p>i want  to intilize  same weight for every tensor and cmopute function between two tensors after set weight and then calculate the gradient…can i make that?</p>

albanD: <p>Sorry I don’t understand what you want here… If you could write down as pseudo code or math what you want that will help.</p>