def fc(X, W, Y): ret_v = core.Net.create() ret_v.append_op(Operator("mul", X="X", Y="W", Out="pre_activation")) ret_v.append_op(Operator("sigmoid", X="pre_activation", Y=Y)) ret_v.complete_add_op(True) return ret_v
def create_op(scope, op_type, inputs, outputs, attrs): kwargs = dict() def __create_var__(name, var_name): scope.var(var_name).get_tensor() kwargs[name].append(var_name) for in_name, in_dup in Operator.get_op_inputs(op_type): if in_name in inputs: kwargs[in_name] = [] if in_dup: sub_in = inputs[in_name] for sub_in_name, _ in sub_in: __create_var__(in_name, sub_in_name) else: __create_var__(in_name, in_name) for out_name, out_dup in Operator.get_op_outputs(op_type): if out_name in outputs: kwargs[out_name] = [] if out_dup: sub_out = outputs[out_name] for sub_out_name, _ in sub_out: __create_var__(out_name, sub_out_name) else: __create_var__(out_name, out_name) for attr_name in Operator.get_op_attr_names(op_type): if attr_name in attrs: kwargs[attr_name] = attrs[attr_name] return Operator(op_type, **kwargs)
def check_with_place(self, place): scope = core.Scope() # create and initialize Grad Variable height = 10 rows = [0, 4, 7] row_numel = 12 grad_selected_rows = scope.var('Grad').get_selected_rows() grad_selected_rows.set_height(height) grad_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 grad_tensor = grad_selected_rows.get_tensor() grad_tensor.set(np_array, place) # create and initialize Param Variable param = scope.var('Param').get_tensor() param_array = np.full((height, row_numel), 5.0).astype("float32") param.set(param_array, place) # create and initialize LeraningRate Variable lr = scope.var('LearningRate').get_tensor() lr_array = np.full((1), 2.0).astype("float32") lr.set(lr_array, place) # create and run sgd operator sgd_op = Operator( "sgd", Param='Param', Grad='Grad', ParamOut='Param', LearningRate='LearningRate') ctx = core.DeviceContext.create(place) sgd_op.run(scope, ctx) # get and compare result result_array = np.array(param) # rows[0] = 0, 5.0 - 2.0 * 2.0 self.assertAlmostEqual(1.0, result_array[rows[0], 0]) # rows[0] = 0, 5.0 - 2.0 * 1.0 self.assertAlmostEqual(3.0, result_array[rows[0], 2]) # 5.0 - 2.0 * 0.0 self.assertAlmostEqual(5.0, result_array[1, 0]) # rows[1] = 4, 5.0 - 2.0 * 1.0 self.assertAlmostEqual(3.0, result_array[rows[1], 10]) # 5.0 - 2.0 * 0.0 self.assertAlmostEqual(5.0, result_array[5, 8]) # rows[2] = 7, 5.0 - 2.0 * 1.0 self.assertAlmostEqual(3.0, result_array[rows[2], 1]) # rows[2] = 7, 5.0 - 2.0 * 4.0 self.assertAlmostEqual(-3.0, result_array[rows[2], 8])
def check_with_place(self, place): scope = core.Scope() # create and initialize Grad Variable height = 10 rows = [0, 4, 7] row_numel = 12 grad_selected_rows = scope.var('Grad').get_selected_rows() grad_selected_rows.set_height(height) grad_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 grad_tensor = grad_selected_rows.get_tensor() grad_tensor.set(np_array, place) # create and initialize Param Variable param = scope.var('Param').get_tensor() param_array = np.full((height, row_numel), 5.0).astype("float32") param.set(param_array, place) # create and initialize LeraningRate Variable lr = scope.var('LearningRate').get_tensor() lr_array = np.full((1), 2.0).astype("float32") lr.set(lr_array, place) # create and run sgd operator sgd_op = Operator("sgd", Param='Param', Grad='Grad', ParamOut='Param', LearningRate='LearningRate') ctx = core.DeviceContext.create(place) sgd_op.run(scope, ctx) # get and compare result result_array = np.array(param) # rows[0] = 0, 5.0 - 2.0 * 2.0 self.assertAlmostEqual(1.0, result_array[rows[0], 0]) # rows[0] = 0, 5.0 - 2.0 * 1.0 self.assertAlmostEqual(3.0, result_array[rows[0], 2]) # 5.0 - 2.0 * 0.0 self.assertAlmostEqual(5.0, result_array[1, 0]) # rows[1] = 4, 5.0 - 2.0 * 1.0 self.assertAlmostEqual(3.0, result_array[rows[1], 10]) # 5.0 - 2.0 * 0.0 self.assertAlmostEqual(5.0, result_array[5, 8]) # rows[2] = 7, 5.0 - 2.0 * 1.0 self.assertAlmostEqual(3.0, result_array[rows[2], 1]) # rows[2] = 7, 5.0 - 2.0 * 4.0 self.assertAlmostEqual(-3.0, result_array[rows[2], 8])
def create_sub_net(self): truenet = core.Net.create() scale_op_t = Operator("scale", X='X', Out='Out', scale=2.) truenet.append_op(scale_op_t) truenet.complete_add_op(True) self.condop.set_truenet(truenet) falsenet = core.Net.create() scale_op_t = Operator("scale", X='X', Out='Out', scale=-2.) falsenet.append_op(scale_op_t) falsenet.complete_add_op(True) self.condop.set_falsenet(falsenet)
def test_get_set(self): ids = self.scope.var("ids").get_lod_tensor_array() self.append_lod_tensor( ids, [[0, 3, 6], [0, 1, 2, 3, 4, 5, 6]], np.array( [1, 2, 3, 4, 5, 6], dtype="int64")) self.append_lod_tensor( ids, [[0, 3, 6], [0, 1, 1, 3, 5, 5, 6]], np.array( [0, 1, 2, 3, 4, 5], dtype="int64")) self.append_lod_tensor( ids, [[0, 3, 6], [0, 0, 1, 2, 3, 4, 5]], np.array( [0, 1, 2, 3, 4], dtype="int64")) scores = self.scope.var("scores").get_lod_tensor_array() self.append_lod_tensor( scores, [[0, 3, 6], [0, 1, 2, 3, 4, 5, 6]], np.array( [1, 2, 3, 4, 5, 6], dtype="float64")) self.append_lod_tensor( scores, [[0, 3, 6], [0, 1, 1, 3, 5, 5, 6]], np.array( [0, 1, 2, 3, 4, 5], dtype="float64")) self.append_lod_tensor( scores, [[0, 3, 6], [0, 0, 1, 2, 3, 4, 5]], np.array( [0, 1, 2, 3, 4], dtype="float64")) sentence_ids = self.scope.var("sentence_ids").get_tensor() sentence_scores = self.scope.var("sentence_scores").get_tensor() beam_search_decode_op = Operator( "beam_search_decode", # inputs Ids="ids", Scores="scores", # outputs SentenceIds="sentence_ids", SentenceScores="sentence_scores") ctx = core.DeviceContext.create(self.cpu_place) beam_search_decode_op.run(self.scope, ctx) expected_lod = [[0, 4, 8], [0, 1, 3, 6, 9, 10, 13, 16, 19]] self.assertEqual(sentence_ids.lod(), expected_lod) self.assertEqual(sentence_scores.lod(), expected_lod) expected_data = np.array( [2, 1, 0, 3, 1, 0, 3, 2, 1, 5, 4, 3, 2, 4, 4, 3, 6, 5, 4], "int64") self.assertTrue(np.array_equal(np.array(sentence_ids), expected_data)) self.assertTrue( np.array_equal(np.array(sentence_scores), expected_data))
def test_run(self): op = Operator( 'beam_search', pre_ids="pre_ids", ids='ids', scores='scores', selected_ids='selected_ids', selected_scores='selected_scores', level=0, beam_size=2, end_id=0, ) op.run(self.scope, self.ctx) selected_ids = self.scope.find_var("selected_ids").get_tensor() print 'selected_ids', np.array(selected_ids) print 'lod', selected_ids.lod()
def uniform_random_test(self, place): scope = core.Scope() scope.var('X').get_tensor() op = Operator("uniform_random", Out='X', shape=[1000, 784], min=-5.0, max=10.0, seed=10) ctx = core.DeviceContext.create(place) op.run(scope, ctx) tensor = numpy.array(scope.find_var('X').get_tensor()) self.assertAlmostEqual(tensor.mean(), 2.5, delta=0.1)
def uniform_random_test(self, place): scope = core.Scope() scope.var('X').get_tensor() op = Operator( "uniform_random", Out='X', shape=[1000, 784], min=-5.0, max=10.0, seed=10) ctx = core.DeviceContext.create(place) op.run(scope, ctx) tensor = numpy.array(scope.find_var('X').get_tensor()) self.assertAlmostEqual(tensor.mean(), 2.5, delta=0.1)
def gaussian_random_test(self, place): scope = core.Scope() scope.var('Out').get_tensor() op = Operator("gaussian_random", Out='Out', shape=[1000, 784], mean=.0, std=1., seed=10) context = core.DeviceContext.create(place) op.run(scope, context) tensor = numpy.array(scope.find_var('Out').get_tensor()) self.assertAlmostEqual(numpy.mean(tensor), .0, delta=0.1) self.assertAlmostEqual(numpy.std(tensor), 1., delta=0.1)
def gaussian_random_test(self, place): scope = core.Scope() scope.var('Out').get_tensor() op = Operator( "gaussian_random", Out='Out', shape=[1000, 784], mean=.0, std=1., seed=10) context = core.DeviceContext.create(place) op.run(scope, context) tensor = numpy.array(scope.find_var('Out').get_tensor()) self.assertAlmostEqual(numpy.mean(tensor), .0, delta=0.1) self.assertAlmostEqual(numpy.std(tensor), 1., delta=0.1)
def test_get_set(self): ids = self.scope.var("ids").get_lod_tensor_array() self.append_lod_tensor(ids, [[0, 3, 6], [0, 1, 2, 3, 4, 5, 6]], np.array([1, 2, 3, 4, 5, 6], dtype="int64")) self.append_lod_tensor(ids, [[0, 3, 6], [0, 1, 1, 3, 5, 5, 6]], np.array([0, 1, 2, 3, 4, 5], dtype="int64")) self.append_lod_tensor(ids, [[0, 3, 6], [0, 0, 1, 2, 3, 4, 5]], np.array([0, 1, 2, 3, 4], dtype="int64")) scores = self.scope.var("scores").get_lod_tensor_array() self.append_lod_tensor(scores, [[0, 3, 6], [0, 1, 2, 3, 4, 5, 6]], np.array([1, 2, 3, 4, 5, 6], dtype="float64")) self.append_lod_tensor(scores, [[0, 3, 6], [0, 1, 1, 3, 5, 5, 6]], np.array([0, 1, 2, 3, 4, 5], dtype="float64")) self.append_lod_tensor(scores, [[0, 3, 6], [0, 0, 1, 2, 3, 4, 5]], np.array([0, 1, 2, 3, 4], dtype="float64")) sentence_ids = self.scope.var("sentence_ids").get_tensor() sentence_scores = self.scope.var("sentence_scores").get_tensor() beam_search_decode_op = Operator( "beam_search_decode", # inputs Ids="ids", Scores="scores", # outputs SentenceIds="sentence_ids", SentenceScores="sentence_scores") ctx = core.DeviceContext.create(self.cpu_place) beam_search_decode_op.run(self.scope, ctx) expected_lod = [[0, 4, 8], [0, 1, 3, 6, 9, 10, 13, 16, 19]] self.assertEqual(sentence_ids.lod(), expected_lod) self.assertEqual(sentence_scores.lod(), expected_lod) expected_data = np.array( [2, 1, 0, 3, 1, 0, 3, 2, 1, 5, 4, 3, 2, 4, 4, 3, 6, 5, 4], "int64") self.assertTrue(np.array_equal(np.array(sentence_ids), expected_data)) self.assertTrue( np.array_equal(np.array(sentence_scores), expected_data))
def test_net_all(self): net = core.Net.create() op1 = Operator("sum", X=["X", "Y"], Out="Out") net.append_op(op1) net2 = core.Net.create() net2.append_op(fc(X="X", W="w", Y="fc.out")) net2.complete_add_op(True) net.append_op(net2) net.complete_add_op(True) expected = ''' Op(plain_net), inputs:{all[W, X, Y]}, outputs:{all[Out, fc.out, pre_activation]}. Op(sum), inputs:{X[X, Y]}, outputs:{Out[Out]}. Op(plain_net), inputs:{all[W, X]}, outputs:{all[fc.out, pre_activation]}. Op(plain_net), inputs:{all[W, X]}, outputs:{all[fc.out, pre_activation]}. Op(mul), inputs:{X[X], Y[W]}, outputs:{Out[pre_activation]}. Op(sigmoid), inputs:{X[pre_activation]}, outputs:{Y[fc.out]}. ''' self.assertEqual(expected, "\n" + str(net))
def set_input(scope, op, inputs, place): def __set_input__(var_name, var): if isinstance(var, tuple) or isinstance(var, np.ndarray): tensor = scope.find_var(var_name).get_tensor() if isinstance(var, tuple): tensor.set_lod(var[1]) var = var[0] tensor.set_dims(var.shape) tensor.set(var, place) elif isinstance(var, float): scope.find_var(var_name).set_float(var) elif isinstance(var, int): scope.find_var(var_name).set_int(var) for in_name, in_dup in Operator.get_op_inputs(op.type()): if in_name in inputs: if in_dup: sub_in = inputs[in_name] for sub_in_name, sub_in_val in sub_in: __set_input__(sub_in_name, sub_in_val) else: __set_input__(in_name, inputs[in_name])
def check_with_place(self, place): scope = core.Scope() # create and initialize Grad Variable height = 10 rows = [0, 4, 7, 4] row_numel = 12 grad_selected_rows = scope.var('Grad').get_selected_rows() grad_selected_rows.set_height(height) grad_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 grad_tensor = grad_selected_rows.get_tensor() grad_tensor.set(np_array, place) # create and initialize Param Variable param = scope.var('Param').get_tensor() param_array = np.full((height, row_numel), 5.0).astype("float32") param.set(param_array, place) # create and initialize LeraningRate Variable lr = scope.var('LearningRate').get_tensor() lr_array = np.full((1), 2.0).astype("float32") lr.set(lr_array, place) # create and initialize moment Variable moment = scope.var('Moment').get_tensor() moment_np_array = np.full((height, row_numel), 2.0).astype("float32") moment.set(moment_np_array, place) # create and run sgd operator adagrad_op = Operator("adagrad", Param='Param', Grad='Grad', ParamOut='Param', Moment='Moment', MomentOut='Moment', LearningRate='LearningRate', epsilon=2.0) ctx = core.DeviceContext.create(place) adagrad_op.run(scope, ctx) # get and compare moment result moment_result_array = np.array(moment) self.assertAlmostEqual(6.0, moment_result_array[rows[0], 0]) self.assertAlmostEqual(3.0, moment_result_array[rows[0], 2]) self.assertAlmostEqual(2.0, moment_result_array[1, 0]) # 2.0 + (1.0 + 1.0)^2 self.assertAlmostEqual(6.0, moment_result_array[rows[1], 10]) self.assertAlmostEqual(6.0, moment_result_array[rows[3], 4]) self.assertAlmostEqual(2.0, moment_result_array[5, 8]) self.assertAlmostEqual(3.0, moment_result_array[rows[2], 1]) self.assertAlmostEqual(18.0, moment_result_array[rows[2], 8]) # get and compare param result result_array = np.array(param) def get_out(param, lr, grad, m, epsilon): return param - lr * grad / (math.sqrt(m) + epsilon) self.assertAlmostEqual(get_out(5.0, 2.0, 2.0, 6.0, 2.0), result_array[rows[0], 0], places=5) self.assertAlmostEqual(get_out(5.0, 2.0, 1.0, 3.0, 2.0), result_array[rows[0], 2], places=5) self.assertAlmostEqual(get_out(5.0, 2.0, 0.0, 2.0, 2.0), result_array[1, 0], places=5) # grad_merge = 1.0 + 1.0 # m = 6.0 self.assertAlmostEqual(get_out(5.0, 2.0, 2.0, 6.0, 2.0), result_array[rows[1], 10], places=5) self.assertAlmostEqual(get_out(5.0, 2.0, 0.0, 2.0, 2.0), result_array[5, 8], places=5) self.assertAlmostEqual(get_out(5.0, 2.0, 1.0, 3.0, 2.0), result_array[rows[2], 1], places=5) self.assertAlmostEqual(get_out(5.0, 2.0, 4.0, 18.0, 2.0), result_array[rows[2], 8], places=5)
def one_case(self, input, target): op = Operator(type="is_empty", X=input, Out="out") ctx = core.DeviceContext.create(core.CPUPlace()) op.run(self.scope, ctx) out = self.scope.var("out").get_tensor() self.assertEqual(np.array(out)[0], target)
def check_with_place(self, place): scope = core.Scope() # create and initialize Grad Variable height = 10 rows = [0, 4, 7, 4] row_numel = 12 grad_selected_rows = scope.var('Grad').get_selected_rows() grad_selected_rows.set_height(height) grad_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 grad_tensor = grad_selected_rows.get_tensor() grad_tensor.set(np_array, place) # create and initialize Param Variable param = scope.var('Param').get_tensor() param_array = np.full((height, row_numel), 5.0).astype("float32") param.set(param_array, place) # create and initialize LeraningRate Variable lr = scope.var('LearningRate').get_tensor() lr_array = np.full((1), 2.0).astype("float32") lr.set(lr_array, place) # create and initialize moment Variable moment = scope.var('Moment').get_tensor() moment_np_array = np.full((height, row_numel), 2.0).astype("float32") moment.set(moment_np_array, place) # create and run sgd operator adagrad_op = Operator( "adagrad", Param='Param', Grad='Grad', ParamOut='Param', Moment='Moment', MomentOut='Moment', LearningRate='LearningRate', epsilon=2.0) ctx = core.DeviceContext.create(place) adagrad_op.run(scope, ctx) # get and compare moment result moment_result_array = np.array(moment) self.assertAlmostEqual(6.0, moment_result_array[rows[0], 0]) self.assertAlmostEqual(3.0, moment_result_array[rows[0], 2]) self.assertAlmostEqual(2.0, moment_result_array[1, 0]) # 2.0 + (1.0 + 1.0)^2 self.assertAlmostEqual(6.0, moment_result_array[rows[1], 10]) self.assertAlmostEqual(6.0, moment_result_array[rows[3], 4]) self.assertAlmostEqual(2.0, moment_result_array[5, 8]) self.assertAlmostEqual(3.0, moment_result_array[rows[2], 1]) self.assertAlmostEqual(18.0, moment_result_array[rows[2], 8]) # get and compare param result result_array = np.array(param) def get_out(param, lr, grad, m, epsilon): return param - lr * grad / (math.sqrt(m) + epsilon) self.assertAlmostEqual( get_out(5.0, 2.0, 2.0, 6.0, 2.0), result_array[rows[0], 0], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 1.0, 3.0, 2.0), result_array[rows[0], 2], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 0.0, 2.0, 2.0), result_array[1, 0], places=5) # grad_merge = 1.0 + 1.0 # m = 6.0 self.assertAlmostEqual( get_out(5.0, 2.0, 2.0, 6.0, 2.0), result_array[rows[1], 10], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 0.0, 2.0, 2.0), result_array[5, 8], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 1.0, 3.0, 2.0), result_array[rows[2], 1], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 4.0, 18.0, 2.0), result_array[rows[2], 8], places=5)
def test_with_place(place, tensor_format, shape): # attr epsilon = 0.00001 momentum = 0.9 if len(shape) == 2: x_shape = shape c = shape[1] else: # n, h, w, c = 2, 3, 4, 2 n, h, w, c = shape[0], shape[1], shape[2], shape[3] if data_format == "NHWC": x_shape = [n, h, w, c] elif data_format == "NCHW": x_shape = [n, c, h, w] else: raise ValueError("Unknown data type.") scale_shape = [c] x_val = np.random.random_sample(x_shape).astype(np.float32) scale_val = np.random.random_sample(scale_shape).astype(np.float32) bias_val = np.random.random_sample(scale_shape).astype(np.float32) mean = np.zeros(scale_shape).astype(np.float32) variance = np.ones(scale_shape).astype(np.float32) # run forward y_out, saved_mean, var_ref = _reference_training( x_val, scale_val, bias_val, epsilon, data_format) # update moving mean and variance mean_out = saved_mean * (1. - momentum) + momentum * mean variance_out = var_ref * (1. - momentum) + momentum * variance saved_variance = 1. / np.sqrt(var_ref + epsilon) # for gradient test # y_grad = np.ones(x_shape).astype(np.float32) y_grad = np.zeros(x_shape).astype(np.float32) if len(y_grad.shape) == 2: y_grad[0, 0] = 1. else: y_grad[0, 0, 0, 0] = 1. # y_grad = np.random.random_sample(x_shape).astype(np.float32) x_grad_ref, scale_grad_ref, bias_grad_ref = _reference_grad( x_val, y_grad, scale_val, saved_mean, var_ref, epsilon, data_format) scope = core.Scope() # create input x_tensor = create_or_get_tensor(scope, "x_val", x_val, place) scale_tensor = create_or_get_tensor(scope, "scale_val", scale_val, place) bias_tensor = create_or_get_tensor(scope, "bias_val", bias_val, place) mean_tensor = create_or_get_tensor(scope, "mean", mean, place) variance_tensor = create_or_get_tensor(scope, "variance", variance, place) # create output y_tensor = create_or_get_tensor(scope, "y_out", None, place) saved_mean_tensor = create_or_get_tensor(scope, "saved_mean", None, place) saved_variance_tensor = create_or_get_tensor( scope, "saved_variance", None, place) mean_out_tensor = mean_tensor variance_out_tensor = variance_tensor batch_norm_op = Operator( "batch_norm", # inputs X="x_val", Scale="scale_val", Bias="bias_val", Mean="mean", Variance="variance", # outputs Y="y_out", MeanOut="mean", VarianceOut="variance", SavedMean="saved_mean", SavedVariance="saved_variance", # attrs is_test=False, tensor_format=tensor_format, momentum=momentum, epsilon=epsilon) ctx = core.DeviceContext.create(place) batch_norm_op.run(scope, ctx) # check forward result self.__assert_close(y_tensor, y_out, "y_out") self.__assert_close(saved_mean_tensor, saved_mean, "saved_mean") self.__assert_close(saved_variance_tensor, saved_variance, "saved_variance") self.__assert_close(mean_out_tensor, mean_out, "mean_out") if isinstance(place, core.GPUPlace): atol = 5e-2 else: atol = 1e-4 self.__assert_close(variance_out_tensor, variance_out, "variance_out", atol) print "op test forward passed: ", str(place), tensor_format # run backward batch_norm_op_grad = get_backward_op(scope, batch_norm_op, set()) set_output_grad( scope, ["y_out", "mean", "variance", "saved_mean", "saved_variance"], place, feed_dict={"y_out": y_grad}) batch_norm_op_grad.run(scope, ctx) x_grad_tensor = create_or_get_tensor(scope, grad_var_name("x_val"), None, place) scale_grad_tensor = create_or_get_tensor( scope, grad_var_name("scale_val"), None, place) bias_grad_tensor = create_or_get_tensor(scope, grad_var_name("bias_val"), None, place) # check gradient output self.__assert_close(x_grad_tensor, x_grad_ref, "x_grad") self.__assert_close(scale_grad_tensor, scale_grad_ref, "scale_grad") self.__assert_close(bias_grad_tensor, bias_grad_ref, "bias_grad") print "op test backward passed: ", str(place), tensor_format
def test_with_place(place, tensor_format, shape): # attr epsilon = 0.00001 momentum = 0.9 if len(shape) == 2: x_shape = shape c = shape[1] else: # n, h, w, c = 2, 3, 4, 2 n, h, w, c = shape[0], shape[1], shape[2], shape[3] if data_format == "NHWC": x_shape = [n, h, w, c] elif data_format == "NCHW": x_shape = [n, c, h, w] else: raise ValueError("Unknown data type.") scale_shape = [c] x_val = np.random.random_sample(x_shape).astype(np.float32) scale_val = np.random.random_sample(scale_shape).astype(np.float32) bias_val = np.random.random_sample(scale_shape).astype(np.float32) mean = np.zeros(scale_shape).astype(np.float32) variance = np.ones(scale_shape).astype(np.float32) # run forward y_out, saved_mean, var_ref = _reference_training( x_val, scale_val, bias_val, epsilon, data_format) # update moving mean and variance mean_out = saved_mean * (1. - momentum) + momentum * mean variance_out = var_ref * (1. - momentum) + momentum * variance saved_variance = 1. / np.sqrt(var_ref + epsilon) # for gradient test # y_grad = np.ones(x_shape).astype(np.float32) y_grad = np.zeros(x_shape).astype(np.float32) if len(y_grad.shape) == 2: y_grad[0, 0] = 1. else: y_grad[0, 0, 0, 0] = 1. # y_grad = np.random.random_sample(x_shape).astype(np.float32) x_grad_ref, scale_grad_ref, bias_grad_ref = _reference_grad( x_val, y_grad, scale_val, saved_mean, var_ref, epsilon, data_format) scope = core.Scope() # create input x_tensor = create_or_get_tensor(scope, "x_val", x_val, place) scale_tensor = create_or_get_tensor(scope, "scale_val", scale_val, place) bias_tensor = create_or_get_tensor(scope, "bias_val", bias_val, place) mean_tensor = create_or_get_tensor(scope, "mean", mean, place) variance_tensor = create_or_get_tensor(scope, "variance", variance, place) # create output y_tensor = create_or_get_tensor(scope, "y_out", None, place) saved_mean_tensor = create_or_get_tensor(scope, "saved_mean", None, place) saved_variance_tensor = create_or_get_tensor( scope, "saved_variance", None, place) mean_out_tensor = mean_tensor variance_out_tensor = variance_tensor batch_norm_op = Operator( "batch_norm", # inputs X="x_val", Scale="scale_val", Bias="bias_val", Mean="mean", Variance="variance", # outputs Y="y_out", MeanOut="mean", VarianceOut="variance", SavedMean="saved_mean", SavedVariance="saved_variance", # attrs is_test=False, tensor_format=tensor_format, momentum=momentum, epsilon=epsilon) ctx = core.DeviceContext.create(place) batch_norm_op.run(scope, ctx) # check forward result self.__assert_close(y_tensor, y_out, "y_out") self.__assert_close(saved_mean_tensor, saved_mean, "saved_mean") self.__assert_close(saved_variance_tensor, saved_variance, "saved_variance") self.__assert_close(mean_out_tensor, mean_out, "mean_out") if isinstance(place, core.GPUPlace): atol = 5e-2 else: atol = 1e-4 self.__assert_close(variance_out_tensor, variance_out, "variance_out", atol) print "op test forward passed: ", str(place), tensor_format # run backward batch_norm_op_grad = get_backward_op(scope, batch_norm_op, set()) set_output_grad( scope, ["y_out", "mean", "variance", "saved_mean", "saved_variance"], place, feed_dict={"y_out": y_grad}) batch_norm_op_grad.run(scope, ctx) x_grad_tensor = create_or_get_tensor(scope, grad_var_name("x_val"), None, place) scale_grad_tensor = create_or_get_tensor(scope, grad_var_name("scale_val"), None, place) bias_grad_tensor = create_or_get_tensor(scope, grad_var_name("bias_val"), None, place) # check gradient output self.__assert_close(x_grad_tensor, x_grad_ref, "x_grad") self.__assert_close(scale_grad_tensor, scale_grad_ref, "scale_grad") self.__assert_close(bias_grad_tensor, bias_grad_ref, "bias_grad") print "op test backward passed: ", str(place), tensor_format
def check_output_with_place(self, place, atol): op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) program = Program() block = program.global_block() inputs = append_input_output(block, op_proto, self.inputs, True) outputs = append_input_output(block, op_proto, self.outputs, False) op = block.append_op( type=self.op_type, inputs=inputs, outputs=outputs, attrs=self.attrs if hasattr(self, "attrs") else dict()) # infer variable type and infer shape in compile-time op.desc.infer_var_type(block.desc) op.desc.infer_shape(block.desc) fetch_list = [] for var_name, var in outputs.iteritems(): if var_name in self.outputs: if isinstance(var, list): for v in var: fetch_list.append(v) else: fetch_list.append(var) feed_map = self.feed_var(inputs, place) exe = Executor(place) outs = exe.run(program, feed=feed_map, fetch_list=fetch_list, return_numpy=False) for out_name, out_dup in Operator.get_op_outputs(self.op_type): if out_name not in self.outputs: continue def find_actual(target_name, fetch_list): found = [ i for i, var in enumerate(fetch_list) if var.name == target_name ] self.assertTrue( len(found) == 1, "Found {} {}".format( len(found), target_name)) return found[0] if out_dup: sub_out = self.outputs[out_name] if not isinstance(sub_out, list): raise AssertionError("sub_out type %s is not list", type(sub_out)) for sub_out_name, expect in sub_out: idx = find_actual(sub_out_name, fetch_list) actual = outs[idx] actual_t = np.array(actual) expect_t = expect[0] \ if isinstance(expect, tuple) else expect self.assertTrue( np.allclose( actual_t, expect_t, atol=atol), "Output (" + sub_out_name + ") has diff at " + str(place)) if isinstance(expect, tuple): self.assertListEqual( actual.lod(), expect[1], "Output (" + sub_out_name + ") has different lod at " + str(place)) else: idx = find_actual(out_name, fetch_list) actual = outs[idx] actual_t = np.array(actual) expect = self.outputs[out_name] expect_t = expect[0] if isinstance(expect, tuple) else expect self.assertTrue( np.allclose( actual_t, expect_t, atol=atol), "Output (" + out_name + ") has diff at " + str(place)) if isinstance(expect, tuple): self.assertListEqual(actual.lod(), expect[1], "Output (" + out_name + ") has different lod at " + str(place))