backward

paddle.Tensor. backward ( self, grad_tensor=None, retain_graph=False )

Run backward of current Graph which starts from current Tensor.

The new gradient will accumulate on previous gradient.

You can clear gradient by Tensor.clear_grad() .

Parameters
  • grad_tensor (Tensor, optional) – initial gradient values of the current Tensor. If grad_tensor is None,

  • 1.0; (the initial gradient values of the current Tensor would be Tensor filled with) –

  • None (if grad_tensor is not) –

  • Tensor. (it must have the same length as the current) –

  • None. (The default value is) –

  • 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.

Returns

None

Return type

NoneType

Examples

>>> import paddle
>>> x = paddle.to_tensor(5., stop_gradient=False)
>>> for i in range(5):
...     y = paddle.pow(x, 4.0)
...     y.backward()
...     print("{}: {}".format(i, x.grad))
0: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
500.)
1: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
1000.)
2: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
1500.)
3: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
2000.)
4: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
2500.)

>>> x.clear_grad()
>>> print("{}".format(x.grad))
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
0.)

>>> grad_tensor=paddle.to_tensor(2.)
>>> for i in range(5):
...     y = paddle.pow(x, 4.0)
...     y.backward(grad_tensor)
...     print("{}: {}".format(i, x.grad))
0: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
1000.)
1: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
2000.)
2: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
3000.)
3: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
4000.)
4: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=False,
5000.)