快捷方式

torch.index_select

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

返回一个新的张量,该张量使用作为 LongTensorindex 中的条目沿 dim 维度索引 input 张量。

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

注意

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

参数
  • input (Tensor) – 输入张量。

  • dim (int) – 进行索引操作的维度

  • index (IntTensorLongTensor) – 包含要索引的下标的一维张量

关键字参数

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

示例

>>> 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]])

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得解答

查看资源