Backward() None issue with autograd

username: xavier
cooked: <p>Hi everyone.  i have this piece of code and i am wondering the difference execution when i change some lines.</p>
<aside class="quote no-group">
<blockquote>
<p>def error(x,y):    <span class="hashtag-raw">#error</span> function<br>
e=0.5*torch.sum(torch.pow((x-y),2))<br>
print(“error=”,e)<br>
return e</p>
<p>def h_calculate(x,w,b):  <span class="hashtag-raw">#activation</span> function<br>
val=torch.matmul(x,w)+b<br>
print(“val=”,val)<br>
return val</p>
<p>lr=0.02<br>
b=torch.tensor([0.7], dtype=torch.float32, requires_grad=True) <span class="hashtag-raw">#bias</span><br>
w=torch.tensor([0.3,-0.8], dtype=torch.float32, requires_grad=True) <span class="hashtag-raw">#weights</span><br>
inputs=torch.tensor([[0.5, 1.2],[-0.8, 0.6]])<br>
y=torch.tensor([1],dtype=torch.float32)</p>
<p>y_hat=h_calculate(inputs,w,b)<br>
y_hat_final=torch.sigmoid(y_hat)<br>
loss=error(y_hat_final,y)<br>
loss.backward()<br>
print(loss)<br>
with torch.no_grad():<br>
w=w-lr<em>w.grad<br>
b=b-lr</em>b.grad<br>
#w-=lr<em>w.grad<br>
#b-=lr</em>b.grad<br>
print(w)<br>
print(b)<br>
print(w.grad)<br>
print(b.grad)</p>
</blockquote>
</aside>
<p>with this code i have the following output:</p>
<aside class="quote no-group">
<blockquote>
<p>val= tensor([-0.1100, -0.0200], grad_fn=)<br>
error= tensor(0.2666, grad_fn=)<br>
tensor(0.2666, grad_fn=)<br>
tensor([ 0.2993, -0.7953])<br>
tensor([0.7052])<br>
None<br>
None</p>
</blockquote>
</aside>
<p>but when i change this section :</p>
<aside class="quote no-group">
<blockquote>
<pre><code>w=w-lr*w.grad
b=b-lr*b.grad
#w-=lr*w.grad
#b-=lr*b.grad
</code></pre>
</blockquote>
</aside>
<p>like this :</p>
<aside class="quote no-group">
<blockquote>
<pre><code>#w=w-lr*w.grad
#b=b-lr*b.grad
w-=lr*w.grad
b-=lr*b.grad
</code></pre>
</blockquote>
</aside>
<p>the code works normally. Here is the output :</p>
<aside class="quote no-group">
<blockquote>
<p>val= tensor([-0.1100, -0.0200], grad_fn=)<br>
error= tensor(0.2666, grad_fn=)<br>
tensor(0.2666, grad_fn=)<br>
tensor([ 0.2993, -0.7953], requires_grad=True)<br>
tensor([0.7052], requires_grad=True)<br>
tensor([ 0.0353, -0.2335])<br>
tensor([-0.2577])</p>
</blockquote>
</aside>
<p>i tought that</p>
<aside class="quote no-group">
<blockquote>
<p>  <strong>a=a-5</strong> is the same as    <strong>a-=5</strong> </p>
</blockquote>
</aside>
<p>please could someone explain me these behavior ?<br>
Thanks</p>

username: beaupreda
cooked: <p>I believe this will explain the problem you are facing.</p>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/49321725/math-operator-difference-or" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/9217904/daniel" target="_blank" rel="nofollow noopener">
    <img alt="Daniel" src="https://www.gravatar.com/avatar/bef2e9215911d87a77de697b9f523b5d?s=128&amp;d=identicon&amp;r=PG" class="thumbnail onebox-avatar" width="60" height="60">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/49321725/math-operator-difference-or" target="_blank" rel="nofollow noopener">math operator difference *= or +=</a>
</h4>

<div class="tags">
  <strong>python, pytorch</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/9217904/daniel" target="_blank" rel="nofollow noopener">
    Daniel
  </a>
  on <a href="https://stackoverflow.com/questions/49321725/math-operator-difference-or" target="_blank" rel="nofollow noopener">01:16PM - 16 Mar 18 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: albanD
cooked: <p>Hi,</p>
<p>The difference is that:</p>
<ul>
<li>
<code>a = a - 5</code> will create a new tensor that contains the result of <code>a - 5</code> and then set the variable <code>a</code> to point to that new Tensor.</li>
<li>
<code>a -= 5</code> will change the Tensor pointed by <code>a</code> inplace and by doing <code>a - 5</code> and saving it in the original Tensor.</li>
</ul>

username: xavier
cooked: <p>Thank you both for your answer, i see it clearly now.<br>
here i use the <strong>-=</strong> operator and i need to zero the gradient graph in order to loop the gradient computation.</p>
<pre><code class="lang-auto">lr=0.02
b=torch.tensor([0.7], dtype=torch.float32, requires_grad=True) #bias
w=torch.tensor([0.3,-0.8], dtype=torch.float32, requires_grad=True) #weights
inputs=torch.tensor([[0.5, 1.2],[-0.8, 0.6]])
y=torch.tensor([1],dtype=torch.float32)


for i in range (5):
    print("Iteration=",i)
    y_hat=h_calculate(inputs,w,b)
    y_hat_final=torch.sigmoid(y_hat)
    loss=error(y_hat_final,y)
    loss.backward()
    print(loss)
    with torch.no_grad():
        #w=w-lr*w.grad
        #b=b-lr*b.grad
        #print(w.requires_grad)
        w-=lr*w.grad
        b-=lr*b.grad
        #print(w.requires_grad)
        print(w)
        print(b)
        #print(w.grad)
        #print(b.grad)
        w.grad.zero_()
        b.grad.zero_()
</code></pre>
<p>but here i don’t anymore need to zero gradient graph since w and b are pointing on a new Tensor, i just have to set requires_grad to <strong>True</strong>, to be able to compute the gradient in the next loop.</p>
<pre><code class="lang-auto">b=torch.tensor([0.7], dtype=torch.float32, requires_grad=True) #bias
w=torch.tensor([0.3,-0.8], dtype=torch.float32, requires_grad=True)
y_hat=h_calculate(inputs,w,b)
y_hat_final=torch.sigmoid(y_hat)
loss=error(y_hat_final,y)
for i in range (5):
    print("i=",i)
    y_hat=h_calculate(inputs,w,b)
    y_hat_final=torch.sigmoid(y_hat)
    loss=error(y_hat_final,y)
    loss.backward()
    print(loss)
    with torch.no_grad():
        w=w-lr*w.grad
        b=b-lr*b.grad
        w.requires_grad=True
        b.requires_grad=True
        #w-=lr*w.grad
        #b-=lr*b.grad
        print(w)
        print(b)
        print(w.grad)
        print(b.grad)
</code></pre>
<p>these two codes give the same output.</p>
<p>thank again.</p>