diff --git a/python/paddle/fluid/tests/unittests/test_compare_op.py b/python/paddle/fluid/tests/unittests/test_compare_op.py old mode 100644 new mode 100755 index 7a142675880220f725a083639895ae5d503cb902..89756385486cf6690a0d1e7bd5c93462e579bdfd --- a/python/paddle/fluid/tests/unittests/test_compare_op.py +++ b/python/paddle/fluid/tests/unittests/test_compare_op.py @@ -96,6 +96,21 @@ def create_paddle_case(op_type, callback): fetch_list=[out]) self.assertEqual((res == self.real_result).all(), True) + def test_api_float(self): + if self.op_type == "equal": + paddle.enable_static() + with program_guard(Program(), Program()): + x = fluid.data(name='x', shape=[4], dtype='int64') + y = fluid.data(name='y', shape=[1], dtype='int64') + op = eval("paddle.%s" % (self.op_type)) + out = op(x, y) + exe = fluid.Executor(self.place) + res, = exe.run(feed={"x": self.input_x, + "y": 1.0}, + fetch_list=[out]) + self.real_result = np.array([1, 0, 0, 0]).astype(np.int64) + self.assertEqual((res == self.real_result).all(), True) + def test_dynamic_api(self): paddle.disable_static() x = paddle.to_tensor(self.input_x) @@ -105,6 +120,47 @@ def create_paddle_case(op_type, callback): self.assertEqual((out.numpy() == self.real_result).all(), True) paddle.enable_static() + def test_dynamic_api_int(self): + if self.op_type == "equal": + paddle.disable_static() + x = paddle.to_tensor(self.input_x) + op = eval("paddle.%s" % (self.op_type)) + out = op(x, 1) + self.real_result = np.array([1, 0, 0, 0]).astype(np.int64) + self.assertEqual((out.numpy() == self.real_result).all(), True) + paddle.enable_static() + + def test_dynamic_api_float(self): + if self.op_type == "equal": + paddle.disable_static() + x = paddle.to_tensor(self.input_x) + op = eval("paddle.%s" % (self.op_type)) + out = op(x, 1.0) + self.real_result = np.array([1, 0, 0, 0]).astype(np.int64) + self.assertEqual((out.numpy() == self.real_result).all(), True) + paddle.enable_static() + + def test_assert(self): + def test_dynamic_api_string(self): + if self.op_type == "equal": + paddle.disable_static() + x = paddle.to_tensor(self.input_x) + op = eval("paddle.%s" % (self.op_type)) + out = op(x, "1.0") + paddle.enable_static() + + self.assertRaises(TypeError, test_dynamic_api_string) + + def test_dynamic_api_bool(self): + if self.op_type == "equal": + paddle.disable_static() + x = paddle.to_tensor(self.input_x) + op = eval("paddle.%s" % (self.op_type)) + out = op(x, True) + self.real_result = np.array([1, 0, 0, 0]).astype(np.int64) + self.assertEqual((out.numpy() == self.real_result).all(), True) + paddle.enable_static() + def test_broadcast_api_1(self): paddle.enable_static() with program_guard(Program(), Program()): diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py old mode 100644 new mode 100755 index 65ad308875761e16dbe694031f0aae89b5be9b7f..f944813f8eddf821d5f38f9cede4f60af37b00e3 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -28,6 +28,7 @@ from ..fluid.layers import logical_xor # noqa: F401 from paddle.common_ops_import import core from paddle import _C_ops +from paddle.tensor.creation import full __all__ = [] @@ -174,6 +175,13 @@ def equal(x, y, name=None): result1 = paddle.equal(x, y) print(result1) # result1 = [True False False] """ + if not isinstance(y, (int, bool, float, Variable)): + raise TypeError( + "Type of input args must be float, bool, int or Tensor, but received type {}". + format(type(y))) + if not isinstance(y, Variable): + y = full(shape=[1], dtype=x.dtype, fill_value=y) + if in_dygraph_mode(): return _C_ops.equal(x, y)