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

Computes the Hessian matrix with a given func with respect to xs .

If the function has multiple inputs, during internal implementation, all input tensors are concatenated after being flatten, the batch dimension is retained.

The Hessian submatrix is lazily evaluated, and can be retrieved with a multidimensional indexes. See details Jacobian .


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

  • func (Callable) – A python function that takes a Tensor or a Tensor sequence as inputs and returns a Tensor with shape [batch_size, 1] with batch or [1] without batch.

  • xs (Tensor|Sequence(Tensor)) – The input Tensor or Tensor sequence of the function func.

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


A python object retains the Hessian matrix.

Return type

Hessian (Object)


import paddle

def reducer(x):
    return paddle.sum(x * x)

x = paddle.rand([2, 2])
h = paddle.incubate.autograd.Hessian(reducer, x)
# Tensor(shape=[4, 4], dtype=float32, place=Place(gpu:0), stop_gradient=False,
#        [[2., 0., 0., 0.],
#         [0., 2., 0., 0.],
#         [0., 0., 2., 0.],
#         [0., 0., 0., 2.]])
property shape

The shape of flattened Hessian matrix.