How to get grad w.r.t. a non-leaf node(input)

username: Origin
cooked: <blockquote>
<p>class Mass(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.weights = nn.Parameter(torch.tensor(2e-2))</p>
<p>def jacobian(self, q1,q2):<br>
f1 = torch.tensor([[self.weights<em>torch.cos(q1)],[self.weights</em>torch.sin(q1)]], requires_grad=True)<br>
f1.sum().backward()<br>
Jv1 = q1.grad<br>
return Jv1</p>
<p>input1 = torch.tensor([[0.5]], requires_grad=True)<br>
input2 = torch.tensor([[0.]], requires_grad=True)<br>
m = Mass()<br>
print(m.jacobian(input1, input2))</p>
</blockquote>
<p>Hi everyone. I am new to Pytorch. I am trying to compute q1.grad but everytime it shows the None as answer. Any help would be appreciated. Thanks.</p>

username: ptrblck
cooked: <p>You are detaching the input tensors from the computation graph by creating a new tensor:</p>
<pre><code class="lang-python">f1 = torch.tensor([[self.weightstorch.cos(q1)],[self.weightstorch.sin(q1)]], requires_grad=True)
</code></pre>
<p>Replace the <code>torch.tensor</code> creation with e.g. <code>torch.cat</code> or <code>torch.stack</code> and it should work.</p>

username: Origin
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks a lot for the exact solution.</p>

username: Origin
cooked: <p>f_x_1 = ac*cos(q1)</p>
<p>f_y_1 = ac*sin(q1)</p>
<p>f1 = Matrix([f_x_1 , f_y_1])</p>
<p>f_x_2 = a<em>cos(q1) + ac</em>cos(q1 +q2)</p>
<p>f_y_2 = a<em>sin(q1) + ac</em>sin(q1 +q2)</p>
<p>f2 = Matrix([f_x_2 , f_y_2])</p>
<p>Jv1 = Matrix([[diff(f1, q1), diff(f1, q2)]])</p>
<p>M = m*(Jv1.T)*(Jv1)</p>
<p>Jv1.subs(q1, 0.5) -----&gt;&gt; [<br>
−0.00958851077208406     0<br>
0.0175516512378075        0 ]</p>
<p>I wrote this code in Python using Sympy module. Could you please let me know what’s the best way to write similar code in Pytorch? I basically wanted to calculate Jacobian which is a 2 by 2 matrix w.r.t inputs (q1,q2). I am really having a hard time here. Thanks.<br>
​</p>

username: ptrblck
cooked: <p>Did you check <a href="https://pytorch.org/docs/stable/generated/torch.autograd.functional.jacobian.html#torch.autograd.functional.jacobian"><code>torch.autograd.functional.jacobian</code></a> already to see if this would work for your use case?<br>
You should be able to replace the other method directly with PyTorch operations, e.g. <code>asin</code> with <code>torch.asin</code>.</p>

username: Origin
cooked: <p>Thanks. It’s my mistake, but the expression is a*sin(q1), so a is constant here. although I have written a different code. I tried</p>
<blockquote>
<p>fx1 = self.weights<em>torch.cos(q1) + 0</em>q2</p>
</blockquote>
<blockquote>
<p>torch.autograd.grad(fx1,(q1,q2),retain_graph=True,allow_unused=True)</p>
</blockquote>
<p>where a = self.weights. (I need to predict the a)</p>
<p>And I believe that it will work well. Will I face any difficulty training those self.weights bcoz I used autograd.grad? I heard that it would cause some problems with retaining the graph or something. I am not sure.</p>

username: ptrblck
cooked: <p>I’m not sure which issues you are referring to and I’m not aware of any regarding retaining the graph.</p>

username: Origin
cooked: <p>Anyway, thank you for your time, sir.</p>