@@ -643,29 +643,34 @@ class WhileGuard(BlockGuard):
...
@@ -643,29 +643,34 @@ class WhileGuard(BlockGuard):
classWhile(object):
classWhile(object):
"""
"""
while loop control flow.
while loop control flow. Repeat while body until cond is False.
Args:
Args:
cond(Variable): condition used to compare.
cond(Variable): A Tensor whose data type is bool controlling whether to continue looping.
is_test(bool): A flag indicating whether execution is in test phase.
is_test(bool, optional): A flag indicating whether execution is in test phase. Default value is None.
name(str): The name of this layer.
name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` .
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle.fluid as fluid
import paddle.fluid as fluid
import numpy as np
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0) # loop counter
res = exe.run(fluid.default_main_program(), feed={}, fetch_list=[i])
print(res) # [array([10])]
"""
"""
BEFORE_WHILE_BLOCK=0
BEFORE_WHILE_BLOCK=0
...
@@ -1464,18 +1469,35 @@ class ConditionalBlock(object):
...
@@ -1464,18 +1469,35 @@ class ConditionalBlock(object):
classSwitch(object):
classSwitch(object):
"""
"""
Switch class works just like a `if-elif-else`. Can be used in learning rate scheduler
to modify learning rate
The Semantics:
This class is used to implement Switch branch control function.
Switch branch contains several case branches and one default branch.
Switch control flow checks whether the case branch conditions are satisfied in turn,
and only executes the statement after the first case branch that satisfies the conditions.
If there is no case branch that satisfies the condition,
only the statement following the default branch is executed.
1. A `switch` control-flow checks cases one-by-one.
Member Functions:
case(cond): The case branch of Switch whose parameter cond is a scalar Variable of bool type. Only if the cond of the current case branch is True and the cond of the previous case branch is False, the statement after the case branch will be executed, and the statement after the case branch will not be executed.
2. The condition of each case is a boolean value, which is a scalar Variable.
default(): The default branch of Switch. When cond of all case branches is False, the statement after default branch is executed.
3. It runs the first matched case, or the default case if there is one.
Case and default functions can only be used inside the scope of Switch, as shown below:
4. Once it matches a case, it runs the corresponding branch and only that branch.
.. code-block:: python
'''
with fluid.layers.Switch() as switch:
with switch.case(cond1):
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=1)
with switch.case(cond2):
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=2)
with switch.default():
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
'''
Args:
name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` .
with fluid.layers.control_flow.Switch() as switch:
with fluid.layers.control_flow.Switch() as switch:
with switch.case(global_step == zero_var):
with switch.case(global_step == zero_var):
...
@@ -1504,6 +1525,11 @@ class Switch(object):
...
@@ -1504,6 +1525,11 @@ class Switch(object):
with switch.default():
with switch.default():
fluid.layers.assign(input=two_var, output=lr)
fluid.layers.assign(input=two_var, output=lr)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
res = exe.run(fluid.default_main_program(), feed={}, fetch_list=[lr])
print(res) # [array([1.], dtype=float32)]
"""
"""
def__init__(self,name=None):
def__init__(self,name=None):
...
@@ -1592,36 +1618,72 @@ class IfElseBlockGuard(object):
...
@@ -1592,36 +1618,72 @@ class IfElseBlockGuard(object):
classIfElse(object):
classIfElse(object):
"""
"""
if-else control flow.
This class is used to implement IfElse branch control function. IfElse contains two blocks, true_block and false_block. IfElse will put data satisfying True or False conditions into different blocks to run.
Args:
Cond is a 2-D Tensor with shape [N, 1] and data type bool, representing the execution conditions of the corresponding part of the input data.
cond (Variable): condition used to compare.
name (str, default None): The name of this layer.
IfElse OP is different from other OPs in usage, which may cause some users confusion. Here is a simple example to illustrate this OP.
Examples:
.. code-block:: python
.. code-block:: python
# The following code completes the function: subtract 10 from the data greater than 0 in x, add 10 to the data less than 0 in x, and sum all the data.
# According to cond condition, the data processed in the two blocks are merged. The output here is output, the type is List, and the element type in List is Variable.
# Get the first Variable in the output List and add all elements.
out = fluid.layers.reduce_sum(output[0])
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
res = exe.run(fluid.default_main_program(), feed={"x":x_d, "y":y_d}, fetch_list=[out])
print res
# [array([-1.], dtype=float32)]
Args:
cond (Variable): cond is a 2-D Tensor with shape [N, 1] and data type bool, representing the corresponding execution conditions of N input data. The data type is bool.
name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` .
Returns:
Unlike other common OPs, the OP call returns an IfElse OP object (e.g. ie in the example), which branches the input data by calling the internal functions of the object ``true_block ()``, ``false_block ()``, ``input ()``, ``output ()``, and integrates the data processed by different branches as the overall output by calling the internal ``call ()`` function. The output type is a list, and the type of each element in the list is Variable.
Internal Functions:
The block is constructed by calling the ``with ie. true_block()`` function in the object, and the computational logic under condition true is put into the block. If no corresponding block is constructed, the input data in the corresponding conditional dimension is unchanged.
The block is constructed by calling the ``with ie. false_block()`` function in the object, and the computational logic under condition false is put into the block. If no corresponding block is constructed, the input data in the corresponding conditional dimension is unchanged.
``Out = ie. input (x)`` will take out the data of the corresponding conditional dimension in X and put it into out, supporting the internal processing of multiple inputs in block.
``ie. output (out)`` writes the result to the output of the corresponding condition.
There is a ``call ()`` function inside the object, that is, by calling ``output = ie ()``, all the outputs inside the block of False are fused as the whole output, the output type is a list, and the type of each element in the list is Variable.