PyTorch torch.cartesian_prod 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

torch.cartesian_prodこれは PyTorch でデカルト積を計算するための関数です。入力テンソルのデカルト積、つまり可能なすべての組み合わせの集合を返します。

関数定義

torch.cartesian_prod(*tensors)

使用例

実例

import torch

# 2つのテンソルのデカルト積を計算
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5])
result = torch.cartesian_prod(a, b)
print("a:", a)
print("b:", b)
print("デカルト積:")
print(result)
# tensor([[1, 4],
#         [1, 5],
#         [2, 4],
#         [2, 5],
#         [3, 4],
#         [3, 5]])

# 複数のテンソルのデカルト積
x = torch.tensor([1, 2])
y = torch.tensor([10, 20, 30])
z = torch.tensor([100, 200])
result = torch.cartesian_prod(x, y, z)
print("3つのテンソルのデカルト積の形状:", result.shape)
print(result)

Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

その他の拡張