From bd8dfe38caef4d4769fe2a57263fbe90a6891592 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Fri, 6 Nov 2020 09:48:31 +0800 Subject: [PATCH] [Dy2Stat] Refine code example for 2.0 (#28440) --- python/paddle/fluid/dygraph/jit.py | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/python/paddle/fluid/dygraph/jit.py b/python/paddle/fluid/dygraph/jit.py index 4e026dab66..3f9d5fb979 100644 --- a/python/paddle/fluid/dygraph/jit.py +++ b/python/paddle/fluid/dygraph/jit.py @@ -176,24 +176,20 @@ def declarative(function=None, input_spec=None): Examples: .. code-block:: python - import paddle.fluid as fluid - import numpy as np - from paddle.fluid.dygraph.jit import declarative - - fluid.enable_dygraph() - - @declarative - def func(x): - x = fluid.dygraph.to_variable(x) - if fluid.layers.mean(x) < 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - x = np.ones([1, 2]) - x_v = func(x) - print(x_v.numpy()) # [[2. 2.]] + import paddle + from paddle.jit import to_static + + @to_static + def func(x): + if paddle.mean(x) < 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + x = paddle.ones([1, 2], dtype='float32') + x_v = func(x) + print(x_v) # [[2. 2.]] """ -- GitLab