I am trying to understand grad() function in python, I know about backpropagation but having some doubt in .grad() function result.
So if i have a very simple network say with one single input and one single weight :
import torch
from torch.autograd import Variable
from torch import FloatTensor
a_tensor=Variable(FloatTensor([1]))
weight=Variable(FloatTensor([1]),requires_grad=True)
Now i am running this in ipython cell:
net_out=a_tensor*weight
loss=5-net_out
loss.backward()
print("atensor",a_tensor)
print('weight',weight)
print('net_out',net_out)
print('loss',loss)
print(weight.grad)
During first run it returns :
atensor tensor([ 1.])
weight tensor([ 1.])
net_out tensor([ 1.])
loss tensor([ 4.])
tensor([-1.])
Which is correct because if i am right then computing gradient equation would be here :
Now netout/w would be (w*a) w.r.t to w ==> 1*a
And loss/netout (5-netout) w.r.t to netout ==> (0-1)
Which would be 1*a*-1 ==> -1
But problem is if i press same cell again without modifying anything then i am getting grad -2 , -3 ,-4 ...etc
atensor tensor([ 1.])
weight tensor([ 1.])
net_out tensor([ 1.])
loss tensor([ 4.])
tensor([-2.])
next run:
atensor tensor([ 1.])
weight tensor([ 1.])
net_out tensor([ 1.])
loss tensor([ 4.])
tensor([-3.])
so on..
I am not getting what's happening there why and how the value of grad is increasing?