backward¶
- paddle.autograd. backward ( tensors, grad_tensors=None, retain_graph=False ) [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 thetensors
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 parameterretain_graph
to True, then the grads will be retained. Thus, setting it to False is much more memory-efficient. Defaults to False.
- 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.]])