Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.cartesian_prod function

PyTorch torch.cartesian_prod function


PyTorch torch reference manual 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

)

print

(

"a:"

,

a

)

print

(

"b:"

,

b

)

print

(

"Cartesian product:"

)

print

(

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

)

print

(

"Shape of the Cartesian product of three tensors:"

,

result.

shape

)

print

(

result

)


PyTorch torch reference manual PyTorch torch reference manual