grad

paddle.autograd.ir_backward. grad ( outputs, inputs, grad_outputs=None, retain_graph=None, create_graph=False, only_inputs=True, allow_unused=False, no_grad_vars=None ) [source]

Note

This API is ONLY available in imperative mode.

This API computes the sum of gradients of outputs with respect to each inputs .

Parameters
  • outputs (Value|list(Value)|tuple(Value)) – the output Value or Value list/tuple of the graph to compute gradients.

  • inputs (Value|list(Value)|tuple(Value)) – the input Value or Value list/tuple of the graph to compute gradients. The returned values of this API are the gradients of inputs .

  • grad_outputs (Value|list(Value|None)|tuple(Value|None), optional) – initial gradient values of outputs . If grad_outputs is None, the initial gradient values of outputs would be Values filled with 1; if grad_outputs is not None, it must have the same length as outputs , and in this case, the initial gradient value of the i-th outputs would be: (1) a Value filled with 1 when the i-th element of grad_outputs is None; (2) the i-th element of grad_outputs when the i-th element of grad_outputs is a Value. Default None.

  • retain_graph (bool, optional) – whether to retain the forward graph which is used to calculate the gradient. When it is True, the graph would be retained, in which way users can calculate backward twice for the same graph. When it is False, the graph would be freed. Default None, which means it is equal to create_graph .

  • create_graph (bool, optional) – whether to create the gradient graphs of the computing process. When it is True, higher order derivatives are supported to compute; when it is False, the gradient graphs of the computing process would be discarded. Default False.

  • only_inputs (bool, optional) – whether to only compute the gradients of inputs . If it is False, the gradients of all remaining leaf Values in the graph would be also computed and accumulated. If it is True, only the gradients of inputs would be computed. Default True. only_inputs=False is under development, and it is not supported yet.

  • allow_unused (bool, optional) – whether to raise error or return None if some Values of inputs are unreachable in the graph. If some Values of inputs are unreachable in the graph (i.e., their gradients are None), error would be raised if allow_unused=False, or None would be returned as their gradients if allow_unused=True. Default False.

  • no_grad_vars (Value|list(Value)|tuple(Value)|set(Value), optional) – the Values whose gradients are not needed to compute. Default None.

Returns

a list of Values, whose length is the same as the Value number inside inputs, and the i-th returned Value is the sum of gradients of outputs with respect to the i-th inputs.

Return type

list