未验证 提交 6fbb975d 编写于 作者: C Chen Long 提交者: GitHub

Update while loop (#34229)

* update readme test=document_fix

* update while loop docs test=document_fix
上级 d4fb5c68
...@@ -1134,30 +1134,14 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None): ...@@ -1134,30 +1134,14 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
refer to :ref:`api_guide_Name`. Default is None. refer to :ref:`api_guide_Name`. Default is None.
Returns: Returns:
A list or tuple of tensors or LoDTensorArrays which returned by ``body`` . A list or tuple of Tensors or LoDTensorArrays which returned by ``body`` .
Returen type:
list(Variable)|tuple(Variable).
Raises:
TypeError: If the type of ``cond`` is not callable.
TypeError: If the type of ``body`` is not callable.
TypeError: If the type of ``loop_vars`` is not list or tuple.
TypeError: If the type of ``cond`` returns is not Variable.
TypeError: If the type of ``cond`` returns is not a boolean variable.
TypeError: If the shape of ``cond`` returns is not equals 1.
ValueError: If the ``var_loops`` is empty.
ValueError: If the length or type of ``body`` returns is not same as ``loop_vars``.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle import paddle
paddle.enable_static() paddle.enable_static()
def cond(i, ten): def cond(i, ten):
return i < ten return i < ten
...@@ -1165,14 +1149,14 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None): ...@@ -1165,14 +1149,14 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
i = i + 1 i = i + 1
return [i, ten] return [i, ten]
main_program = fluid.default_main_program() main_program = paddle.static.default_main_program()
startup_program = fluid.default_startup_program() startup_program = paddle.static.default_startup_program()
with fluid.program_guard(main_program, startup_program): with paddle.static.program_guard(main_program, startup_program):
i = layers.fill_constant(shape=[1], dtype='int64', value=0) # loop counter i = paddle.full(shape=[1], fill_value=0, dtype='int64') # loop counter
ten = layers.fill_constant(shape=[1], dtype='int64', value=10) # loop length ten = paddle.full(shape=[1], fill_value=10, dtype='int64') # loop length
i, ten = layers.while_loop(cond, body, [i, ten]) i, ten = paddle.static.nn.while_loop(cond, body, [i, ten])
exe = fluid.Executor(fluid.CPUPlace()) exe = paddle.static.Executor(paddle.CPUPlace())
res = exe.run(main_program, feed={}, fetch_list=[i]) res = exe.run(main_program, feed={}, fetch_list=[i])
print(res) # [array([10])] print(res) # [array([10])]
""" """
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册