PyTorch torch.combinations 関数


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

torch.combinationsこれは、PyTorch で入力テンソルの要素からなるすべての r 要素の組み合わせを計算するための関数です。入力テンソル内の長さ r のすべての可能な組み合わせを返します。

関数定義

torch.combinations(input, r=2, with_replacement=False)

使用例

実例

import torch

# すべての 2 要素の組み合わせを計算
x = torch.tensor([1, 2, 3, 4])
result = torch.combinations(x, r=2)
print(入力:, x)
print(2 要素の組み合わせ:)
print(result)
# tensor([[1, 2],
#         [1, 3],
#         [1, 4],
#         [2, 3],
#         [2, 4],
#         [3, 4]])

# 3 要素の組み合わせ
result3 = torch.combinations(x, r=3)
print(3 要素の組み合わせ:)
print(result3)

# 重複ありの組み合わせ (with_replacement=True)
result_with_replacement = torch.combinations(x, r=2, with_replacement=True)
print(重複ありの 2 要素の組み合わせ:)
print(result_with_replacement)

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

その他の拡張