Updating parameters -> gradient is not populated as expected

MSS: <p>Hi,<br>
I have the following problem.</p>
<p>I want to update parameters a and b that build up a set of tensors. For simplicity assume this MRC:</p>
<pre><code class="lang-auto">import torch
a = torch.tensor([100.], requires_grad=True)
b = torch.tensor([50.], requires_grad=True)
c = torch.tensor([a/10, b/10], requires_grad=True)
c.retain_grad()

optim = torch.optim.Adam([a,b], lr=0.1)

d = torch.tensor([5,5])
for i in range(10):
    L = (c-d).mean()
    L.backward()
    optim.step()
    optim.zero_grad()
    print(L.item())
</code></pre>
<p>The computation graph looks like this. So it seems to end before reaching a and b and I do not understand why.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/f/afd6f22539d99e5fd5718afc31ec2763ad9f0fbd.png" data-download-href="https://discuss.pytorch.org/uploads/default/afd6f22539d99e5fd5718afc31ec2763ad9f0fbd" title="Screenshot 2023-09-05 at 14.49.13"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afd6f22539d99e5fd5718afc31ec2763ad9f0fbd_2_219x500.png" alt="Screenshot 2023-09-05 at 14.49.13" data-base62-sha1="p5y4L2yEavzfuKgqSoBfrAaQKGp" width="219" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afd6f22539d99e5fd5718afc31ec2763ad9f0fbd_2_219x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/f/afd6f22539d99e5fd5718afc31ec2763ad9f0fbd_2_328x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/f/afd6f22539d99e5fd5718afc31ec2763ad9f0fbd.png 2x" data-dominant-color="BDC1BC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2023-09-05 at 14.49.13</span><span class="informations">336×766 19.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>unfortunately, I don’t know why a and be are not populated with gradients and why there is no update happening.<br>
Any help would be much appreciated.<br>
Thank you</p>

ptrblck: <p><code>c</code> is a new leaf-tensor and there is no computation graph to <code>a</code> and <code>b</code> since you’ve re-created a tensor. Use <code>torch.stack</code> or <code>torch.cat</code> to create <code>c</code> instead if you want gradients in <code>a</code> and <code>b</code>.</p>

MSS: <p>Thank you, this is a valid point.<br>
With further testing, I also realized that the <code>torch.cat</code> operation needs to be performed for every optimization loop to not break the graph and update the values properly.</p>
<p>(I try to keep this information here for reference if someone stumbles upon the same errors)</p>
<p>Code that works as expected:</p>
<pre><code class="lang-auto">import torch

a = torch.tensor([100.], requires_grad=True)
b = torch.tensor([50.], requires_grad=True)

optim = torch.optim.Adam([a, b], lr=0.1)

d = torch.tensor([5, 5], dtype=torch.float32)  # Make sure the dtype matches
losses = []
for i in range(1000):
    c = torch.cat([a, b])/ 10
    L = torch.nn.functional.mse_loss(c, d)
    L.backward()
    optim.step()
    optim.zero_grad()
    losses.append(L.item())

print("Updated a:", a.item())
print("Updated b:", b.item())
</code></pre>