# jacobian¶

paddle.autograd. jacobian ( ys: Union[paddle.Tensor, Tuple[paddle.Tensor, ...]], xs: Union[paddle.Tensor, Tuple[paddle.Tensor, ...]], batch_axis: Optional[int] = None ) Union[Tuple[Tuple[paddle.autograd.autograd.Jacobian, ...], ...], Tuple[paddle.autograd.autograd.Jacobian, ...], paddle.autograd.autograd.Jacobian] [source]

Computes the Jacobian of the dependent variable `ys` versus the independent variable `xs`.

Where `ys` represents the output of `xs` after a certain operation, `ys` and `xs` can be Tensor or tuple of Tensors, `batch_axis` indicates the position of the batch dimension of the parameter data.

When the input is a tuple Tensors, the returned result is a `Jacobian` object with the same number of nesting levels as `xs`, and each Jacobian has the same shape as The `xs` tuples are identical in one-to-one correspondence.

• When `batch_axis=None`, only 0-dimensional Tensor or 1-dimensional Tensor is supported, assuming the shape of `xs` is `[N, ]`, the shape of `ys` is `[M, ]`, then the output Jacobian matrix shape is `[M, N]`.

• When `batch_axis=0`, only 1-dimensional Tensor or 2-dimensional Tensor is supported, assuming the shape of `xs` is `[B, N]`, The shape of `ys` is `[B, M]`, then the output Jacobian matrix shape is `[B, M, N]`.

After the `Jacobian` object is created, the actual calculation process does not occur, but the lazy evaluation method is used for calculation. It can be multi-dimensional indexed to obtain the entire Jacobian matrix or sub-matrix, and the actual calculation will be performed at this time the value is calculated and the result is returned. At the same time, in the actual evaluation process, the calculated sub-matrix will be cached to avoid duplicate calculations in the subsequent indexing process.

For example, assuming `Jacobian` instance `J` has shape `[B, M, N]`, assuming `M > 4` , then `J[:, 1:4:1, :]` means to get the values from row `1` to row `3` of `J`. In actual calculation, only the rows `1` to `3` are evaluated, and the calculation results of `1` to `3` will be cached at the granularity of the row, and will be used next time. When obtaining one or more rows of results above, the already calculated parts will not be recalculated.

Parameters
• ys (Union[paddle.Tensor, Tuple[paddle.Tensor, ...]]) – Output or tuple of outputs derived from xs.

• xs (Union[paddle.Tensor, Tuple[paddle.Tensor, ...]]) – Input or tuple of inputs.

• batch_axis (Optional[int], optional) – Index of batch axis. Defaults to None.

Returns

Jacobian(s) of ys derived from xs.

Return type

Union[Tuple[Tuple[Jacobian, …], …], Tuple[Jacobian, …], Jacobian]

Examples

```>>> import paddle

>>> x1 = paddle.randn([3, ])
>>> x2 = paddle.randn([3, ])
>>> x1.stop_gradient = False
>>> x2.stop_gradient = False

>>> y = x1 + x2

>>> J = paddle.autograd.jacobian(y, (x1, x2))
>>> J_y_x1 = J[0][:] # evaluate result of dy/dx1
>>> J_y_x2 = J[1][:] # evaluate result of dy/dx2

>>> print(J_y_x1.shape)
[3, 3]
>>> print(J_y_x2.shape)
[3, 3]
```