Abueidda
(Abu2Pytorch)
#1
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.

LRHammond
(Lewis Hammond)
#2
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