gradients

api_attr

declarative programming (static graph)

paddle.fluid.backward.gradients(targets, inputs, target_gradients=None, no_grad_set=None)[source]

Backpropagate the gradients of targets to inputs.

Parameters
  • targets (Variable|list[Variable]) – The target variables.

  • inputs (Variable|list[Variable]) – The input variables.

  • target_gradients (Variable|list[Variable], optional) – The gradient variables of targets which has the same shape with targets, If None, ones will be created for them.

  • no_grad_set (set[Variable|str], optional) – Set of Variables or Variable.names in the Block 0 whose gradients should be ignored. All variables with stop_gradient=True from all blocks will be automatically added into this set. If this parameter is not None, the Variables or Variable.names in this set will be added to the default set. Default: None.

Returns

A list of gradients for inputs If an input does not affect targets, the corresponding gradient variable will be None.

Return type

(list[Variable])

Examples

import paddle.fluid as fluid

x = fluid.data(name='x', shape=[None,2,8,8], dtype='float32')
x.stop_gradient=False
y = fluid.layers.conv2d(x, 4, 1, bias_attr=False)
y = fluid.layers.relu(y)
y = fluid.layers.conv2d(y, 4, 1, bias_attr=False)
y = fluid.layers.relu(y)
z = fluid.gradients([y], x)
print(z)