jvp

paddle.incubate.autograd. jvp ( func, xs, v=None ) [source]

Computes the Jacobian-Vector product for a function at the given inputs and a vector in the tangent space induced by the inputs.

Warning

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

Parameters
  • func (Callable) – The func takes as input a Tensor or a Sequence of Tensors and returns a Tensor or a Sequence of Tensors.

  • xs (Tensor|Sequence[Tensor]) – Used as positional arguments to evaluate func. The xs is accepted as one Tensor or a Sequence of Tensors.

  • v (Tensor|Sequence[Tensor]|None, Optional) – The tangent vector involved in the JVP computation. The v matches the size and shape of xs . Default value is None and in this case is equivalent to all ones the same size of xs .

Returns

  • func_out(Tensor|tuple[Tensor]): The output of func(xs) .

  • jvp(Tensor|tuple[Tensor]): The jvp result.

Return type

output(tuple)

Examples

>>> import paddle

>>> def func(x):
...     return paddle.matmul(x, x)
...
>>> x = paddle.ones(shape=[2, 2], dtype='float32')
>>> _, jvp_result = paddle.incubate.autograd.jvp(func, x)
>>> print(jvp_result)
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=False,
       [[4., 4.],
        [4., 4.]])

>>> v = paddle.to_tensor([[1.0, 0.0], [0.0, 0.0]])
>>> _, jvp_result = paddle.incubate.autograd.jvp(func, x, v)
>>> print(jvp_result)
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=False,
       [[2., 1.],
        [1., 0.]])