|
|
@ -78,13 +78,21 @@ class TestApiWhileLoop(unittest.TestCase):
|
|
|
|
self.assertTrue(np.allclose(np.asarray(res[1]), data))
|
|
|
|
self.assertTrue(np.allclose(np.asarray(res[1]), data))
|
|
|
|
|
|
|
|
|
|
|
|
def test_var_dict(self):
|
|
|
|
def test_var_dict(self):
|
|
|
|
def cond(i, ten, test_dict):
|
|
|
|
def cond(i, ten, test_dict, test_list, test_list_dict):
|
|
|
|
return layers.less_than(i, ten)
|
|
|
|
return layers.less_than(i, ten)
|
|
|
|
|
|
|
|
|
|
|
|
def body(i, ten, test_dict):
|
|
|
|
def body(i, ten, test_dict, test_list, test_list_dict):
|
|
|
|
layers.assign(i, test_dict["test_key"])
|
|
|
|
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)
|
|
|
|
i = layers.increment(i)
|
|
|
|
return [i, ten, test_dict]
|
|
|
|
return [i, ten, test_dict, test_list, test_list_dict]
|
|
|
|
|
|
|
|
|
|
|
|
main_program = Program()
|
|
|
|
main_program = Program()
|
|
|
|
startup_program = Program()
|
|
|
|
startup_program = Program()
|
|
|
@ -92,18 +100,42 @@ class TestApiWhileLoop(unittest.TestCase):
|
|
|
|
i = layers.zeros(shape=[1], dtype='int64')
|
|
|
|
i = layers.zeros(shape=[1], dtype='int64')
|
|
|
|
ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
|
|
|
|
ten = layers.fill_constant(shape=[1], dtype='int64', value=10)
|
|
|
|
test_data = layers.fill_constant(shape=[1], dtype='int64', value=0)
|
|
|
|
test_data = layers.fill_constant(shape=[1], dtype='int64', value=0)
|
|
|
|
|
|
|
|
|
|
|
|
test_dict = {"test_key": test_data}
|
|
|
|
test_dict = {"test_key": test_data}
|
|
|
|
i, ten, test_dict = layers.while_loop(cond, body,
|
|
|
|
test_list = [
|
|
|
|
[i, ten, test_dict])
|
|
|
|
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])
|
|
|
|
place = fluid.CUDAPlace(0) if core.is_compiled_with_cuda(
|
|
|
|
place = fluid.CUDAPlace(0) if core.is_compiled_with_cuda(
|
|
|
|
) else fluid.CPUPlace()
|
|
|
|
) else fluid.CPUPlace()
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
res = exe.run(main_program, fetch_list=[test_dict["test_key"]])
|
|
|
|
res = exe.run(main_program,
|
|
|
|
|
|
|
|
fetch_list=[
|
|
|
|
|
|
|
|
test_dict["test_key"], test_list[0],
|
|
|
|
|
|
|
|
test_list_dict[0]["test_key"]
|
|
|
|
|
|
|
|
])
|
|
|
|
self.assertTrue(
|
|
|
|
self.assertTrue(
|
|
|
|
np.allclose(
|
|
|
|
np.allclose(
|
|
|
|
np.asarray(res[0]),
|
|
|
|
np.asarray(res[0]),
|
|
|
|
np.full(
|
|
|
|
np.full(
|
|
|
|
shape=(1), fill_value=9, dtype=np.int64)))
|
|
|
|
shape=(1), fill_value=10, dtype=np.int64)))
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
|
|
|
np.allclose(
|
|
|
|
|
|
|
|
np.asarray(res[1]),
|
|
|
|
|
|
|
|
np.full(
|
|
|
|
|
|
|
|
shape=(2, 1), fill_value=10, dtype=np.int64)))
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
|
|
|
np.allclose(
|
|
|
|
|
|
|
|
np.asarray(res[2]),
|
|
|
|
|
|
|
|
np.full(
|
|
|
|
|
|
|
|
shape=(1), fill_value=10, dtype=np.float32)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestApiWhileLoop_Nested(unittest.TestCase):
|
|
|
|
class TestApiWhileLoop_Nested(unittest.TestCase):
|
|
|
|