PyTorch torch.cartesian_prod function
PyTorch torch reference manual
torch.cartesian_prod is a function in PyTorch used to compute the Cartesian product. It returns the Cartesian product of the input tensors, that is, the set of all possible combinations.
Function definition
torch.cartesian_prod(*tensors)
Usage examples
Example
import
torch
Compute the Cartesian product of two tensors
a
=
torch.
tensor
(
[
1
,
2
,
3
]
)
b
=
torch.
tensor
(
[
4
,
5
]
)
result
=
torch.
cartesian_prod
(
a
,
b
)
(
"a:"
,
a
)
(
"b:"
,
b
)
(
"Cartesian product:"
)
(
result
)
tensor([[1, 4],
[1, 5],
[2, 4],
[2, 5],
[3, 4],
[3, 5]])
Cartesian product of multiple tensors
x
=
torch.
tensor
(
[
1
,
2
]
)
y
=
torch.
tensor
(
[
10
,
20
,
30
]
)
z
=
torch.
tensor
(
[
100
,
200
]
)
result
=
torch.
cartesian_prod
(
x
,
y
,
z
)
(
"Shape of the Cartesian product of three tensors:"
,
result.
shape
)
(
result
)