Rate this Page

torch.functional.cartesian_prod#

torch.functional.cartesian_prod(*tensors)[source]#

Do cartesian product of the given sequence of tensors. The behavior is similar topython’sitertools.product.

Parameters

*tensors (Tensor) – any number of 1 dimensional tensors.

Returns

A tensor equivalent to converting all the input tensors into lists,doitertools.product on these lists, and finally convert the resulting listinto tensor.

Return type

Tensor

Example:

>>>importitertools>>>a=[1,2,3]>>>b=[4,5]>>>list(itertools.product(a,b))[(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]>>>tensor_a=torch.tensor(a)>>>tensor_b=torch.tensor(b)>>>torch.cartesian_prod(tensor_a,tensor_b)tensor([[1, 4],        [1, 5],        [2, 4],        [2, 5],        [3, 4],        [3, 5]])