backward

paddle.autograd. backward ( tensors: Tensor | Sequence[Tensor], grad_tensors: Tensor | Sequence[Tensor | None] | None = None, retain_graph: bool = False, *, dump_backward_graph_path: str | None = None ) None [source]

Compute the backward gradients of given tensors.

Parameters
  • tensors (list of Tensors) – the tensors which the gradient to be computed. The tensors can not contain the same tensor.

  • grad_tensors (list of Tensors of None, optional) – the init gradients of the tensors` .If not None, it must have the same length with tensors , and if any of the elements is None, then the init gradient is the default value which is filled with 1.0. If None, all the gradients of the tensors is the default value which is filled with 1.0. Defaults to None.

  • retain_graph (bool, optional) – If False, the graph used to compute grads will be freed. If you would like to add more ops to the built graph after calling this method( backward ), set the parameter retain_graph to True, then the grads will be retained. Thus, setting it to False is much more memory-efficient. Defaults to False.

  • dump_backward_graph_path (str, optional) – Specifies the directory path for storing the debug file. If this parameter is specified, the backward-related graph (in dot format) and the debugging call stack information will be generated in this directory.

Returns

None

Return type

NoneType

Examples

>>> import paddle
>>> x = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32', stop_gradient=False)
>>> y = paddle.to_tensor([[3, 2], [3, 4]], dtype='float32')

>>> grad_tensor1 = paddle.to_tensor([[1,2], [2, 3]], dtype='float32')
>>> grad_tensor2 = paddle.to_tensor([[1,1], [1, 1]], dtype='float32')

>>> z1 = paddle.matmul(x, y)
>>> z2 = paddle.matmul(x, y)

>>> paddle.autograd.backward([z1, z2], [grad_tensor1, grad_tensor2], True)
>>> print(x.grad)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[12., 18.],
 [17., 25.]])


>>> x.clear_grad()

>>> paddle.autograd.backward([z1, z2], [grad_tensor1, None], True)
>>> print(x.grad)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[12., 18.],
 [17., 25.]])

>>> x.clear_grad()

>>> paddle.autograd.backward([z1, z2])
>>> print(x.grad)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
[[10., 14.],
 [10., 14.]])