logic.py 37.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
# TODO: define logic functions of a tensor

17
import paddle
18

19
from ..common_ops_import import Variable
20 21
from ..fluid.data_feeder import check_type, check_variable_and_dtype
from .layer_function_generator import templatedoc
22

W
wanghuancoder 已提交
23
Tensor = paddle.fluid.framework.core.eager.Tensor
24

25
from paddle import _C_ops
26
from paddle.tensor.creation import full
27

28 29
from ..framework import LayerHelper, in_dygraph_mode

30 31
__all__ = []

32

33
def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
34
    if in_dygraph_mode():
35 36 37 38 39
        op = getattr(_C_ops, op_name)
        if binary_op:
            return op(x, y)
        else:
            return op(x)
40
    else:
41
        check_variable_and_dtype(
42 43
            x,
            "x",
44 45 46 47 48 49 50 51 52
            [
                "bool",
                "int8",
                "int16",
                "int32",
                "int64",
                "float16",
                "float32",
                "float64",
53
                "uint16",
54
            ],
55 56
            op_name,
        )
57 58 59 60 61 62 63 64 65 66
        if y is not None:
            check_variable_and_dtype(
                y,
                "y",
                [
                    "bool",
                    "int8",
                    "int16",
                    "int32",
                    "int64",
67
                    "float16",
68 69
                    "float32",
                    "float64",
70
                    "uint16",
71 72 73 74 75
                ],
                op_name,
            )
        if out is not None:
            check_type(out, "out", Variable, op_name)
76

77
        helper = LayerHelper(op_name, **locals())
78

79 80 81 82 83
        if binary_op and x.dtype != y.dtype:
            raise ValueError(
                "(InvalidArgument) The DataType of %s Op's Variable must be consistent, but received %s and %s."
                % (op_name, x.dtype, y.dtype)
            )
84

85 86
        if out is None:
            out = helper.create_variable_for_type_inference(dtype=x.dtype)
87

88 89 90 91 92 93 94 95
        if binary_op:
            helper.append_op(
                type=op_name, inputs={"X": x, "Y": y}, outputs={"Out": out}
            )
        else:
            helper.append_op(
                type=op_name, inputs={"X": x}, outputs={"Out": out}
            )
96

97
        return out
98 99 100 101 102


def logical_and(x, y, out=None, name=None):
    r"""

103
    Compute element-wise logical AND on ``x`` and ``y``, and return ``out``. ``out`` is N-dim boolean ``Tensor``.
104 105 106 107 108 109
    Each element of ``out`` is calculated by

    .. math::

        out = x \&\& y

110
    Note:
I
Infinity_lee 已提交
111 112 113
        ``paddle.logical_and`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

        .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
114 115

    Args:
116 117
        x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
        y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
118
        out(Tensor, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([True])
            y = paddle.to_tensor([True, False, True, False])
            res = paddle.logical_and(x, y)
            print(res) # [True False True False]
    """
    if in_dygraph_mode():
135
        return _C_ops.logical_and(x, y)
136

137 138 139
    return _logical_op(
        op_name="logical_and", x=x, y=y, name=name, out=out, binary_op=True
    )
140 141 142 143 144 145 146 147 148 149 150 151


def logical_or(x, y, out=None, name=None):
    """

    ``logical_or`` operator computes element-wise logical OR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``.
    Each element of ``out`` is calculated by

    .. math::

        out = x || y

152
    Note:
I
Infinity_lee 已提交
153 154 155
        ``paddle.logical_or`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

        .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
156

157
    Args:
158 159
        x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
        y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
160 161 162 163 164 165 166 167 168 169 170
        out(Tensor): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.

    Examples:
        .. code-block:: python

            import paddle

171 172
            x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1])
            y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2])
173
            res = paddle.logical_or(x, y)
174 175 176 177
            print(res)
            # Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
            #        [[True , True ],
            #         [True , False]])
178 179
    """
    if in_dygraph_mode():
180
        return _C_ops.logical_or(x, y)
181 182 183
    return _logical_op(
        op_name="logical_or", x=x, y=y, name=name, out=out, binary_op=True
    )
