Gradients are None after torch.autograd.grad()

jia: <p>Hi, I have some issues about autograd. I try to implement a network whose nodes in the hidden layer takes diverse noisy inputs. However, it hindered when computing the gradients w.r.t all the parameters.</p>
<p>num_hid = 1<br>
nr_classes = 10<br>
w1 = torch.rand(1024,num_hid,requires_grad = True)-0.5<br>
w1_bias = torch.rand(num_hid,requires_grad = True)-0.5<br>
w2 = torch.rand(num_hid,nr_classes,requires_grad = True)-0.5<br>
w2_bias = torch.rand(nr_classes,requires_grad = True)-0.5</p>
<p>noise_gt_data1 = GenerateNoisyData(n)<br>
linear_output_first_layer = torch.matmul(noise_gt_data1.reshape(-1,1024),w1)+w1_bias<br>
sigmoid = nn.Sigmoid()<br>
activation_output_first_layer = sigmoid(linear_output_first_layer)<br>
out = Variable(torch.matmul(activation_output_first_layer,w2),requires_grad=True)+w2_bias</p>
<p>net = Net(num_hid)<br>
net.body[0].weight.data = w1<br>
net.body[0].bias.data = w1_bias<br>
net.fc[0].weight.data = w2<br>
net.fc[0].bias.data = w2_bias</p>
<p>y = criterion(out, gt_onehot_label)<br>
gradients=torch.autograd.grad(y,net.parameters(),create_graph=True,retain_graph=True,allow_unused=True)<br>
print(gradients)</p>
<p>It shows gradients are all None. I tried both backward() and autograd.grad(). Neither works.</p>

albanD: <p>Hi,</p>
<p>Few things: You should not use <code>Variable</code> anymore. All the Tensors can require gradients now.<br>
And if the output of a function does not have requires_grad=True it is because no input were requiring gradients. So you should make sure that the inputs you want the gradients for are set properly.</p>
<p>Also you should not use <code>.data</code> as it has many side effects that you most likely don’t want (and will lead to silently wrong gradients in some cases).<br>
If you want to set the weights of your net you can do:</p>
<pre><code class="lang-python">with torch.no_grad():
  net.body[0].weight.copy_(w1)
  net.body[0].bias.copy_(w1_bias)
  # etc</code></pre>

jia: <aside class="quote no-group" data-username="albanD" data-post="2" data-topic="96601">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p><code>with torch.no_grad():</code></p>
</blockquote>
</aside>
<p>Really appreciate your help. I have removed Variable().<br>
I kind of initial nodes in the hidden layer accordingly, but when I compute the gradients w.r.t net.parameters(), all the gradients are None. However, if w.r.t [w1,w1_bias,w2,w2_bias], it works properly.<br>
Is there any way to solve it?</p>

albanD: <p>As is clear with the <code>torch.no_grad()</code>. setting the value in the net is not a differentiable operation and <code>w1</code> is not the same as <code>net.body[0].weight</code>.  (you previous code was breaking this as well but in a more subtle manner by using .data).</p>
<p>I am afraid you cannot assign these in a differentiable manner. You can set them before and use these to compute the output though if you want the gradient wrt to these paramters to be computed.</p>

jia: <p>solved in the other way, thanks:)</p>