The gradient of input variables is None

Tucik_Fadu: <p>Hello, everyone. I need to use the neural network in an unconventional way, in which I have to compute the gradient of the model output with respect to the input, but I always get a None.<br>
My code is like this:</p>
<pre><code class="lang-auto">    model = torch.load('totalmodel.pth')
    model.eval()
    x = torch.tensor([1.,2.,3.,4.],device=device,requires_grad=True)
    y = model(x)
    y.backward()
    print(y)
    print(x.grad)
</code></pre>
<p>And the output is:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/8/786a7d1aab84bbc8f41b391f8875ec5c76d1166b.png" data-download-href="https://discuss.pytorch.org/uploads/default/786a7d1aab84bbc8f41b391f8875ec5c76d1166b" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/8/786a7d1aab84bbc8f41b391f8875ec5c76d1166b_2_690x63.png" alt="image" data-base62-sha1="hbfrJRNbofGlmmjnwHGTeRJi4A3" width="690" height="63" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/8/786a7d1aab84bbc8f41b391f8875ec5c76d1166b_2_690x63.png, https://discuss.pytorch.org/uploads/default/original/3X/7/8/786a7d1aab84bbc8f41b391f8875ec5c76d1166b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/8/786a7d1aab84bbc8f41b391f8875ec5c76d1166b.png 2x" data-dominant-color="0F0F0F"><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">image</span><span class="informations">720×66 6.55 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>
It shows that the grad_fn of output y is valid, and x requires_grad is True, why is the grad of x None?</p>

Tucik_Fadu: <p>I worked this out. I detach the input when I convert the data type of the input parameters… I quit the detach and it works…</p>

Smule: <aside class="quote no-group" data-username="Tucik_Fadu" data-post="2" data-topic="156090">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tucik_fadu/48/51265_2.png" class="avatar"> Tucik_Fadu:</div>
<blockquote>
<p>en I conver</p>
</blockquote>
</aside>
<p>Hi Tucik,</p>
<p>Out of interest how did you “quit the detach” for the input?</p>
<p>I don’t see this or the data type conversion in your code, but am facing the same problem,</p>
<p>Thanks</p>

srishti-git1110: <p>If the gradients are unexpectedly None, you could try the following simple checks -</p>
<ol>
<li><code>tensor.requires_grad == True</code></li>
<li>
<code>tensor.is_leaf == True</code>, or <code>tensor.grad_fn</code> is <code>None</code>;<br>
if it is not None it implies the tensor isn’t a leaf tensor and you might want to use <code>retain_grad()</code> on it.</li>
<li>autograd’s gradient computation is not disabled using<br>
• <code>torch.no_grad()</code> context manager<br>
• <code>torch.autograd.set_grad_enabled(False)</code>
</li>
<li>You are not running any non-differentiable operation and/or breaking the computation graph.</li>
</ol>
<p>Finally, you can use <code>torchviz</code> to visualize the tensor you are calculating the gradients of and ensure that the tensor you are accessing the <code>.grad</code> of is a leaf in the computation graph.</p>