184 185 186 187 188 189 190 191 192 193 194 195


def logical_xor(x, y, out=None, name=None):
    r"""

    ``logical_xor`` operator computes element-wise logical XOR on ``x`` and ``y``, and returns ``out``. ``out`` is N-dim boolean ``Tensor``.
    Each element of ``out`` is calculated by

    .. math::

        out = (x || y) \&\& !(x \&\& y)

196
    Note:
I
Infinity_lee 已提交
197 198 199
        ``paddle.logical_xor`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

        .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
200 201

    Args:
202 203
        x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
        y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
204 205 206 207 208 209 210 211 212 213 214
        out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.

    Examples:
        .. code-block:: python

            import paddle

215 216
            x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1])
            y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2])
217
            res = paddle.logical_xor(x, y)
218 219 220 221
            print(res)
            # Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
            #        [[False, True ],
            #         [True , False]])
222 223
    """
    if in_dygraph_mode():
224
        return _C_ops.logical_xor(x, y)
225

226 227 228
    return _logical_op(
        op_name="logical_xor", x=x, y=y, name=name, out=out, binary_op=True
    )
229 230 231 232 233 234 235 236 237 238 239 240


def logical_not(x, out=None, name=None):
    """

    ``logical_not`` operator computes element-wise logical NOT on ``x``, and returns ``out``. ``out`` is N-dim boolean ``Variable``.
    Each element of ``out`` is calculated by

    .. math::

        out = !x

I
Infinity_lee 已提交
241 242 243 244 245
    Note:
        ``paddle.logical_not`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

        .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

246
    Args:
247
        x(Tensor):  Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64.
248 249 250 251
        out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output.
        name(str|None): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`.

    Returns:
252
        N-D Tensor. A location into which the result is stored. It's dimension equals with ``x``.
253 254 255 256 257 258 259 260 261 262 263

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([True, False, True, False])
            res = paddle.logical_not(x)
            print(res) # [False  True False  True]
    """
    if in_dygraph_mode():
264
        return _C_ops.logical_not(x)
265 266 267
    return _logical_op(
        op_name="logical_not", x=x, y=None, name=name, out=out, binary_op=False
    )
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301


def is_empty(x, name=None):
    """

    Test whether a Tensor is empty.

    Args:
        x (Tensor): The Tensor to be tested.
        name (str, optional): The default value is ``None`` . Normally users
                            don't have to set this parameter. For more information,
                            please refer to :ref:`api_guide_Name` .

    Returns:
        Tensor: A bool scalar Tensor. True if 'x' is an empty Tensor.

    Examples:
        .. code-block:: python

            import paddle

            input = paddle.rand(shape=[4, 32, 32], dtype='float32')
            res = paddle.is_empty(x=input)
            print("res:", res)
            # ('res:', Tensor: eager_tmp_1
            #    - place: CPUPlace
            #    - shape: [1]
            #    - layout: NCHW
            #    - dtype: bool
            #    - data: [0])

    """
    if in_dygraph_mode():
        return _C_ops.is_empty(x)
302 303 304 305 306
    else:
        check_variable_and_dtype(
            x, 'x', ['float32', 'float64', 'int32', 'int64'], 'is_empty'
        )
        check_type(name, "name", (str, type(None)), "is_empty")
307

308 309 310 311 312 313 314
        helper = LayerHelper("is_empty", **locals())
        cond = helper.create_variable_for_type_inference(dtype='bool')
        cond.stop_gradient = True
        helper.append_op(
            type='is_empty', inputs={'X': [x]}, outputs={'Out': [cond]}
        )
        return cond
315 316


