Placing tensor in new Tensor list

jsalfity: <p>I’m coming across what seemingly appears to be a bug in my code, best described by this example…</p>
<p>The variable I want to include in the computation graph is <code>theta['dm']</code>. This variable gets used in <code>m</code>, then included in <code>B</code>. Finally, it gets used in a state transition equation <code>x = A@x + B@u</code>, and a trajectory cost gets calculated.</p>
<p>Here is a small code bock:</p>
<pre><code class="lang-auto">theta = {'dm' : torch.ones(1, requires_grad=True)} 
m_bar = 1
m = m_bar * (1+theta['dm']) 
# tensor([2.], grad_fn=&lt;MulBackward0&gt;)

B = torch.tensor([[0,0],
                 [dt/m,0],
                 [0,0],
                 [0,dt/m]],
                 requires_grad=True)

# tensor([[0.0000, 0.0000],
#        [0.0500, 0.0000],
#        [0.0000, 0.0000],
#        [0.0000, 0.0500]], requires_grad=True)

# B.grad_fn = None
# B.is_leaf = True
</code></pre>
<p>The problem I’m experiencing is <code>theta['dm']</code> does not appear to be included on the computational graph. When calculating the trajectory cost mentioned above and performing <code>cost.backward()</code>, then <code>optimizer_theta.step()</code>,  the <code>theta['dm'].grad = None</code>, which appears that its not on the computation graph.</p>
<p>A warning sign I see – when created the <code>B</code> tensor, shouldn’t there be a <code>grad_fn</code> and <code>B.is_leaf=False</code>? Seems like I’m doing an incorrect assignment of <code>B</code>, if I want <code>theta['dm']</code> to be included in <code>B</code>.</p>
<p>An even simpler example:</p>
<pre><code class="lang-auto">a = torch.tensor(2.0, requires_grad=True)
b = torch.tensor(3.0)
c = a*b # works --&gt; tensor(6., grad_fn=&lt;MulBackward0&gt;)
c = torch.tensor([a*b]) # does not work --&gt; tensor([6.0]), requires_grad=False, grad_fn=None
c = torch.tensor([[a*b],[a*b]]) # does not work
</code></pre>
<p>What is wrong with the second and third assignment of c, <code>c=torch.tensor([a*b])</code> and <code>c = torch.tensor([[a*b],[a*b]])</code>?</p>
<p>Appreciate any help! Thank you.</p>
<p>update<br>
Doing some digging, I may have found a hack to work. Does this look correct / elegant?</p>
<pre><code class="lang-auto"> B1 = torch.tensor([0, 0]) 
 B2 = torch.cat((dt/m, torch.tensor([0]))) 
 B3 = torch.tensor([0, 0]) 
 B4 = torch.cat((torch.tensor([0]), dt/m)) 
  
 B = torch.stack((B1, B2, B3, B4)) 
</code></pre>

Varal7: <p>Your solution does not look like a hack to me.<br>
You should use <code>torch.cat</code> and <code>torch.stack</code> instead of <code>torch.tensor</code> to respectively concatenate and stack tensors while preserving the graph.</p>

jsalfity: <p>Thank you! It seems to be working.<br>
I don’t understand why the operators aren’t overloaded for something like <code>torch.tensor([[a*b],[a*b]])</code></p>
<p><img src="https://discuss.pytorch.org/images/emoji/apple/man_shrugging.png?v=10" title=":man_shrugging:" class="emoji only-emoji" alt=":man_shrugging:"><img src="https://discuss.pytorch.org/images/emoji/apple/man_shrugging.png?v=10" title=":man_shrugging:" class="emoji only-emoji" alt=":man_shrugging:"><img src="https://discuss.pytorch.org/images/emoji/apple/man_shrugging.png?v=10" title=":man_shrugging:" class="emoji only-emoji" alt=":man_shrugging:"></p>