isin

paddle. isin ( x, test_x, assume_unique=False, invert=False, name=None ) [source]

Tests if each element of x is in test_x.

Parameters
  • x (Tensor) – The input Tensor. Supported data type: ‘bfloat16’, ‘float16’, ‘float32’, ‘float64’, ‘int32’, ‘int64’.

  • test_x (Tensor) – Tensor values against which to test for each input element. Supported data type: ‘bfloat16’, ‘float16’, ‘float32’, ‘float64’, ‘int32’, ‘int64’.

  • assume_unique (bool, optional) – If True, indicates both x and test_x contain unique elements, which could make the calculation faster. Default: False.

  • invert (bool, optional) – Indicate whether to invert the boolean return tensor. If True, invert the results. Default: False.

  • name (str, optional) – Name for the operation (optional, default is None).For more information, please refer to Name.

Returns

out (Tensor), The output Tensor with the same shape as x.

Examples

>>> import paddle
>>> paddle.set_device('cpu')
>>> x = paddle.to_tensor([-0., -2.1, 2.5, 1.0, -2.1], dtype='float32')
>>> test_x = paddle.to_tensor([-2.1, 2.5], dtype='float32')
>>> res = paddle.isin(x, test_x)
>>> print(res)
Tensor(shape=[5], dtype=bool, place=Place(cpu), stop_gradient=True,
[False, True, True, False, True])

>>> x = paddle.to_tensor([-0., -2.1, 2.5, 1.0, -2.1], dtype='float32')
>>> test_x = paddle.to_tensor([-2.1, 2.5], dtype='float32')
>>> res = paddle.isin(x, test_x, invert=True)
>>> print(res)
Tensor(shape=[5], dtype=bool, place=Place(cpu), stop_gradient=True,
[True, False, False, True, False])

>>> # Set `assume_unique` to True only when `x` and `test_x` contain unique values, otherwise the result may be incorrect.
>>> x = paddle.to_tensor([0., 1., 2.]*20).reshape([20, 3])
>>> test_x = paddle.to_tensor([0., 1.]*20)
>>> correct_result = paddle.isin(x, test_x, assume_unique=False)
>>> print(correct_result)
Tensor(shape=[20, 3], dtype=bool, place=Place(cpu), stop_gradient=True,
[[True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False],
 [True , True , False]])

>>> incorrect_result = paddle.isin(x, test_x, assume_unique=True)
>>> print(incorrect_result)
Tensor(shape=[20, 3], dtype=bool, place=Place(gpu:0), stop_gradient=True,
[[True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , True ],
 [True , True , False]])