paddle.static. gradients ( targets, inputs, target_gradients=None, no_grad_set=None ) [source]

Backpropagate the gradients of targets to inputs.

  • targets (Tensor|list[Tensor]|tuple[Tensor]) – The target Tensors.

  • inputs (Tensor|list[Tensor]|tuple[Tensor]) – The input Tensors.

  • target_gradients (Tensor|list[Tensor]|tuple[Tensor], optional) – The gradient Tensor of targets which has the same shape with targets, If None, ones will be created for them.

  • no_grad_set (set[Tensor|str], optional) – Set of Tensors or Tensor.names in the Block 0 whose gradients should be ignored. All Tensors with stop_gradient=True from all blocks will be automatically added into this set. If this parameter is not None, the Tensors or Tensor.names in this set will be added to the default set. Default: None.


A list of gradients for inputs If an input does not affect targets, the corresponding gradient Tensor will be None.

Return type



>>> import paddle
>>> import paddle.nn.functional as F

>>> paddle.enable_static()

>>> x ='x', shape=[None, 2, 8, 8], dtype='float32')
>>> x.stop_gradient=False
>>> y = paddle.static.nn.conv2d(x, 4, 1, bias_attr=False)
>>> y = F.relu(y)
>>> z = paddle.static.gradients([y], x)
>>> print(z)
[var x@GRAD : LOD_TENSOR.shape(-1, 2, 8, 8).dtype(float32).stop_gradient(False)]