W
wawltor 已提交
317
def equal_all(x, y, name=None):
318
    """
319
    Returns the truth value of :math:`x == y`. True if two inputs have the same elements, False otherwise.
320

321
    Note:
322
        The output has no gradient.
323 324

    Args:
325 326
        x(Tensor): Tensor, data type is bool, float32, float64, int32, int64.
        y(Tensor): Tensor, data type is bool, float32, float64, int32, int64.
W
wawltor 已提交
327 328
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.
329 330

    Returns:
W
wawltor 已提交
331
        Tensor: output Tensor, data type is bool, value is [False] or [True].
332 333 334 335 336

    Examples:
        .. code-block:: python

          import paddle
W
wawltor 已提交
337

338 339 340
          x = paddle.to_tensor([1, 2, 3])
          y = paddle.to_tensor([1, 2, 3])
          z = paddle.to_tensor([1, 4, 3])
W
wawltor 已提交
341
          result1 = paddle.equal_all(x, y)
N
Noel 已提交
342
          print(result1) # result1 = [True ]
W
wawltor 已提交
343
          result2 = paddle.equal_all(x, z)
N
Noel 已提交
344
          print(result2) # result2 = [False ]
345
    """
H
hong 已提交
346
    if in_dygraph_mode():
347
        return _C_ops.equal_all(x, y)
