backward¶
- paddle.Tensor. backward ( self: Tensor, grad_tensor: Tensor | None = None, retain_graph: bool = False ) None
-
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|None, optional) – initial gradient values of the current Tensor. If grad_tensor is None, the initial gradient values of the current Tensor would be Tensor filled with 1.0; if grad_tensor is not None, it must have the same length as the current Tensor. The default value is 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
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.)