Shortcuts

torch.index_select

torch.index_select(input, dim, index, *, out=None) 张量

返回一个新的张量,该张量沿维度 diminput 张量进行索引,索引的条目来自 index,这是一个 LongTensor

返回的张量具有与原始张量相同的维度数量 (input)。 第dim维度的大小与index的长度相同;其他维度的大小与原始张量中的大小相同。

注意

返回的张量使用与原始张量相同的存储空间。如果out的形状与预期不同,我们会静默地将其更改为正确的形状,并在必要时重新分配底层存储。

Parameters
  • 输入 (张量) – 输入张量。

  • dim (int) – 我们索引的维度

  • 索引 (IntTensorLongTensor) – 包含要索引的索引的1-D张量

Keyword Arguments

输出 (张量, 可选) – 输出张量。

示例:

>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-0.4664,  0.2647, -0.1228, -1.1068],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2])
>>> torch.index_select(x, 0, indices)
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> torch.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
        [-0.4664, -0.1228],
        [-1.1734,  0.7230]])
优云智算