From 0ce933a9eeebbf079dd70e7df22861a32f32f16e Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Wed, 11 Nov 2020 10:44:09 +0800 Subject: [PATCH] [API2.0] Fix documents of 6 APIs to fit API2.0: (#28514) 1. Remove 'fluid'; 2. Variable -> Tensor APIs: sum, convert_call, convert_ifelse, convert_logical_and, convert_logical_or, convert_logical_not --- .../dygraph_to_static/convert_call_func.py | 40 ++++++++++--------- .../dygraph_to_static/convert_operators.py | 18 ++++----- python/paddle/tensor/math.py | 1 - 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py b/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py index 9654a238520..bd7f51d89b2 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py @@ -98,25 +98,27 @@ def convert_call(func): Examples: .. code-block:: python - import paddle.fluid as fluid - from paddle.fluid.dygraph.dygraph_to_static import convert_call - - def dyfunc(x): - if fluid.layers.mean(x) < 0: - x_v = x - 1 - else: - x_v = x + 1 - - return x_v - new_func = convert_call(dyfunc) - x = fluid.layers.fill_constant(shape=[3, 3], value=0, dtype='float64') - x_v = new_func(x) - exe = fluid.Executor(fluid.CPUPlace()) - out = exe.run(fetch_list=[x_v]) - print(out[0]) - # [[1. 1. 1.] - # [1. 1. 1.] - # [1. 1. 1.]] + import paddle + from paddle.jit.dy2static import convert_call + + paddle.enable_static() + def dyfunc(x): + if paddle.mean(x) < 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + new_func = convert_call(dyfunc) + x = paddle.tensor.manipulation.fill_constant(shape=[3, 3], value=0, dtype='float64') + x_v = new_func(x) + + exe = paddle.static.Executor(paddle.CPUPlace()) + out = exe.run(fetch_list=[x_v]) + print(out[0]) + # [[1. 1. 1.] + # [1. 1. 1.] + # [1. 1. 1.]] """ translator_logger.log(1, diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py b/python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py index 02d8754e62c..f64d97569fe 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py @@ -24,7 +24,7 @@ def convert_while_loop(cond, body, loop_vars): A function representation of a Python ``while`` statement. Args: - cond(Callable): A callable object that returns a boolean variable to control whether to execute the loop body. It takes ``loop_vars`` as arguments. + cond(Callable): A callable object that returns a boolean variable to control whether to execute the loop body. It takes ``loop_vars`` as arguments. body(Callable): A callable object that returns a tuple or list of variables with the same arguments ``loops_vars`` as ``cond`` . loop_vars(list|tuple): A list or tuple of variables passed to ``cond`` and ``body`` . @@ -44,7 +44,7 @@ def convert_while_loop(cond, body, loop_vars): def _run_paddle_while_loop(cond, body, loop_vars): - # NOTE: loop_vars of Paddle op `control_flow.while_loop` must be Paddle Variable. + # NOTE: loop_vars of Paddle op `control_flow.while_loop` must be Paddle Tensors. loop_vars = [to_static_variable(var) for var in loop_vars] loop_vars = control_flow.while_loop(cond, body, loop_vars) return loop_vars @@ -61,8 +61,8 @@ def convert_logical_and(x, y): A function representation of a Python ``and`` statement. Args: - x(bool|Variable): Left hand operand of ``and`` operator. - y(bool|Variable): Right hand operand of ``and`` operator. + x(bool|Tensor): Left hand operand of ``and`` operator. + y(bool|Tensor): Right hand operand of ``and`` operator. Returns: A python bool variable or a bool Tensor. @@ -94,8 +94,8 @@ def convert_logical_or(x, y): A function representation of a Python ``or`` statement. Args: - x(bool|Variable): Left hand operand of ``or`` operator. - y(bool|Variable): Right hand operand of ``or`` operator. + x(bool|Tensor): Left hand operand of ``or`` operator. + y(bool|Tensor): Right hand operand of ``or`` operator. Returns: A python bool variable or a bool Tensor. @@ -127,7 +127,7 @@ def convert_logical_not(x): A function representation of a Python ``not`` statement. Args: - x(bool|Variable): Operand of of ``not`` operator. + x(bool|Tensor): Operand of of ``not`` operator. Returns: A python bool variable or a bool Tensor. @@ -153,7 +153,7 @@ def convert_ifelse(pred, true_fn, false_fn, true_args, false_args, return_vars): A function representation of a Python ``if/else`` statement. Args: - pred(bool|Variable): A boolean variable which determines whether to return the result of ``true_fn`` or ``false_fn`` . + pred(bool|Tensor): A boolean Tensor which determines whether to return the result of ``true_fn`` or ``false_fn`` . true_fn(callable): A callable to be performed if ``pred`` is true. false_fn(callable): A callable to be performed if ``pred`` is false. true_args(tuple): Parameters of ``true_fn``. @@ -175,7 +175,7 @@ def _run_paddle_cond(pred, true_fn, false_fn, true_args, false_args, return_vars): return_var_ids = [id(var) for var in return_vars] - # NOTE 1: return vars of Paddle op `control_flow.cond` must be Paddle Variable + # NOTE 1: Returned vars of Paddle op `control_flow.cond` must be Paddle Tensors # NOTE 2: Here uses id(var) not var, because `if var in return_var` use operator `==`, # which will call `fluid.layers.equal` and causes error when var in return_vars is not initialized. true_args = [ diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index d2e9340e8a8..56933cf73ef 100755 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -627,7 +627,6 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): .. code-block:: python import paddle - paddle.disable_static() # x is a Tensor with following elements: # [[0.2, 0.3, 0.5, 0.9] -- GitLab