Gradient of output is 0

Abhranta: <p>I am sorry I cannot share my code, but is it somewhere along the lines of:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/6/a63596f2711f1f0ffe49ff751f80991685792c02.png" alt="forum" data-base62-sha1="nIm4FGSqL9lankZAypGy0NxO9Dc" width="494" height="285"></p>
<p>When I try to print the gradient of y_predicted (which is the output label) in my training loop after calling .backward(), it gives me None. Can anyone help with where I am going wrong ?</p>
<p>P.S. Please ignore the logic errors in the code.</p>

tom: <p>Don’t use  torch.tensor in between and never use torch.FloatTensor, but instead use <code>stack</code> to make a list of tensors into one large tensor.<br>
Of course, the presentation makes me wonder if you can cut the for loops by vectorizing and cumsum, too.</p>
<p>Best regards</p>
<p>Thomas</p>

Abhranta: <p>Here is an axample code.<br>
1)loss function<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/2/227bf788388c1bb5c9edaab9636438deeb78c411.png" data-download-href="https://discuss.pytorch.org/uploads/default/227bf788388c1bb5c9edaab9636438deeb78c411" title="Screenshot from 2021-08-04 01-19-08"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/227bf788388c1bb5c9edaab9636438deeb78c411_2_690x167.png" alt="Screenshot from 2021-08-04 01-19-08" data-base62-sha1="4V3PzPoG60QftkeqjKIwMGR90dz" width="690" height="167" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/227bf788388c1bb5c9edaab9636438deeb78c411_2_690x167.png, https://discuss.pytorch.org/uploads/default/original/3X/2/2/227bf788388c1bb5c9edaab9636438deeb78c411.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/2/227bf788388c1bb5c9edaab9636438deeb78c411.png 2x" data-dominant-color="0E0F0E"><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 from 2021-08-04 01-19-08</span><span class="informations">937×227 29.1 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><br>
2)training loop<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/7/87e3fdde406193b2e7b01ed4c3a1872b0eea6f32.png" data-download-href="https://discuss.pytorch.org/uploads/default/87e3fdde406193b2e7b01ed4c3a1872b0eea6f32" title="Screenshot from 2021-08-04 01-20-54"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/87e3fdde406193b2e7b01ed4c3a1872b0eea6f32_2_515x500.png" alt="Screenshot from 2021-08-04 01-20-54" data-base62-sha1="jo8VyPAgvtu4dqSHsj6ZILSruYa" width="515" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/7/87e3fdde406193b2e7b01ed4c3a1872b0eea6f32_2_515x500.png, https://discuss.pytorch.org/uploads/default/original/3X/8/7/87e3fdde406193b2e7b01ed4c3a1872b0eea6f32.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/7/87e3fdde406193b2e7b01ed4c3a1872b0eea6f32.png 2x" data-dominant-color="0F0F0E"><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 from 2021-08-04 01-20-54</span><span class="informations">731×709 69.4 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>This is an example code for something that I want to do. Here when i print output_label.grad , it returns None. I don’t get what I am doing wring here. Also, can you please give an example about how can i get rid of the for loops ?</p>

Abhranta: <p>It says that output_label is not a leaf node of the graph. What does this mean ? If I am not wrong, while doing backpropagation, the gradient of the loss with respect to the output also needs to be calculated. uu here, output_label.grad is giving None.<br>
Am I doing something wrong here ?</p>

Varal7: <p>Under the hood, yes, the autograd engine does compute the gradient of the loss with respect to the output but the result is never stored but passed along backward through the graph to compute the gradient with respect to the leaves.<br>
What are leaves: that’s exactly the tensors created with <code>requires_grad=True</code>.<br>
Typically, that would be the parameters of your <code>net</code> and you don’t really need to explicitly set  <code>requires_grad=True</code> because that’s the default for parameters.</p>
<p>That’s why you never really want to write <code>z_predicted = torch.tensor(z_predicted,requires_grad=True)</code> as it breaks your autograd graph.</p>
<p>If you really just gradient of your loss with respect to <code>output_label</code>, try:</p>
<pre><code class="lang-auto">output_label = net(input)
output_label_leaf = output_label.detach()
output_label_leaf.requires_grad_()
loss = criterion(output_label_leaf)  # don't do any torch.tensor shenanigans in here
loss.backward()
print(output_label_leaf.grad)
</code></pre>