IfElse op 使用疑问
Created by: dingsiyu
def tt(tensor1): l = [2.0, 4.0, 10.0] r = [7.0, 9.0, 12.0] re_ = layers.zeros(shape=[3,3], dtype='int32') for i in range(3): l_idx = l[i] r_idx = r[i] c = layers.logical_and((tensor1 >= l_idx), (tensor1 < r_idx)) d = layers.reduce_sum(layers.cast(c, 'int32')) mask = layers.where(c) d_ = layers.is_empty(mask) cond = d == 0 cond = layers.cast(cond, dtype='int32') cond = layers.reshape(cond, [1, 1]) cond = layers.cast(cond, dtype='bool') ie = fluid.layers.IfElse(cond) int1 = 2 int2 = 4 with ie.true_block(): ou = layers.ones(shape=[3,3], dtype='int32') * int1 ie.output(ou) with ie.false_block(): ou = layers.ones(shape=[3,3], dtype='int32') * int2 layers.Print(ou) ie.output(ou) re = ie()[0] layers.Print(re) layers.Print(re[:3, :]) re_ += re[:3, :] return d, re_
tensor = layers.data(name='tensor1', shape=[2,3], dtype="float32", append_batch_size=False) a_ = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype='float32')
mask_int, mask = tt(tensor)
place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program())
feed_list = {tensor.name: a_} r1 = exe.run(feed=feed_list, fetch_list=[mask_int, mask]) print(r1[0]) #print("\n") print(r1[1])
部分打印信息: Tensor[tmp_9] shape: [3,3,] dtype: i data: 4,4,4,4,4,4,4,4,4, 1565922625 The place is:CPUPlace Tensor[merge_lod_tensor_0.tmp_0] shape: [6,3,] dtype: i data: 4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1565922625 The place is:CPUPlace Tensor[merge_lod_tensor_0.tmp_0_slice_0] shape: [3,3,] dtype: i data: 4,4,4,0,0,0,0,0,0,
为什么ie.ouput(ou)与block中实际生成的ou不同呢