Created by: Aurelius84
In this PR, we support to modify dict and list in while_loop.
You can write code as followed:
def cond(i, ten, test_dict, test_list, test_list_dict):
return layers.less_than(i, ten)
def body(i, ten, test_dict, test_list, test_list_dict):
# Modify the tensor by python dict syntax.
test_dict["test_key"] = i
test_dict["test_key"] += 1
test_list[0] = fluid.layers.reshape(test_list[0], [2, -1]) + 1
test_list_dict[0]["test_key"] += 1
test_list_dict[0]["test_key"] = fluid.layers.relu(test_list_dict[0][
"test_key"])
i = layers.increment(i)
return [i, ten, test_dict, test_list, test_list_dict]
main_program = Program()
startup_program = Program()
with program_guard(main_program, startup_program):
i = layers.zeros(shape=[1], dtype='int64')
ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
test_data = layers.fill_constant(shape=[1], dtype='int64', value=0)
# Define list/dict that holds tensor
test_dict = {"test_key": test_data}
test_list = [layers.fill_constant(shape=[1, 2], dtype='int64', value=0)]
test_list_dict = [{"test_key": layers.fill_constant(shape=[1], dtype='float32', value=0)}]
i, ten, test_dict, test_list, test_list_dict = layers.while_loop(cond, body, [i, ten, test_dict, test_list, test_list_dict])