Hi everyone.  i have this piece of code and i am wondering the difference execution when i change some lines.


def error(x,y):    #error function
e=0.5*torch.sum(torch.pow((x-y),2))
print(“error=”,e)
return e
def h_calculate(x,w,b):  #activation function
val=torch.matmul(x,w)+b
print(“val=”,val)
return val
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)
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-lrw.grad
b=b-lrb.grad
#w-=lrw.grad
#b-=lrb.grad
print(w)
print(b)
print(w.grad)
print(b.grad)


with this code i have the following output:


val= tensor([-0.1100, -0.0200], grad_fn=)
error= tensor(0.2666, grad_fn=)
tensor(0.2666, grad_fn=)
tensor([ 0.2993, -0.7953])
tensor([0.7052])
None
None


but when i change this section :


w=w-lr*w.grad
b=b-lr*b.grad
#w-=lr*w.grad
#b-=lr*b.grad



like this :


#w=w-lr*w.grad
#b=b-lr*b.grad
w-=lr*w.grad
b-=lr*b.grad



the code works normally. Here is the output :


val= tensor([-0.1100, -0.0200], grad_fn=)
error= tensor(0.2666, grad_fn=)
tensor(0.2666, grad_fn=)
tensor([ 0.2993, -0.7953], requires_grad=True)
tensor([0.7052], requires_grad=True)
tensor([ 0.0353, -0.2335])
tensor([-0.2577])


i tought that


  a=a-5 is the same as    a-=5 


please could someone explain me these behavior ?
Thanks