From ea6a251c0bd7c3e77e03c2709376156bd48caad1 Mon Sep 17 00:00:00 2001 From: zhongpu <2013000149@qq.com> Date: Tue, 7 Apr 2020 18:57:45 +0800 Subject: [PATCH] fix sample code, test=develop (#23448) --- python/paddle/fluid/dygraph/layers.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/dygraph/layers.py b/python/paddle/fluid/dygraph/layers.py index 94ac86b774..2416e76c3c 100644 --- a/python/paddle/fluid/dygraph/layers.py +++ b/python/paddle/fluid/dygraph/layers.py @@ -122,6 +122,7 @@ class Layer(core.Layer): .. code-block:: python import paddle.fluid as fluid + import numpy as np # the forward_post_hook change the output of the layer: output = output * 2 def forward_post_hook(layer, input, output): @@ -136,15 +137,15 @@ class Layer(core.Layer): # register the hook forward_post_hook_handle = linear.register_forward_post_hook(forward_post_hook) - value = np.arange(26).reshape(2, 13).astype("float32") - in = fluid.dygraph.to_variable(value0) + value1 = np.arange(26).reshape(2, 13).astype("float32") + in1 = fluid.dygraph.to_variable(value1) - out0 = linear(in) + out0 = linear(in1) # remove the hook forward_post_hook_handle.remove() - out1 = linear(in) + out1 = linear(in1) # hook change the linear's output to output * 2, so out0 is equal to out1 * 2. assert (out0.numpy() == (out1.numpy()) * 2).any() @@ -173,6 +174,7 @@ class Layer(core.Layer): .. code-block:: python import paddle.fluid as fluid + import numpy as np # the forward_post_hook change the input of the layer: input = input * 2 def forward_pre_hook(layer, input): -- GitLab