PyTorch torch.gather 関数


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

torch.gatherPyTorch において、指定された次元に沿って指定されたインデックスの要素を収集するための関数です。

関数定義

torch.gather(input, dim, index, sparse_grad)

使用例

実例

import torch

x = torch.tensor([[1, 2], [3, 4], [5, 6]])

# dim=1 に沿って収集
index = torch.tensor([[0], [1], [0]])
result = torch.gather(x, dim=1, index=index)

print(result)

出力結果は次のとおりです:

tensor([[1],
        [4],
        [5]])

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

その他の拡張