Skip to content

Commit 8db945a

Browse files
TCChenlongjzhang533
authored andcommitted
Update while loop (#34229)
* update readme test=document_fix * update while loop docs test=document_fix
1 parent 519df32 commit 8db945a

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

python/paddle/fluid/layers/control_flow.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,45 +1133,29 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
11331133
refer to :ref:`api_guide_Name`. Default is None.
11341134
11351135
Returns:
1136-
A list or tuple of tensors or LoDTensorArrays which returned by ``body`` .
1137-
1138-
Returen type:
1139-
list(Variable)|tuple(Variable).
1140-
1141-
Raises:
1142-
TypeError: If the type of ``cond`` is not callable.
1143-
TypeError: If the type of ``body`` is not callable.
1144-
TypeError: If the type of ``loop_vars`` is not list or tuple.
1145-
TypeError: If the type of ``cond`` returns is not Variable.
1146-
TypeError: If the type of ``cond`` returns is not a boolean variable.
1147-
TypeError: If the shape of ``cond`` returns is not equals 1.
1148-
ValueError: If the ``var_loops`` is empty.
1149-
ValueError: If the length or type of ``body`` returns is not same as ``loop_vars``.
1136+
A list or tuple of Tensors or LoDTensorArrays which returned by ``body`` .
11501137
11511138
Examples:
11521139
.. code-block:: python
11531140
1154-
import paddle.fluid as fluid
1155-
import paddle.fluid.layers as layers
11561141
import paddle
11571142
paddle.enable_static()
11581143
1159-
11601144
def cond(i, ten):
11611145
return i < ten
11621146
11631147
def body(i, ten):
11641148
i = i + 1
11651149
return [i, ten]
11661150
1167-
main_program = fluid.default_main_program()
1168-
startup_program = fluid.default_startup_program()
1169-
with fluid.program_guard(main_program, startup_program):
1170-
i = layers.fill_constant(shape=[1], dtype='int64', value=0) # loop counter
1171-
ten = layers.fill_constant(shape=[1], dtype='int64', value=10) # loop length
1172-
i, ten = layers.while_loop(cond, body, [i, ten])
1151+
main_program = paddle.static.default_main_program()
1152+
startup_program = paddle.static.default_startup_program()
1153+
with paddle.static.program_guard(main_program, startup_program):
1154+
i = paddle.full(shape=[1], fill_value=0, dtype='int64') # loop counter
1155+
ten = paddle.full(shape=[1], fill_value=10, dtype='int64') # loop length
1156+
i, ten = paddle.static.nn.while_loop(cond, body, [i, ten])
11731157
1174-
exe = fluid.Executor(fluid.CPUPlace())
1158+
exe = paddle.static.Executor(paddle.CPUPlace())
11751159
res = exe.run(main_program, feed={}, fetch_list=[i])
11761160
print(res) # [array([10])]
11771161
"""

0 commit comments

Comments
 (0)