未验证 提交 c7602c59 编写于 作者: P Pei Yang 提交者: GitHub

Refine API docs of While (#1317)

* refine While doc, test=develop

* add refine Switch api doc, test=develop

* refine While, delete Switch, test=develop, test=document_preview
上级 faee210d
......@@ -52,4 +52,3 @@ Switch类实现的功能十分类似if-elif-else。它可以在学习率调度
......@@ -6,32 +6,38 @@ While
.. py:class:: paddle.fluid.layers.While (cond, is_test=False, name=None)
该类用于实现while循环控制功能。
该类用于实现while循环控制功能,只要循环条件cond为True,就循环执行while循环体中的语句,直到cond为False为止
参数:
- **cond** (Variable) – 用于比较的条件
- **is_test** (bool) – 用于表明是不是在测试阶段执行
- **name** (str) - 该层的命名
- **cond** (Variable) – 用于判断循环继续进行的条件,为数据类型bool型的Tensor,其shape必须为[1]。
- **is_test** (bool,可选) – 用于表明是否在测试阶段执行,默认值为False。
- **name** (str,可选) - 该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` ,默认值为None。
**代码示例**
.. code-block:: python
import paddle.fluid as fluid
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
d0 = fluid.layers.data("d0", shape=[10], dtype='float32')
data_array = fluid.layers.array_write(x=d0, i=i)
array_len = fluid.layers.fill_constant(shape=[1],dtype='int64', value=3)
cond = fluid.layers.less_than(x=i, y=array_len)
while_op = fluid.layers.While(cond=cond)
with while_op.block():
d = fluid.layers.array_read(array=data_array, i=i)
i = fluid.layers.increment(x=i, value=1, in_place=True)
fluid.layers.less_than(x=i, y=array_len, cond=cond)
# 该示例代码展示整数循环+1,循环10次,输出计数结果
import paddle.fluid as fluid
import numpy as np
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0) # 循环计数器
loop_len = fluid.layers.fill_constant(shape=[1],dtype='int64', value=10) # 循环次数
cond = fluid.layers.less_than(x=i, y=loop_len) # 循环条件
while_op = fluid.layers.While(cond=cond)
with while_op.block(): # 循环体
i = fluid.layers.increment(x=i, value=1, in_place=True)
fluid.layers.less_than(x=i, y=loop_len, cond=cond) # 更新循环条件
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
res = exe.run(fluid.default_main_program(), feed={}, fetch_list=[i])
print(res) # [array([10])]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册