From c96f06f2f6941d01d58909274484dafb1e5b72cb Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Wed, 18 Dec 2019 11:47:14 +0800 Subject: [PATCH] add unary operator __neg__, test=develop (#21787) adds unary operator __neg__ for VarBase in dygraph mode, and for Variable in static graph mode. --- python/paddle/fluid/dygraph/math_op_patch.py | 14 +++++++------- python/paddle/fluid/layers/math_op_patch.py | 6 +++++- .../fluid/tests/unittests/test_math_op_patch.py | 13 +++++++++++++ .../tests/unittests/test_math_op_patch_var_base.py | 7 +++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/fluid/dygraph/math_op_patch.py index 57b59185255..daea5b647a1 100644 --- a/python/paddle/fluid/dygraph/math_op_patch.py +++ b/python/paddle/fluid/dygraph/math_op_patch.py @@ -35,11 +35,7 @@ def monkey_patch_math_varbase(): """ def safe_get_dtype(var): - try: - dtype = var.dtype - except: - raise ValueError("Cannot get data type from %s", var.name) - return dtype + return var.dtype @no_grad def create_tensor(value, dtype, shape): @@ -117,6 +113,9 @@ def monkey_patch_math_varbase(): outs = core.ops.scale(inputs, attrs) return outs['Out'][0] + def _neg_(var): + return _scalar_elementwise_op_(var, -1.0, 0.0) + def _scalar_elementwise_add_(var, value): return _scalar_elementwise_op_(var, 1.0, value) @@ -217,6 +216,7 @@ def monkey_patch_math_varbase(): setattr(core.VarBase, method_name, _elemwise_method_creator_(method_name, op_type, reverse, - scalar_method)) - + scalar_method)), + # b = -a + core.VarBase.__neg__ = _neg_ core.VarBase.astype = astype diff --git a/python/paddle/fluid/layers/math_op_patch.py b/python/paddle/fluid/layers/math_op_patch.py index bfb4c8543ba..5a9f7705369 100644 --- a/python/paddle/fluid/layers/math_op_patch.py +++ b/python/paddle/fluid/layers/math_op_patch.py @@ -157,6 +157,9 @@ def monkey_patch_variable(): "bias": bias}) return out + def _neg_(var): + return _scalar_elementwise_op_(var, -1.0, 0.0) + def _scalar_elementwise_add_(var, value): return _scalar_elementwise_op_(var, 1.0, value) @@ -273,5 +276,6 @@ def monkey_patch_variable(): setattr(Variable, method_name, _elemwise_method_creator_(method_name, op_type, reverse, scalar_method)) - + # b = -a + Variable.__neg__ = _neg_ Variable.astype = astype diff --git a/python/paddle/fluid/tests/unittests/test_math_op_patch.py b/python/paddle/fluid/tests/unittests/test_math_op_patch.py index c90640b65af..62944d0c879 100644 --- a/python/paddle/fluid/tests/unittests/test_math_op_patch.py +++ b/python/paddle/fluid/tests/unittests/test_math_op_patch.py @@ -200,6 +200,19 @@ class TestMathOpPatches(unittest.TestCase): b_np_actual = (a_np / 7).astype('int64') self.assertTrue(numpy.array_equal(b_np, b_np_actual)) + @prog_scope() + def test_neg(self): + a = fluid.layers.data(name="a", shape=[10, 1]) + b = -a + place = fluid.CPUPlace() + exe = fluid.Executor(place) + a_np = numpy.random.uniform(-1, 1, size=[10, 1]).astype('float32') + + b_np = exe.run(fluid.default_main_program(), + feed={"a": a_np}, + fetch_list=[b]) + self.assertTrue(numpy.allclose(-a_np, b_np)) + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py b/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py index 1fffccb8fa3..2f454b174bf 100644 --- a/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py @@ -201,6 +201,13 @@ class TestMathOpPatchesVarBase(unittest.TestCase): res = (a >= b) self.assertTrue(np.array_equal(res.numpy(), a_np >= b_np)) + def test_neg(self): + a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype) + with fluid.dygraph.guard(): + a = fluid.dygraph.to_variable(a_np) + res = -a + self.assertTrue(np.array_equal(res.numpy(), -a_np)) + if __name__ == '__main__': unittest.main() -- GitLab