gradients

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]|None) – The gradient variables of targets which has the same shape with targets, If None, ones will be created for them.

  • no_grad_set (set[string]) – The names of variables that have no gradients in Block 0. All variables with stop_gradient=True from all blocks will be automatically added.

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.layers.data(name='x', shape=[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)