Suppose I have a function def f(x): return torch.sin(x)
I can find the derivative of the function at a point as folllows:
x = torch.tensor([2.], requires_grad=True)
y = f(x)
y.backward(retain_graph=True)
x.grad

However, this requires computing the value of f(x) in order to find it’s derivative, which should not be required
Other libraries such as jax provide grad(f) which returns the function cos(x)
I was wondering if there is any Pytorch equivalent of this function?
I tried using y.grad_fn in conjunction with next_functions in order to reconstruct f’(x), but this did not seem to give correct values
Any ideas?