torch.autograd.jacobian
function jacobian(func: TensorFunction, inputs: Tensor | Tensor[], options?: JacobianOptions): Tensor | Tensor[] | Tensor[][]function jacobian(func: TensorFunction, inputs: Tensor | Tensor[], create_graph: boolean, strict: boolean, vectorize: boolean, options?: JacobianOptions): Tensor | Tensor[] | Tensor[][]Computes the Jacobian of a function with respect to its inputs.
The Jacobian is a matrix of all first-order partial derivatives. For a function f: R^n -> R^m, the Jacobian is an m x n matrix.
Overload conventions:
jacobian(func, inputs, options?)jacobian(func, inputs, create_graph, options?)jacobian(func, inputs, create_graph, strict, options?)jacobian(func, inputs, create_graph, strict, vectorize)
Parameters
funcTensorFunction- A function that takes a tensor or tuple of tensors and returns a tensor or tuple of tensors
optionsJacobianOptionsoptional- Optional settings for Jacobian computation
Returns
Examples
import * as torch from '@torchjsorg/torch.js';
const x = torch.randn(3, { requires_grad: true });
const func = (x: Tensor) => x.pow(2);
// Jacobian of x^2 is diag(2x)
const J = torch.autograd.functional.jacobian(func, x);