backward
- paddle.Tensor. backward ( self: Tensor, grad_tensor: Tensor | None = None, retain_graph: bool = False, *, dump_backward_graph_path: str | None = None ) 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_graphto 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
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: 500.0 1: 1000.0 2: 1500.0 3: 2000.0 4: 2500.0 >>> x.clear_grad() >>> print("{}".format(x.grad)) 0.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: 1000.0 1: 2000.0 2: 3000.0 3: 4000.0 4: 5000.0
