IfElse

该类用于实现IfElse分支控制功能, IfElse包含两个Block,true_block,false_block,IfElse会将满足True或False条件的数据分别放入不同的block运行。

  1. # 以下代码完成的功能:对x中大于0的数据减去10,对x中小于0的数据加上10,并将所有的数据求和
  2. import numpy as np
  3. import paddle.fluid as fluid
  4. x = fluid.layers.data(name='x', shape=[4, 1], dtype='float32', append_batch_size=False)
  5. y = fluid.layers.data(name='y', shape=[4, 1], dtype='float32', append_batch_size=False)
  6. x_d = np.array([[3], [1], [-2], [-3]]).astype(np.float32)
  7. y_d = np.zeros((4, 1)).astype(np.float32)
  8. # 比较x, y对元素的大小,输出cond, cond是shape为[4, 1],数据类型为bool的2-D tensor。
  9. # 根据输入数据x_d, y_d,可以推断出cond中的数据为[[true], [true], [false], [false]]
  10. cond = fluid.layers.greater_than(x, y)
  11. # 同其他常见OP不同的是,该OP返回的ie是一个IfElse OP的对象
  12. ie = fluid.layers.IfElse(cond)
  13. with ie.true_block():
  14. # 在这个block中,根据cond条件,获取x中对应条件为true维度的数据,并减去10
  15. out_1 = ie.input(x)
  16. out_1 = out_1 - 10
  17. ie.output(out_1)
  18. with ie.false_block():
  19. # 在这个block中,根据cond条件,获取x中对应条件为false维度的数据,并加上10
  20. out_1 = ie.input(x)
  21. out_1 = out_1 + 10
  22. ie.output(out_1)
  23. # 根据cond条件将两个block中处理后的数据进行合并,此处的output为输出,类型为List,List中的元素类型为Variable。
  24. output = ie() # [array([[-7.], [-9.], [ 8.], [ 7.]], dtype=float32)]
  25. # 将输出List中的第一个Variable获取出来,并计算所有元素和
  26. out = fluid.layers.reduce_sum(output[0])
  27. exe = fluid.Executor(fluid.CPUPlace())
  28. exe.run(fluid.default_startup_program())
  29. res = exe.run(fluid.default_main_program(), feed={"x":x_d, "y":y_d}, fetch_list=[output[0], out])
  30. print(res)

输出:

  1. [array([[-7.],
  2. [-9.],
  3. [ 8.],
  4. [ 7.]], dtype=float32), array([-1.], dtype=float32)]

参考:IfElse

While

该类用于实现while循环控制功能,只要循环条件cond为True,就循环执行while循环体中的语句,直到cond为False为止。

  1. # 该示例代码展示整数循环+1,循环10次,输出计数结果
  2. import paddle.fluid as fluid
  3. import numpy as np
  4. i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0) # 循环计数器
  5. loop_len = fluid.layers.fill_constant(shape=[1],dtype='int64', value=10) # 循环次数
  6. cond = fluid.layers.less_than(x=i, y=loop_len) # 循环条件
  7. while_op = fluid.layers.While(cond=cond)
  8. with while_op.block(): # 循环体
  9. i = fluid.layers.increment(x=i, value=1, in_place=True)
  10. fluid.layers.less_than(x=i, y=loop_len, cond=cond) # 更新循环条件
  11. exe = fluid.Executor(fluid.CPUPlace())
  12. exe.run(fluid.default_startup_program())
  13. res = exe.run(fluid.default_main_program(), feed={}, fetch_list=[i])
  14. print(res) # [array([10])]

输出:

  1. [array([10], dtype=int64)]

参考:While

Switch

该类用于实现Switch分支控制功能。Switch分支包含多个case分支和一个default分支,Switch控制流会依次检查各case分支条件是否满足,并仅执行第一个满足条件的case分支后面的语句。若不存在满足条件的case分支,则仅执行default分支后面的语句。

  1. import paddle.fluid as fluid
  2. lr = fluid.layers.create_global_var(
  3. shape=[1],
  4. value=0.0,
  5. dtype='float32',
  6. persistable=True,
  7. name="learning_rate")
  8. zero_var = fluid.layers.fill_constant(
  9. shape=[1], dtype='float32', value=0.0)
  10. one_var = fluid.layers.fill_constant(
  11. shape=[1], dtype='float32', value=1.0)
  12. two_var = fluid.layers.fill_constant(
  13. shape=[1], dtype='float32', value=2.0)
  14. # 将参数中的begin设为非0值,则进入Switch的default分支,输出数组中的数字将为2
  15. global_step = fluid.layers.autoincreased_step_counter(counter_name='@LR_DECAY_COUNTER@', begin=0, step=1)
  16. with fluid.layers.control_flow.Switch() as switch:
  17. with switch.case(global_step == zero_var):
  18. fluid.layers.assign(input=one_var, output=lr)
  19. with switch.default():
  20. fluid.layers.assign(input=two_var, output=lr)
  21. exe = fluid.Executor(fluid.CPUPlace())
  22. exe.run(fluid.default_startup_program())
  23. res = exe.run(fluid.default_main_program(), feed={}, fetch_list=[lr])
  24. print(res) # [array([1.], dtype=float32)]

输出:

  1. [array([1.], dtype=float32)]

参考:Switch

参考资料