I have a trained network. And I want to calculate the gradients of outputs w.r.t. the inputs. By querying the PyTorch Docs, torch.autograd.grad may be useful. So, I use the following code:
x_test = torch.randn(D_in,requires_grad=True)
y_test = model(x_test)
d = torch.autograd.grad(y_test, x_test)[0]
model
is the neural network. x_test
is the input of size D_in
and y_test
is a scalar output.
I want to compare the calculated result with numerical difference by scipy.misc.derivative
.
So, I calculated the partial derivate by setting a index.
idx = 3
x_test = torch.randn(D_in,requires_grad=True)
y_test = model(x_test)
print(x_test[idx].item())
d = torch.autograd.grad(y_test, x_test)[0]
print(d[idx].item())
def fun(x):
x_input = x_test.detach()
x_input[idx] = x
with torch.no_grad():
y = model(x_input)
return y.item()
x0 = x_test[idx].item()
print(x0)
print(derivative(fun, x0, dx=1e-6))
But I got totally different results.
The gradient calculated by torch.autograd.grad
is -0.009522666223347187
,
while that by scipy.misc.derivative
is -0.014901161193847656
.
Is there anything wrong about the calculation? Or I use torch.autograd.grad
wrongly?