How to define a vector function in pytorch from R to R^n in order to obtain its derivative?

If I have a tensor x.
Can I define a vector function, say f(x)=(3x, x+2), in such a way that I can then obtain df/dx?

I hope my question is understood. In a nutshell: I want a way to define such a vector function, from which I can get its gradient.

Hi Juan!

Does jacobian() do what you want?

If you implement your function using (differentiable) pytorch tensor
functions, autograd and jacobian() should compute for you the
full set of partial derivatives.

Best.

K. Frank