Paddle
Paddle copied to clipboard
请问IfElse Block是否支持嵌套?
我需要使用 IfElse 结构实现一个三选一的逻辑,于是我这样写了一段代码:
ie1 = fluid.layers.IfElse(cond1)
with ie1.true_block():
_in = rk1_in
_eta = rk1_eta
ie1.output(_in, _eta)
with ie1.false_block():
tmp_rk2_in = rk2_in
tmp_rk2_eta = rk2_eta
tmp_rk3_in = rk3_in
tmp_rk3_eta = rk3_eta
cond2 = fluid.layers.less_than(select, third)
ie2 = fluid.layers.IfElse(cond2)
with ie2.true_block():
tmp_in = tmp_rk2_in
tmp_eta = tmp_rk2_eta
ie2.output(tmp_in, tmp_eta)
with ie2.false_block():
tmp_in = tmp_rk3_in
tmp_eta = tmp_rk3_eta
ie2.output(tmp_in, tmp_eta)
_in, _eta = ie2()
ie1.output(_in, _eta)
其中rk1_in, rk1_eta, rk2_in, rk2_eta, rk3_in, rk3_eta均为fluid.layers.data类型,name属性即为变量名 然后运行之后得到如下信息:
> Traceback (most recent call last):
File "tmp.py", line 11, in <module>
network_fc.init_net(30, 6)
File "/home/zzhzz/tbcnn/classifier/tbcnn_fluid/network_fc.py", line 64, in init_net
ie1.output(_in, _eta)
File "/usr/local/lib/python2.7/dist-packages/paddle/v2/fluid/layers/control_flow.py", line 1143, in __exit__
if not self.cond_block.__exit__(exc_type, exc_val, exc_tb):
File "/usr/local/lib/python2.7/dist-packages/paddle/v2/fluid/layers/control_flow.py", line 1060, in __exit__
self.block.complete()
File "/usr/local/lib/python2.7/dist-packages/paddle/v2/fluid/layers/control_flow.py", line 1097, in complete
if each_name not in input_set
File "/usr/local/lib/python2.7/dist-packages/paddle/v2/fluid/framework.py", line 652, in var
raise ValueError("var %s not in this block" % name)
ValueError: var rk2_tree not in this block
所以我想请问IfElse是否支持嵌套?若支持的话有什么需要注意的地方?
是否可以将代码贴全,我看报错提到了 rk2_tree, 但在给出的代码中没有看见这个。
#the tree size is fea*max_tree*max_child
rk1_in = fluid.layers.data(
name='rk1_tree', shape=[feature_size, rk1_numNodes, rk1_numChild]
)
rk1_eta = fluid.layers.data(
name='rk1_eta', shape=[rk1_numNodes, rk1_numChild, 3]
)
rk2_in = fluid.layers.data(
name='rk2_tree', shape=[feature_size, rk2_numNodes, rk2_numChild]
)
rk2_eta = fluid.layers.data(
name='rk2_eta', shape=[rk2_numNodes, rk2_numChild, 3]
)
rk3_in = fluid.layers.data(
name='rk3_tree', shape=[feature_size, rk3_numNodes, rk3_numChild]
)
rk3_eta = fluid.layers.data(
name='rk3_eta', shape=[rk3_numNodes, rk3_numChild, 3]
)
select = fluid.layers.data(
name='select', shape=[1], dtype='int'
)
second = fluid.layers.fill_constant_batch_size_like(select, [1], 'int', 2)
third = fluid.layers.fill_constant_batch_size_like(select, [1], 'int', 3)
cond1 = fluid.layers.less_than(select, second)
ie1 = fluid.layers.IfElse(cond1)
with ie1.true_block():
_in = rk1_in
_eta = rk1_eta
ie1.output(_in, _eta)
with ie1.false_block():
tmp_rk2_in = rk2_in
tmp_rk2_eta = rk2_eta
tmp_rk3_in = rk3_in
tmp_rk3_eta = rk3_eta
cond2 = fluid.layers.less_than(select, third)
ie2 = fluid.layers.IfElse(cond2)
with ie2.true_block():
tmp_in = tmp_rk2_in
tmp_eta = tmp_rk2_eta
ie2.output(tmp_in, tmp_eta)
with ie2.false_block():
tmp_in = tmp_rk3_in
tmp_eta = tmp_rk3_eta
ie2.output(tmp_in, tmp_eta)
_in, _eta = ie2()
ie1.output(_in, _eta)
inputs, eta = ie1()
labels = fluid.layers.data(
name='label', shape=[1], dtype='int64'
)
@ranqiu92
大概的意思就是通过一个整数输入selector来选择对应的输入层作为模型的输入层 @ranqiu92