Calculating the gradients with respect to the input

Hi there,

I have this problem regarding gradient calculation. Any help is appreciated.

Given the input x, the output u is inferenced from a NN model. Then, u is used in calculating the tensor s=f(u). I am trying to find ds/dx.

I really appreciate any help you can provide.

I’m a relative newbie, so you should defer to more experienced people if they comment, but have you tried:

d = torch.autograd.grad(outputs=f(nn(x)), inputs=x)

where nn is your model? This should (I think) get you the gradient ds/dx. If you haven’t already read the Autograd tutorial I’d recommend it!

1 Like