Backward error : 'NoneType' object has no attribute 'data'

xavier: <p>hi. i want to compute the gradient with pytorch autograd in a loop (because i have may training set):</p>
<aside class="quote no-group">
<blockquote>
<p>dataloader_train = DataLoader(dataset_train, batch_size=4, shuffle=True)<br>
#b=torch.tensor([0.7],dtype=torch.float32, requires_grad=True)<br>
b=Variable(torch.tensor([0.7]), requires_grad=True)<br>
dataloader_train = DataLoader(dataset_train, batch_size=4, shuffle=True)<br>
#w=torch.tensor([0.3,-0.8], dtype=torch.float32, requires_grad=True)<br>
w=Variable(torch.tensor([0.3,-0.8]),requires_grad=True)<br>
alpha=0.01<br>
epochs=1</p>
<p>for epoch in range(epochs):<br>
for i_batch, (x_batch,y_batch) in enumerate(dataloader_train):<br>
print(“i_batch=”,i_batch)<br>
for i in range(4):<br>
w.grad.data.zero_()<br>
b.grad.data.zero_()<br>
print(“i_iteration=”,i)<br>
current_train=x_batch[i,:]<br>
print(“current_train=”,current_train)<br>
print(“old_w=”,w)<br>
z=torch.sum(torch.mul(current_train,w))+b<br>
print(“z=”,z)<br>
y_hat=torch.sigmoid(z)<br>
#y_hat=l(w)<br>
<span class="hashtag-raw">#print</span>(“y_hat_vor round=”,y_hat)<br>
#y_hat=torch.round(y_hat)<br>
print(“y_hat after round=”,y_hat)<br>
print(“has w grad=”,w.requires_grad)<br>
print(“has b grad=”,b.requires_grad)<br>
y_hat.backward()<br>
y_hat_w_grad=w.grad.data.detach().clone()<br>
y_hat_b_grad=b.grad.data.detach().clone()<br>
print(“y_hat_w_grad=”,y_hat_w_grad)<br>
print(“y_hat_b_grad=”,y_hat_b_grad)<br>
print(“y_batch=”,y_batch[i])<br>
w=w+(alpha*(-1)<em>y_hat_w_grad)<br>
b=b+(alpha</em>(-1)*y_hat_b_grad)<br>
print(“new_w=”,w)<br>
print(w)</p>
</blockquote>
</aside>
<p>Basicaly it compute the gradient hust for the first loop.<br>
for the second loop, it show this error :</p>
<aside class="quote no-group">
<blockquote>
<p>AttributeError                            Traceback (most recent call last)<br>
 in <br>
33             print(“has b grad=”,b.requires_grad)<br>
34             y_hat.backward()<br>
—&gt; 35             y_hat_w_grad=w.grad.data.detach().clone()<br>
36<br>
37             y_hat_b_grad=b.grad.data.detach().clone()</p>
<p>AttributeError: ‘NoneType’ object has no attribute ‘data’</p>
</blockquote>
</aside>
<p>please what i am doing wrong or how can i solve it.<br>
thanks</p>

xavier: <p>i have change the update of weights and bias like this</p>
<pre><code>        w=Variable((w+(alpha*(-1)*y_hat_w_grad)),requires_grad=True)
        b=Variable((b+(alpha*(-1)*y_hat_b_grad)),requires_grad=True)
</code></pre>
<p>the code doesn’t not show any error but i still wonder if it compute good the gradient (with these two lines</p>
<aside class="quote no-group">
<blockquote>
<p>y_hat_w_grad=w.grad.data.detach().clone()<br>
y_hat_b_grad=b.grad.data.detach().clone()</p>
</blockquote>
</aside>
<p>)</p>

Nikronic: <p>Hi,<br>
First of all, could you please reformat your code section using ```???<br>
It is really hard to read.</p>
<p>And note that <code>Variable</code> has been removed from PyTorch, and now if you want to enable gradient computing on a tensor use following snippet:</p>
<aside class="quote no-group" data-username="xavier" data-post="1" data-topic="59933">
<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/xavier/48/17271_2.png" class="avatar"> xavier:</div>
<blockquote>
<p>b=Variable(torch.tensor([0.7]), requires_grad=True)</p>
</blockquote>
</aside>
<pre><code class="lang-auto">b = torch.tensor([0.7])
b.requires_grad = True
</code></pre>
<p>But about this part:</p>
<aside class="quote no-group" data-username="xavier" data-post="1" data-topic="59933">
<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/xavier/48/17271_2.png" class="avatar"> xavier:</div>
<blockquote>
<p>y_hat_w_grad=w.grad.data.detach().clone()</p>
</blockquote>
</aside>
<p>I think you first need to <code>clone</code> then <code>detach</code>. If you <code>detach</code> first, your <code>w</code> will no longer have <code>grad</code> because it is detached from computing graph.<br>
So the solution would be something like this:</p>
<pre><code class="lang-auto">y_hat_w_grad=w.grad.data.clone().detach()
</code></pre>
<p>Bests<br>
Nik</p>

xavier: <p>thank you for the info about “Variable”.  what you proposed works.</p>