Copy
tensorplay.autograd.functional.jacobian
- tensorplay.autograd.functional.jacobian(func, inputs, create_graph=False, strict=False, vectorize=False, strategy='reverse-mode')[source]
Compute the Jacobian of a given function.
- Parameters:
func (function) – a Python function that takes Tensor inputs and returns a tuple of Tensors or a Tensor.
inputs (tuple of Tensors or Tensor) – inputs to the function
func.create_graph (bool, optional) – If
True, the Jacobian will be computed in a differentiable manner. Note that whenstrictisFalse, the result can not require gradients or be disconnected from the inputs. Defaults toFalse.strict (bool, optional) – If
True, an error will be raised when we detect that there exists an input such that all the outputs are independent of it. IfFalse, we return a Tensor of zeros as the jacobian for said inputs, which is the expected mathematical value. Defaults toFalse.vectorize (bool, optional) – Not supported by this engine yet; passing
TrueraisesNotImplementedError.strategy (str, optional) – Set to
"reverse-mode"(default) or"forward-mode". Forward-mode AD is not supported by this engine yet; passing it raisesNotImplementedError.
- Returns:
if there is a single input and output, this will be a single Tensor containing the Jacobian for the linearized inputs and output. If one of the two is a tuple, then the Jacobian will be a tuple of Tensors. If both of them are tuples, then the Jacobian will be a tuple of tuple of Tensors where
Jacobian[i][j]will contain the Jacobian of theith output andjth input and will have as size the concatenation of the sizes of the corresponding output and the corresponding input and will have same dtype and device as the corresponding input.- Return type:
Jacobian (Tensor or nested tuple of Tensors)
Example
>>> def exp_reducer(x): ... return x.exp().sum(dim=1) >>> inputs = tensorplay.rand(2, 2) >>> jacobian(exp_reducer, inputs) tensor([[[1.4917, 2.4352], [0.0000, 0.0000]], [[0.0000, 0.0000], [2.4369, 2.3799]]])
Help improve this page
Found an error, an unclear step, or a missing example?
