grad¶
- paddle.incubate.autograd. grad ( outputs, inputs, grad_outputs=None ) [source]
-
Reverse mode of automatic differentiation.
Note
ONLY available in the static graph mode and primitive operators
- Parameters
-
outputs (Tensor|Sequence[Tensor]) – The output Tensor or Tensors.
inputs (Tensor|Sequence[Tensor]) – The input Tensor or Tensors.
grad_outputs (Tensor|Sequence[Tensor]) – Optional, the gradient Tensor or Tensors of outputs which has the same shape with outputs, Defaults to None, in this case is equivalent to all ones.
- Returns
-
The gradients for inputs.
- Return type
-
grad_inputs(Tensor|Tensors)
Examples
>>> import numpy as np >>> import paddle >>> paddle.enable_static() >>> paddle.incubate.autograd.enable_prim() >>> startup_program = paddle.static.Program() >>> main_program = paddle.static.Program() >>> with paddle.static.program_guard(main_program, startup_program): ... x = paddle.static.data('x', shape=[1], dtype='float32') ... x.stop_gradients = False ... y = x * x ... x_grad = paddle.incubate.autograd.grad(y, x) ... paddle.incubate.autograd.prim2orig() ... >>> exe = paddle.static.Executor() >>> exe.run(startup_program) >>> x_grad = exe.run(main_program, feed={'x': np.array([2.]).astype('float32')}, fetch_list=[x_grad]) >>> print(x_grad) [array([4.], dtype=float32)] >>> paddle.incubate.autograd.disable_prim() >>> paddle.disable_static()