cartesian_prod

paddle. cartesian_prod ( x: Sequence[Tensor], name: str | None = None ) → Tensor [source]

This API has two signatures:

  1. paddle.cartesian_prod(x, name=None) (Paddle-style):

    Perform Cartesian product on a sequence of tensors.

  2. paddle.cartesian_prod(*tensors) (PyTorch-style):

    Perform Cartesian product on variadic tensor arguments.

This behavior is similar to the itertools.product in Python. Equivalent to converting all input tensors into lists, performing itertools.product on these lists, and finally converting the resulting list into tensors.

Parameters
  • x (list[Tensor]|tuple[Tensor]) – Sequence of 1-D input Tensors. Supported data types: bfloat16, float16, float32, float64, int32, int64, complex64 or complex128.

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

Returns

out (Tensor), cartesian product of input tensors with the same data type.

Examples

>>> import paddle
>>> a = paddle.to_tensor([1, 2, 3], dtype='int32')
>>> b = paddle.to_tensor([5, 6], dtype='int32')
>>> res = paddle.cartesian_prod([a, b])
>>> print(res)
Tensor(shape=[6, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[1, 5],
 [1, 6],
 [2, 5],
 [2, 6],
 [3, 5],
 [3, 6]])

>>> c = paddle.to_tensor([7, 8, 9], dtype='float32')
>>> res = paddle.cartesian_prod([c])
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[7., 8., 9.])

>>> d = paddle.empty([0], dtype='float64')
>>> e = paddle.to_tensor([1, 2], dtype='float64')
>>> f = paddle.to_tensor([3, 4, 5, 6, 7], dtype='float64')
>>> res = paddle.cartesian_prod([d, e, f])
>>> print(res)
Tensor(shape=[0, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
[])