Reputation: 25
If I have a tensor x
.
Can I define a vector function, say f(x) = (3x, x+2)
, and obtain its derivative df/dx
?
In a nutshell: I want a way to define such a vector function, from which I can get its gradient.
Upvotes: 0
Views: 856
Reputation: 40628
You can do so using torch.autograd.functional.jacobian
, providing the function and input:
>>> jacobian(lambda x: (3*x, x+2), inputs=torch.tensor([3.]))
In this case the result is df/dx = (3, 1)
for all x
.
Upvotes: 1