PyTorch torch.cartesian_prod Function
PyTorch torch Reference Manual
torch.cartesian_prodIt is a function in PyTorch used to compute the Cartesian product. It returns the Cartesian product of the input tensors, i.e., the set of all possible combinations.
Function Definition
torch.cartesian_prod(*tensors)
Usage Example
Example
import torch
# Calculate 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)
# Calculate 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)
Other Extensions