348 349 350 351 352 353 354 355 356
    else:
        helper = LayerHelper("equal_all", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        helper.append_op(
            type='equal_all',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
Z
Zhen Wang 已提交
357 358 359


@templatedoc()
360
def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
361 362 363 364 365 366
    r"""
    Check if all :math:`x` and :math:`y` satisfy the condition:

    .. math::
        \left| x - y \right| \leq atol + rtol \times \left| y \right|

H
hg-1099255210 已提交
367
    elementwise, for all elements of :math:`x` and :math:`y`. This is analogous to :math:`numpy.allclose`, namely that it returns :math:`True` if
368
    two tensors are elementwise equal within a tolerance.
Z
Zhen Wang 已提交
369 370

    Args:
371 372
        x(Tensor): The input tensor, it's data type should be float16, float32, float64..
        y(Tensor): The input tensor, it's data type should be float16, float32, float64..
H
huangxu96 已提交
373 374
        rtol(rtoltype, optional): The relative tolerance. Default: :math:`1e-5` .
        atol(atoltype, optional): The absolute tolerance. Default: :math:`1e-8` .
375 376 377
        equal_nan(equalnantype, optional): ${equal_nan_comment}.
        name (str, optional): Name for the operation. For more information, please
            refer to :ref:`api_guide_Name`. Default: None.
Z
Zhen Wang 已提交
378 379

    Returns:
380
        Tensor: The output tensor, it's data type is bool.
381

Z
Zhen Wang 已提交
382 383 384 385 386
    Examples:
        .. code-block:: python

          import paddle

387 388
          x = paddle.to_tensor([10000., 1e-07])
          y = paddle.to_tensor([10000.1, 1e-08])
389
          result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
Z
Zhen Wang 已提交
390
                                  equal_nan=False, name="ignore_nan")
391
          # [False]
392

393
          result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
Z
Zhen Wang 已提交
394
                                      equal_nan=True, name="equal_nan")
395 396
          # [False]

397 398
          x = paddle.to_tensor([1.0, float('nan')])
          y = paddle.to_tensor([1.0, float('nan')])
399 400 401
          result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
                                  equal_nan=False, name="ignore_nan")
          # [False]
402

403 404 405
          result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08,
                                      equal_nan=True, name="equal_nan")
          # [True]
Z
Zhen Wang 已提交
406 407
    """

408
    if in_dygraph_mode():
409
        return _C_ops.allclose(x, y, rtol, atol, equal_nan)
410
    else:
411 412 413 414 415 416
        check_variable_and_dtype(
            x, "input", ['float16', 'float32', 'float64'], 'allclose'
        )
        check_variable_and_dtype(
            y, "input", ['float16', 'float32', 'float64'], 'allclose'
        )
417 418 419 420 421 422 423 424 425 426 427 428
        check_type(rtol, 'rtol', float, 'allclose')
        check_type(atol, 'atol', float, 'allclose')
        check_type(equal_nan, 'equal_nan', bool, 'allclose')

        helper = LayerHelper("allclose", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')

        inputs = {'Input': x, 'Other': y}
        outputs = {'Out': out}
        attrs = {'rtol': str(rtol), 'atol': str(atol), 'equal_nan': equal_nan}
        helper.append_op(
            type='allclose', inputs=inputs, outputs=outputs, attrs=attrs
429
        )
Z
Zhen Wang 已提交
430

431
        return out
432 433


W
wawltor 已提交
434 435
@templatedoc()
def equal(x, y, name=None):
436
    """
S
swtkiwi 已提交
437

438
    This layer returns the truth value of :math:`x == y` elementwise.
N
Noel 已提交
439

440
    Note:
441
        The output has no gradient.
442 443

    Args:
陈沧夜 已提交
444 445
        x(Tensor): Tensor, data type is bool, float16, float32, float64, int32, int64.
        y(Tensor): Tensor, data type is bool, float16, float32, float64, int32, int64.
446 447 448 449
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
W
wawltor 已提交
450
        Tensor: output Tensor, it's shape is the same as the input's Tensor,
451
        and the data type is bool. The result of this op is stop_gradient.
452 453 454 455

    Examples:
        .. code-block:: python

W
wawltor 已提交
456 457
          import paddle

458 459
          x = paddle.to_tensor([1, 2, 3])
          y = paddle.to_tensor([1, 3, 2])
W
wawltor 已提交
460
          result1 = paddle.equal(x, y)
N
Noel 已提交
461
          print(result1)  # result1 = [True False False]
462
    """
463 464
    if not isinstance(y, (int, bool, float, Variable)):
        raise TypeError(
465 466 467 468
            "Type of input args must be float, bool, int or Tensor, but received type {}".format(
                type(y)
            )
        )
469
    if not isinstance(y, Variable):
470
        y = full(shape=[], dtype=x.dtype, fill_value=y)
471

J
Jiabin Yang 已提交
472
    if in_dygraph_mode():
473
        return _C_ops.equal(x, y)
J
Jiabin Yang 已提交
474
    else:
475 476 477
        check_variable_and_dtype(
            x,
            "x",
478 479 480 481 482 483 484 485 486
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
487 488 489 490 491
            "equal",
        )
        check_variable_and_dtype(
            y,
            "y",
492 493 494 495 496 497 498 499 500
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
501 502 503 504 505
            "equal",
        )
        helper = LayerHelper("equal", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        out.stop_gradient = True
J
Jiabin Yang 已提交
506

507 508 509 510 511 512
        helper.append_op(
            type='equal',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
513

W
wawltor 已提交
514 515 516 517

@templatedoc()
def greater_equal(x, y, name=None):
    """
518
    Returns the truth value of :math:`x >= y` elementwise, which is equivalent function to the overloaded operator `>=`.
N
Noel 已提交
519

520
    Note:
521
        The output has no gradient.
W
wawltor 已提交
522 523

    Args:
524 525
        x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
        y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
W
wawltor 已提交
526 527 528
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.
    Returns:
529
        Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
W
wawltor 已提交
530 531 532

    Examples:
        .. code-block:: python
N
Noel 已提交
533

W
wawltor 已提交
534 535
            import paddle

536 537
            x = paddle.to_tensor([1, 2, 3])
            y = paddle.to_tensor([1, 3, 2])
W
wawltor 已提交
538
            result1 = paddle.greater_equal(x, y)
N
Noel 已提交
539
            print(result1)  # result1 = [True False True]
W
wawltor 已提交
540
    """
J
Jiabin Yang 已提交
541
    if in_dygraph_mode():
542
        return _C_ops.greater_equal(x, y)
J
Jiabin Yang 已提交
543
    else:
544 545 546
        check_variable_and_dtype(
            x,
            "x",
547 548 549 550 551 552 553 554 555
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
556 557 558 559 560
            "greater_equal",
        )
        check_variable_and_dtype(
            y,
            "y",
561 562 563 564 565 566 567 568 569
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
570 571 572 573 574
            "greater_equal",
        )
        helper = LayerHelper("greater_equal", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        out.stop_gradient = True
J
Jiabin Yang 已提交
575

576 577 578 579 580 581
        helper.append_op(
            type='greater_equal',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
W
wawltor 已提交
582 583 584 585 586


@templatedoc()
def greater_than(x, y, name=None):
    """
587
    Returns the truth value of :math:`x > y` elementwise, which is equivalent function to the overloaded operator `>`.
N
Noel 已提交
588

589
    Note:
590
        The output has no gradient.
W
wawltor 已提交
591 592

    Args:
J
Jx-qi 已提交
593 594
        x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
        y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
W
wawltor 已提交
595 596 597
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.
    Returns:
598
        Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
W
wawltor 已提交
599 600 601

    Examples:
        .. code-block:: python
N
Noel 已提交
602

W
wawltor 已提交
603 604
            import paddle

605 606
            x = paddle.to_tensor([1, 2, 3])
            y = paddle.to_tensor([1, 3, 2])
W
wawltor 已提交
607
            result1 = paddle.greater_than(x, y)
N
Noel 已提交
608
            print(result1)  # result1 = [False False True]
W
wawltor 已提交
609
    """
J
Jiabin Yang 已提交
610
    if in_dygraph_mode():
611
        return _C_ops.greater_than(x, y)
J
Jiabin Yang 已提交
612
    else:
613 614 615
        check_variable_and_dtype(
            x,
            "x",
616 617 618 619 620 621 622 623 624
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
625 626 627 628 629
            "greater_than",
        )
        check_variable_and_dtype(
            y,
            "y",
630 631 632 633 634 635 636 637 638
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
639 640 641 642 643
            "greater_than",
        )
        helper = LayerHelper("greater_than", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        out.stop_gradient = True
J
Jiabin Yang 已提交
644

645 646 647 648 649 650
        helper.append_op(
            type='greater_than',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
W
wawltor 已提交
651 652 653 654 655


@templatedoc()
def less_equal(x, y, name=None):
    """
656
    Returns the truth value of :math:`x <= y` elementwise, which is equivalent function to the overloaded operator `<=`.
N
Noel 已提交
657

658
    Note:
659
        The output has no gradient.
W
wawltor 已提交
660 661

    Args:
B
BellaZYL 已提交
662 663
        x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
        y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
W
wawltor 已提交
664 665 666 667
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
668
        Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
W
wawltor 已提交
669 670 671

    Examples:
        .. code-block:: python
N
Noel 已提交
672

W
wawltor 已提交
673 674
            import paddle

675 676
            x = paddle.to_tensor([1, 2, 3])
            y = paddle.to_tensor([1, 3, 2])
W
wawltor 已提交
677
            result1 = paddle.less_equal(x, y)
N
Noel 已提交
678
            print(result1)  # result1 = [True True False]
W
wawltor 已提交
679
    """
J
Jiabin Yang 已提交
680
    if in_dygraph_mode():
681
        return _C_ops.less_equal(x, y)
J
Jiabin Yang 已提交
682
    else:
683 684 685
        check_variable_and_dtype(
            x,
            "x",
686 687 688 689 690 691 692 693 694
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
695 696 697 698 699
            "less_equal",
        )
        check_variable_and_dtype(
            y,
            "y",
700 701 702 703 704 705 706 707 708
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
709 710 711 712 713
            "less_equal",
        )
        helper = LayerHelper("less_equal", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        out.stop_gradient = True
J
Jiabin Yang 已提交
714

715 716 717 718 719 720
        helper.append_op(
            type='less_equal',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
W
wawltor 已提交
721 722 723 724 725


@templatedoc()
def less_than(x, y, name=None):
    """
726
    Returns the truth value of :math:`x < y` elementwise, which is equivalent function to the overloaded operator `<`.
N
Noel 已提交
727

728
    Note:
729
        The output has no gradient.
W
wawltor 已提交
730 731

    Args:
H
hh-qiao 已提交
732 733
        x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
        y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64.
W
wawltor 已提交
734 735 736 737
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
738
        Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
W
wawltor 已提交
739 740 741

    Examples:
        .. code-block:: python
N
Noel 已提交
742

W
wawltor 已提交
743 744
            import paddle

745 746
            x = paddle.to_tensor([1, 2, 3])
            y = paddle.to_tensor([1, 3, 2])
W
wawltor 已提交
747
            result1 = paddle.less_than(x, y)
N
Noel 已提交
748
            print(result1)  # result1 = [False True False]
W
wawltor 已提交
749
    """
J
Jiabin Yang 已提交
750
    if in_dygraph_mode():
751
        return _C_ops.less_than(x, y)
J
Jiabin Yang 已提交
752
    else:
753 754 755
        check_variable_and_dtype(
            x,
            "x",
756 757 758 759 760 761 762 763 764
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
765 766 767 768 769
            "less_than",
        )
        check_variable_and_dtype(
            y,
            "y",
770 771 772 773 774 775 776 777 778
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
779 780 781 782 783
            "less_than",
        )
        helper = LayerHelper("less_than", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        out.stop_gradient = True
J
Jiabin Yang 已提交
784

785 786 787 788 789 790
        helper.append_op(
            type='less_than',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
W
wawltor 已提交
791 792 793 794 795


@templatedoc()
def not_equal(x, y, name=None):
    """
796
    Returns the truth value of :math:`x != y` elementwise, which is equivalent function to the overloaded operator `!=`.
797 798

    Note:
799
        The output has no gradient.
W
wawltor 已提交
800 801

    Args:
802 803
        x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float32, float64, int32, int64.
        y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float32, float64, int32, int64.
W
wawltor 已提交
804 805 806 807
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
808
        Tensor: The output shape is same as input :attr:`x`. The output data type is bool.
W
wawltor 已提交
809 810 811

    Examples:
        .. code-block:: python
812

W
wawltor 已提交
813 814
            import paddle

815 816
            x = paddle.to_tensor([1, 2, 3])
            y = paddle.to_tensor([1, 3, 2])
W
wawltor 已提交
817
            result1 = paddle.not_equal(x, y)
N
Noel 已提交
818
            print(result1)  # result1 = [False True True]
W
wawltor 已提交
819
    """
J
Jiabin Yang 已提交
820
    if in_dygraph_mode():
821
        return _C_ops.not_equal(x, y)
J
Jiabin Yang 已提交
822
    else:
823 824 825
        check_variable_and_dtype(
            x,
            "x",
826 827 828 829 830 831 832 833 834
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
835 836 837 838 839
            "not_equal",
        )
        check_variable_and_dtype(
            y,
            "y",
840 841 842 843 844 845 846 847 848
            [
                "bool",
                "float16",
                "float32",
                "float64",
                "int32",
                "int64",
                "uint16",
            ],
849 850 851 852 853
            "not_equal",
        )
        helper = LayerHelper("not_equal", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')
        out.stop_gradient = True
J
Jiabin Yang 已提交
854

855 856 857 858 859 860
        helper.append_op(
            type='not_equal',
            inputs={'X': [x], 'Y': [y]},
            outputs={'Out': [out]},
        )
        return out
Z
zhulei 已提交
861 862 863 864 865


def is_tensor(x):
    """

C
Chen Long 已提交
866
    Tests whether input object is a paddle.Tensor.
Z
zhulei 已提交
867 868 869 870 871

    Args:
        x (object): Object to test.

    Returns:
C
Chen Long 已提交
872
        A boolean value. True if ``x`` is a paddle.Tensor, otherwise False.
Z
zhulei 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885

    Examples:
        .. code-block:: python

            import paddle

            input1 = paddle.rand(shape=[2, 3, 5], dtype='float32')
            check = paddle.is_tensor(input1)
            print(check)  #True

            input3 = [1, 4]
            check = paddle.is_tensor(input3)
            print(check)  #False
886

Z
zhulei 已提交
887
    """
888 889 890 891
    if in_dygraph_mode():
        return isinstance(x, (Tensor, paddle.fluid.core.eager.Tensor))
    else:
        return isinstance(x, Variable)
892 893 894


def _bitwise_op(op_name, x, y, out=None, name=None, binary_op=True):
895
    if in_dygraph_mode():
W
wanghuancoder 已提交
896
        op = getattr(_C_ops, op_name)
897 898 899 900
        if binary_op:
            return op(x, y)
        else:
            return op(x)
901
    else:
902
        check_variable_and_dtype(
903 904
            x,
            "x",
905 906 907
            ["bool", "uint8", "int8", "int16", "int32", "int64"],
            op_name,
        )
908 909 910 911 912 913 914 915 916
        if y is not None:
            check_variable_and_dtype(
                y,
                "y",
                ["bool", "uint8", "int8", "int16", "int32", "int64"],
                op_name,
            )
        if out is not None:
            check_type(out, "out", Variable, op_name)
917

918 919 920
        helper = LayerHelper(op_name, **locals())
        if binary_op:
            assert x.dtype == y.dtype
921

922 923
        if out is None:
            out = helper.create_variable_for_type_inference(dtype=x.dtype)
924

925 926 927 928 929 930 931 932
        if binary_op:
            helper.append_op(
                type=op_name, inputs={"X": x, "Y": y}, outputs={"Out": out}
            )
        else:
            helper.append_op(
                type=op_name, inputs={"X": x}, outputs={"Out": out}
            )
933

934
        return out
935 936 937


def bitwise_and(x, y, out=None, name=None):
938 939 940 941 942 943 944 945 946 947 948
    r"""

    Apply ``bitwise_and`` on Tensor ``X`` and ``Y`` .

    .. math::
        Out = X \& Y

    .. note::
        ``paddle.bitwise_and`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .

    .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor.
949

950
    Args:
951 952 953
        x (Tensor): Input Tensor of ``bitwise_and`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        y (Tensor): Input Tensor of ``bitwise_and`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        out(Tensor): Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor.
954 955

    Returns:
956
        Tensor: Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor.
957

958 959 960 961 962 963 964 965 966
    Examples:
        .. code-block:: python

            import paddle
            x = paddle.to_tensor([-5, -1, 1])
            y = paddle.to_tensor([4,  2, -3])
            res = paddle.bitwise_and(x, y)
            print(res)  # [0, 2, 1]
    """
0
0x45f 已提交
967
    if in_dygraph_mode() and out is None:
968
        return _C_ops.bitwise_and(x, y)
969 970 971
    return _bitwise_op(
        op_name="bitwise_and", x=x, y=y, name=name, out=out, binary_op=True
    )
972 973 974


def bitwise_or(x, y, out=None, name=None):
975 976 977 978 979 980 981 982 983 984 985
    r"""

    Apply ``bitwise_or`` on Tensor ``X`` and ``Y`` .

    .. math::
        Out = X | Y

    .. note::
        ``paddle.bitwise_or`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .

    .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor.
986

987
    Args:
988 989 990
        x (Tensor): Input Tensor of ``bitwise_or`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        y (Tensor): Input Tensor of ``bitwise_or`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        out(Tensor): Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor.
991 992

    Returns:
993
        Tensor: Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor.
994 995 996 997 998 999 1000 1001 1002 1003

    Examples:
        .. code-block:: python

            import paddle
            x = paddle.to_tensor([-5, -1, 1])
            y = paddle.to_tensor([4,  2, -3])
            res = paddle.bitwise_or(x, y)
            print(res)  # [-1, -1, -3]
    """
0
0x45f 已提交
1004
    if in_dygraph_mode() and out is None:
1005
        return _C_ops.bitwise_or(x, y)
H
hong 已提交
1006

1007 1008 1009
    return _bitwise_op(
        op_name="bitwise_or", x=x, y=y, name=name, out=out, binary_op=True
    )
1010 1011 1012


def bitwise_xor(x, y, out=None, name=None):
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    r"""

    Apply ``bitwise_xor`` on Tensor ``X`` and ``Y`` .

    .. math::
        Out = X ^\wedge Y

    .. note::
        ``paddle.bitwise_xor`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .

    .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor.
1024 1025

    Args:
1026 1027 1028
        x (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        y (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        out(Tensor): Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor.
1029 1030

    Returns:
1031
        Tensor: Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor.
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

    Examples:
        .. code-block:: python

            import paddle
            x = paddle.to_tensor([-5, -1, 1])
            y = paddle.to_tensor([4,  2, -3])
            res = paddle.bitwise_xor(x, y)
            print(res) # [-1, -3, -4]
    """
0
0x45f 已提交
1042
    if in_dygraph_mode() and out is None:
1043
        return _C_ops.bitwise_xor(x, y)
1044 1045 1046
    return _bitwise_op(
        op_name="bitwise_xor", x=x, y=y, name=name, out=out, binary_op=True
    )
1047 1048 1049


def bitwise_not(x, out=None, name=None):
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    r"""

    Apply ``bitwise_not`` on Tensor ``X``.

    .. math::
        Out = \sim X

    .. note::
        ``paddle.bitwise_not`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ .

        .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor.
1061 1062

    Args:
1063 1064
        x (Tensor): Input Tensor of ``bitwise_not`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64.
        out(Tensor): Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor.
1065

1066
    Returns:
1067
        Tensor: Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor.
1068 1069 1070 1071 1072 1073 1074 1075 1076

    Examples:
        .. code-block:: python

            import paddle
            x = paddle.to_tensor([-5, -1, 1])
            res = paddle.bitwise_not(x)
            print(res) # [4, 0, -2]
    """
0
0x45f 已提交
1077
    if in_dygraph_mode() and out is None:
1078
        return _C_ops.bitwise_not(x)
1079

1080 1081 1082
    return _bitwise_op(
        op_name="bitwise_not", x=x, y=None, name=name, out=out, binary_op=False
    )
A
andyjpaddle 已提交
1083 1084 1085 1086


@templatedoc()
def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
1087
    r"""
1088
    Check if all :math:`x` and :math:`y` satisfy the condition:
1089 1090 1091 1092 1093 1094 1095 1096

    .. math::

        \left| x - y \right| \leq atol + rtol \times \left| y \right|

    elementwise, for all elements of :math:`x` and :math:`y`. The behaviour of this
    operator is analogous to :math:`numpy.isclose`, namely that it returns :math:`True` if
    two tensors are elementwise equal within a tolerance.
A
andyjpaddle 已提交
1097 1098

    Args:
1099 1100
        x(Tensor): The input tensor, it's data type should be float16, float32, float64.
        y(Tensor): The input tensor, it's data type should be float16, float32, float64.
A
andyjpaddle 已提交
1101 1102
        rtol(rtoltype, optional): The relative tolerance. Default: :math:`1e-5` .
        atol(atoltype, optional): The absolute tolerance. Default: :math:`1e-8` .
1103
        equal_nan(equalnantype, optional): If :math:`True` , then two :math:`NaNs` will be compared as equal. Default: :math:`False` .
A
andyjpaddle 已提交
1104 1105 1106 1107
        name (str, optional): Name for the operation. For more information, please
            refer to :ref:`api_guide_Name`. Default: None.

    Returns:
1108
        Tensor: The output tensor, it's data type is bool.
A
andyjpaddle 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

    Examples:
        .. code-block:: python

          import paddle

          x = paddle.to_tensor([10000., 1e-07])
          y = paddle.to_tensor([10000.1, 1e-08])
          result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                                  equal_nan=False, name="ignore_nan")
          # [True, False]
          result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                                      equal_nan=True, name="equal_nan")
          # [True, False]

          x = paddle.to_tensor([1.0, float('nan')])
          y = paddle.to_tensor([1.0, float('nan')])
          result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                                  equal_nan=False, name="ignore_nan")
          # [True, False]
          result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08,
                                      equal_nan=True, name="equal_nan")
          # [True, True]
    """

1134
    if in_dygraph_mode():
1135
        return _C_ops.isclose(x, y, rtol, atol, equal_nan)
1136
    else:
1137 1138 1139 1140 1141 1142
        check_variable_and_dtype(
            x, "input", ['float16', 'float32', 'float64'], 'isclose'
        )
        check_variable_and_dtype(
            y, "input", ['float16', 'float32', 'float64'], 'isclose'
        )
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        check_type(rtol, 'rtol', float, 'isclose')
        check_type(atol, 'atol', float, 'isclose')
        check_type(equal_nan, 'equal_nan', bool, 'isclose')

        helper = LayerHelper("isclose", **locals())
        out = helper.create_variable_for_type_inference(dtype='bool')

        inputs = {'Input': x, 'Other': y}
        outputs = {'Out': out}
        attrs = {'rtol': str(rtol), 'atol': str(atol), 'equal_nan': equal_nan}
        helper.append_op(
            type='isclose', inputs=inputs, outputs=outputs, attrs=attrs
1155
        )
1156
        return out