快捷方式

torch.take_along_dim

torch.take_along_dim(input, indices, dim=None, *, out=None) Tensor

沿给定维度 dim,从 indices 中的一维索引处选择 input 中的值。

如果 dim 为 None,则输入数组将被视为已展平为一维。

返回沿维度索引的函数,例如 torch.argmax()torch.argsort(),旨在与此函数一起使用。请参阅以下示例。

注意

此函数类似于 NumPy 的 take_along_axis。另请参阅 torch.gather()

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

  • indices (tensor) – 进入 input 的索引。必须具有 long dtype。

  • dim (int, optional) – 要沿其选择的维度。

关键字参数

out (Tensor, optional) – 输出张量。

示例

>>> t = torch.tensor([[10, 30, 20], [60, 40, 50]])
>>> max_idx = torch.argmax(t)
>>> torch.take_along_dim(t, max_idx)
tensor([60])
>>> sorted_idx = torch.argsort(t, dim=1)
>>> torch.take_along_dim(t, sorted_idx, dim=1)
tensor([[10, 20, 30],
        [40, 50, 60]])

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

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

查看教程

资源

查找开发资源并获得问题解答

查看资源