IfElse
该类用于实现IfElse分支控制功能, IfElse包含两个Block,true_block,false_block,IfElse会将满足True或False条件的数据分别放入不同的block运行。
# 以下代码完成的功能:对x中大于0的数据减去10,对x中小于0的数据加上10,并将所有的数据求和
import numpy as np
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[4, 1], dtype='float32', append_batch_size=False)
y = fluid.layers.data(name='y', shape=[4, 1], dtype='float32', append_batch_size=False)
x_d = np.array([[3], [1], [-2], [-3]]).astype(np.float32)
y_d = np.zeros((4, 1)).astype(np.float32)
# 比较x, y对元素的大小,输出cond, cond是shape为[4, 1],数据类型为bool的2-D tensor。
# 根据输入数据x_d, y_d,可以推断出cond中的数据为[[true], [true], [false], [false]]
cond = fluid.layers.greater_than(x, y)
# 同其他常见OP不同的是,该OP返回的ie是一个IfElse OP的对象
ie = fluid.layers.IfElse(cond)
with ie.true_block():
# 在这个block中,根据cond条件,获取x中对应条件为true维度的数据,并减去10
out_1 = ie.input(x)
out_1 = out_1 - 10
ie.output(out_1)
with ie.false_block():
# 在这个block中,根据cond条件,获取x中对应条件为false维度的数据,并加上10
out_1 = ie.input(x)
out_1 = out_1 + 10
ie.output(out_1)
# 根据cond条件将两个block中处理后的数据进行合并,此处的output为输出,类型为List,List中的元素类型为Variable。
output = ie() # [array([[-7.], [-9.], [ 8.], [ 7.]], dtype=float32)]
# 将输出List中的第一个Variable获取出来,并计算所有元素和
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=[output[0], out])
print(res)
输出:
[array([[-7.],
[-9.],
[ 8.],
[ 7.]], dtype=float32), array([-1.], dtype=float32)]
参考:IfElse
While
该类用于实现while循环控制功能,只要循环条件cond为True,就循环执行while循环体中的语句,直到cond为False为止。
# 该示例代码展示整数循环+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])]
输出:
[array([10], dtype=int64)]
参考:While
Switch
该类用于实现Switch分支控制功能。Switch分支包含多个case分支和一个default分支,Switch控制流会依次检查各case分支条件是否满足,并仅执行第一个满足条件的case分支后面的语句。若不存在满足条件的case分支,则仅执行default分支后面的语句。
import paddle.fluid as fluid
lr = fluid.layers.create_global_var(
shape=[1],
value=0.0,
dtype='float32',
persistable=True,
name="learning_rate")
zero_var = fluid.layers.fill_constant(
shape=[1], dtype='float32', value=0.0)
one_var = fluid.layers.fill_constant(
shape=[1], dtype='float32', value=1.0)
two_var = fluid.layers.fill_constant(
shape=[1], dtype='float32', value=2.0)
# 将参数中的begin设为非0值,则进入Switch的default分支,输出数组中的数字将为2
global_step = fluid.layers.autoincreased_step_counter(counter_name='@LR_DECAY_COUNTER@', begin=0, step=1)
with fluid.layers.control_flow.Switch() as switch:
with switch.case(global_step == zero_var):
fluid.layers.assign(input=one_var, output=lr)
with switch.default():
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)]
输出:
[array([1.], dtype=float32)]
参考:Switch
参考资料
下一篇:📃 常用操作