static_pylayer¶
- paddle.static.nn. static_pylayer ( forward_fn, inputs, backward_fn=None, name=None ) [source]
-
This API returns
forward_fn(inputs)
, and two sub-block are created based on the logic offorward_fn
andbackward_fn
, with the operatorpylayer
holding information about the two blocks.forward_fn
andbackward_fn
should return a nest structure of Variables. A nest structure of Variables in PaddlePaddle is Variable(s), or tuple of Variables, or list of Variables.Note
1. If
backward_fn
is not None, user needs to keep the number of Variable inputs toforward_fn
the same as the number of Variable outputs tobackward_fn
, and the number of Variable outputs toforward_fn
the same as the number of Variable inputs tobackward_fn
.2. If
backward_fn
is None,stop_gradient
attr of all Variable ininputs
is expected to be True. Otherwise it might get unexpected results in backward propagation.This API can only be used under static graph mode.
- Parameters
-
forward_fn (callable) – A callable to be performed in forward propagation
inputs (list[Variable]) – The list of input Variable to the
forward_fn
backward_fn (callable, optional) – A callable to be performed in backward propagation. Default: None, which means no need to do backward propagation.
name (str, optional) – The default value is
None
. Normally users don’t have to set this parameter. For more information, please refer to Name .
- Returns
-
returns the output of
forward_fn(inputs)
- Return type
-
Variable|list(Variable)|tuple(Variable)
Examples
>>> import paddle >>> import numpy as np >>> paddle.enable_static() >>> def forward_fn(x): ... return paddle.exp(x) >>> def backward_fn(dy): ... return 2 * paddle.exp(dy) >>> main_program = paddle.static.Program() >>> start_program = paddle.static.Program() >>> place = paddle.CPUPlace() >>> exe = paddle.static.Executor(place) >>> with paddle.static.program_guard(main_program, start_program): ... data = paddle.static.data(name="X", shape=[None, 5], dtype="float32") ... data.stop_gradient = False ... ret = paddle.static.nn.static_pylayer(forward_fn, [data], backward_fn) ... data_grad = paddle.static.gradients([ret], data)[0] >>> exe.run(start_program) >>> x = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32) >>> x, x_grad, y = exe.run( ... main_program, ... feed={"X": x}, ... fetch_list=[ ... data.name, ... data_grad.name, ... ret.name ... ], ... ) >>> print(x) [[1. 2. 3. 4. 5.]] >>> print(x_grad) [[5.4365635 5.4365635 5.4365635 5.4365635 5.4365635]] >>> print(y) [[ 2.7182817 7.389056 20.085537 54.59815 148.41316 ]]