(Newbie Question) Getting the gradient of output with respect to the input
Hello all, I’m new to using neural network libraries, so I’m sorry if this is  a stupid question. I have a pretrained network with a 28x28 input(MNIST) image and 10 outputs. I want to get the gradient of one of those outputs wrt the input. Thank you very much in advance!
ckanbak
DiffEverything
<SCODE>import torch
a = torch.autograd.Variable(torch.Tensor(10,10))
print(a.requires_grad) # prints False
<ECODE>
albanD
DiffEverything
ckanbak
Hi, <SCODE>for digit in selected_digits:
    output[digit].backward(retrain_variables=True)
<ECODE>
albanD
<SCODE>net1.zero_grad(); net2.zero_grad()
x = Variable(torch.ones((1,20)), requires_grad=True)
y = net1(x)
z = net2(y)
z.backward(torch.ones(z.size()),retain_variables=True)
print(x.grad) # this will give non-zero value
print(y.grad) # this is all zero

net1.zero_grad(); net2.zero_grad()
y = torch.from_numpy(y.data.numpy())
y = Variable(y, requires_grad=True)
z = net2(y)
z.backward(torch.ones(z.size()),retain_variables=True)
print(y.grad) # this will give non-zero value<ECODE>
Guan-Horng_Liu
smth
Hi, would this still be the preferred approach in version 1.0, I can’t seem to get retrain_variable=True to work and am trying to do the same thing Thanks John
fromLittleAcorns
Hi,
albanD
If I do this will the gradients coming out of input increment each time or will they be overwritten.  I read that gradients are retained and only cleared when the model is zero grad is called.  If they are incremental then can I get each individual value but taking the difference of the previous value and the latest? Thanks again
fromLittleAcorns
albanD