@@ -1133,45 +1133,29 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None):
1133
1133
refer to :ref:`api_guide_Name`. Default is None.
1134
1134
1135
1135
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`` .
1150
1137
1151
1138
Examples:
1152
1139
.. code-block:: python
1153
1140
1154
- import paddle.fluid as fluid
1155
- import paddle.fluid.layers as layers
1156
1141
import paddle
1157
1142
paddle.enable_static()
1158
1143
1159
-
1160
1144
def cond(i, ten):
1161
1145
return i < ten
1162
1146
1163
1147
def body(i, ten):
1164
1148
i = i + 1
1165
1149
return [i, ten]
1166
1150
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])
1173
1157
1174
- exe = fluid. Executor(fluid .CPUPlace())
1158
+ exe = paddle.static. Executor(paddle .CPUPlace())
1175
1159
res = exe.run(main_program, feed={}, fetch_list=[i])
1176
1160
print(res) # [array([10])]
1177
1161
"""
0 commit comments