PyTorch torch.cartesian_prod-Funktion
PyTorch torch-Referenzhandbuch
torch.cartesian_prodEs ist eine Funktion in PyTorch, die zur Berechnung des kartesischen Produkts verwendet wird. Sie gibt das kartesische Produkt der Eingabetensoren zurück, d. h., die Menge aller möglichen Kombinationen.
Funktionsdefinition
torch.cartesian_prod(*tensors)
Anwendungsbeispiel
Beispiel
import torch
# Berechne das kartesische Produkt zweier Tensoren
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5])
result = torch.cartesian_prod(a, b)
print("a:", a)
print("b:", b)
print("Kartesisches Produkt:")
print(result)
# tensor([[1, 4],
# [1, 5],
# [2, 4],
# [2, 5],
# [3, 4],
# [3, 5]])
# Kartesisches Produkt mehrerer Tensoren
x = torch.tensor([1, 2])
y = torch.tensor([10, 20, 30])
z = torch.tensor([100, 200])
result = torch.cartesian_prod(x, y, z)
print("Form des kartesischen Produkts dreier Tensoren:", result.shape)
print(result)
# Berechne das kartesische Produkt zweier Tensoren
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5])
result = torch.cartesian_prod(a, b)
print("a:", a)
print("b:", b)
print("Kartesisches Produkt:")
print(result)
# tensor([[1, 4],
# [1, 5],
# [2, 4],
# [2, 5],
# [3, 4],
# [3, 5]])
# Kartesisches Produkt mehrerer Tensoren
x = torch.tensor([1, 2])
y = torch.tensor([10, 20, 30])
z = torch.tensor([100, 200])
result = torch.cartesian_prod(x, y, z)
print("Form des kartesischen Produkts dreier Tensoren:", result.shape)
print(result)
Andere Erweiterungen