Jacobian

class paddle.incubate.autograd. Jacobian ( func, xs, is_batched=False ) [source]

Computes the Jacobian matrix of a given function.

If the function has multiple inputs and multiple outputs, during internal implementation, all input tensors are concatenated after being flatten, the batch dimension is retained, and the output is subject to the same processing rules.

Once the Jacobian J is constructed, you can use a multidimensional index to retrieve the submatrix of J, as same as slicing a Tensor. The submatrix is lazily evaluated along row axis, and will be cached once evaluated.

For examples, supposing is_batched=True, you can retrieve the submatrix by following methods:

  • J[:], retrieving the full matrix.

  • J[:, :, j], retrieving the partial derivatives w.r.t. the j’th input variable.

  • J[:, i, :], retrieving the partial derivatives w.r.t. the i’th output variable.

  • J[:, i, j], retrieving the partial derivatives w.r.t. the i’th output variable and the j’th input variable.

Notes

Eclipsis index is not supported currently.

Warning

This API is in beta, the signatures could be changed in future version.

Parameters
  • func (Callable) – A python function that takes a Tensor or a sequence of Tensors as inputs(the first dimension is batch size) and returns a Tensor a sequence of Tensors.

  • xs (Tensor|Sequence[Tensor]) – The input to the function func .

  • is_batched (bool) – If true, the first axis is batch axis. Defaults to False.

Returns

A python object retains the Jacobian matrix.

Return type

Jacobian (Object)

Examples

>>> import paddle

>>> def func(x, y):
...     return paddle.matmul(x, y)
...
>>> x = paddle.to_tensor([[1., 2.], [3., 4.]])
>>> J = paddle.incubate.autograd.Jacobian(func, [x, x])
>>> print(J[:, :])
Tensor(shape=[4, 8], dtype=float32, place=Place(cpu), stop_gradient=False,
       [[1., 3., 0., 0., 1., 0., 2., 0.],
        [2., 4., 0., 0., 0., 1., 0., 2.],
        [0., 0., 1., 3., 3., 0., 4., 0.],
        [0., 0., 2., 4., 0., 3., 0., 4.]])

>>> print(J[0, :])
Tensor(shape=[8], dtype=float32, place=Place(cpu), stop_gradient=False,
       [1., 3., 0., 0., 1., 0., 2., 0.])
>>> print(J[:, 0])
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=False,
       [1., 2., 0., 0.])
property shape

The shape of flattened Jacobian matrix.