While_cn.rst 1012 字节
Newer Older
H
Hao Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
.. _cn_api_fluid_layers_While:

While
-------------------------------

.. py:class:: paddle.fluid.layers.While (cond, is_test=False, name=None)


该类用于实现while循环控制功能。


参数:
    - **cond** (Variable) – 用于比较的条件
    - **is_test** (bool) – 用于表明是不是在测试阶段执行
    - **name** (str) - 该层的命名

**代码示例**

..  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)