Solved by LRHammond in post #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 i…