def test_recognize_digits_conv(self): program = Program() with program_guard(program, startup_program=Program()): images = layers.data( name='pixel', shape=[1, 28, 28], dtype='float32') label = layers.data(name='label', shape=[1], dtype='int32') conv_pool_1 = nets.simple_img_conv_pool( input=images, filter_size=5, num_filters=2, pool_size=2, pool_stride=2, act="relu") conv_pool_2 = nets.simple_img_conv_pool( input=conv_pool_1, filter_size=5, num_filters=4, pool_size=2, pool_stride=2, act="relu") predict = layers.fc(input=conv_pool_2, size=10, act="softmax") cost = layers.cross_entropy(input=predict, label=label) avg_cost = layers.mean(x=cost) program.append_backward(avg_cost) print(str(program))
def test_recognize_digits_conv(self): program = Program() with program_guard(program, startup_program=Program()): images = layers.data(name='pixel', shape=[1, 28, 28], dtype='float32') label = layers.data(name='label', shape=[1], dtype='int32') conv_pool_1 = nets.simple_img_conv_pool(input=images, filter_size=5, num_filters=2, pool_size=2, pool_stride=2, act="relu") conv_pool_2 = nets.simple_img_conv_pool(input=conv_pool_1, filter_size=5, num_filters=4, pool_size=2, pool_stride=2, act="relu") predict = layers.fc(input=conv_pool_2, size=10, act="softmax") cost = layers.cross_entropy(input=predict, label=label) avg_cost = layers.mean(x=cost) program.append_backward(avg_cost) print(str(program))
def test_fit_a_line(self): program = Program() with program_guard(program, startup_program=Program()): x = layers.data(name='x', shape=[13], dtype='float32') y_predict = layers.fc(input=x, size=1, act=None) y = layers.data(name='y', shape=[1], dtype='float32') cost = layers.square_error_cost(input=y_predict, label=y) avg_cost = layers.mean(x=cost) self.assertIsNotNone(avg_cost) program.append_backward(avg_cost) print(str(program))
def test_append_backward(self): prog = Program() block = prog.global_block() mul_x = block.create_var( dtype="float32", shape=[5, 10], lod_level=0, name="mul.x") mul_y = block.create_var( dtype="float32", shape=[10, 8], lod_level=0, name="mul.y") mul_out = block.create_var( dtype="float32", shape=[5, 8], lod_level=0, name="mul.out") mul_op = block.append_op( type="mul", inputs={"X": [mul_x], "Y": mul_y}, outputs={"Out": [mul_out]}, attrs={"x_num_col_dims": 1}) add_y = block.create_var( dtype="float32", shape=[5, 8], lod_level=0, name="add.y") add_out = block.create_var( dtype="float32", shape=[5, 8], lod_level=0, name="add.out") add_op = block.append_op( type="elementwise_add", inputs={"X": mul_out, "Y": add_y}, outputs={"Out": add_out}, attrs={"x_num_col_dims": 1}) mean_out = block.create_var( dtype="float32", shape=[1], lod_level=0, name="mean.out") block.append_op( type="mean", inputs={"X": add_out}, outputs={"Out": mean_out}) self.assertEqual(mul_op.idx, 0) self.assertEqual(add_op.idx, 1) param_to_grad = prog.append_backward(mean_out, set()) def grad_name(name): return name + "@GRAD" for var_name in ("mul.x", "mul.y", "mul.out", "add.y", "add.out", "mean.out"): self.assertEqual(param_to_grad[var_name][0], grad_name(var_name)) self.assertEqual(param_to_grad[var_name][1], 0) expect_ops = [ "mul", "elementwise_add", "mean", "fill_constant", "mean_grad", "elementwise_add_grad", "mul_grad" ] actual_ops = [] for op in block.ops: actual_ops.append(op.type) self.assertEqual(actual_ops, expect_ops)
def test_append_backward(self): prog = Program() block = prog.global_block() mul_x = block.create_var(dtype="float32", shape=[5, 10], lod_level=0, name="mul.x") mul_y = block.create_var(dtype="float32", shape=[10, 8], lod_level=0, name="mul.y") mul_out = block.create_var(dtype="float32", shape=[5, 8], lod_level=0, name="mul.out") mul_op = block.append_op(type="mul", inputs={ "X": [mul_x], "Y": mul_y }, outputs={"Out": [mul_out]}, attrs={"x_num_col_dims": 1}) add_y = block.create_var(dtype="float32", shape=[5, 8], lod_level=0, name="add.y") add_out = block.create_var(dtype="float32", shape=[5, 8], lod_level=0, name="add.out") add_op = block.append_op(type="elementwise_add", inputs={ "X": mul_out, "Y": add_y }, outputs={"Out": add_out}, attrs={"x_num_col_dims": 1}) mean_out = block.create_var(dtype="float32", shape=[1], lod_level=0, name="mean.out") block.append_op(type="mean", inputs={"X": add_out}, outputs={"Out": mean_out}) self.assertEqual(mul_op.idx, 0) self.assertEqual(add_op.idx, 1) param_to_grad = prog.append_backward(mean_out, set()) def grad_name(name): return name + "@GRAD" for var_name in ("mul.x", "mul.y", "mul.out", "add.y", "add.out", "mean.out"): self.assertEqual(param_to_grad[var_name][0], grad_name(var_name)) self.assertEqual(param_to_grad[var_name][1], 0) expect_ops = [ "mul", "elementwise_add", "mean", "fill_constant", "mean_grad", "elementwise_add_grad", "mul_grad" ] actual_ops = [] for op in block.ops: actual_ops.append(op.type) self.assertEqual(actual_ops, expect_ops)