Shortcuts

torch.unravel_index

torch.unravel_index(indices, shape)[源代码]

将一个扁平索引的张量转换为坐标张量的元组,这些坐标张量可以索引到具有指定形状的任意张量中。

Parameters
  • indices (Tensor) – 一个包含索引的整数张量,指向形状为 shape 的任意张量的展平版本。 所有元素必须在范围 [0, prod(shape) - 1] 内。

  • 形状 (整数, 整数序列, 或 torch.Size) – 任意张量的形状。所有元素必须为非负数。

Returns

输出中的每个i-th张量对应于shape的第i维度。每个张量的形状与indices相同,并为每个由indices给出的扁平索引包含一个指向第i维度的索引。

Return type

张量的元组

示例:

>>> import torch
>>> torch.unravel_index(torch.tensor(4), (3, 2))
(tensor(2),
 tensor(0))

>>> torch.unravel_index(torch.tensor([4, 1]), (3, 2))
(tensor([2, 0]),
 tensor([0, 1]))

>>> torch.unravel_index(torch.tensor([0, 1, 2, 3, 4, 5]), (3, 2))
(tensor([0, 0, 1, 1, 2, 2]),
 tensor([0, 1, 0, 1, 0, 1]))

>>> torch.unravel_index(torch.tensor([1234, 5678]), (10, 10, 10, 10))
(tensor([1, 5]),
 tensor([2, 6]),
 tensor([3, 7]),
 tensor([4, 8]))

>>> torch.unravel_index(torch.tensor([[1234], [5678]]), (10, 10, 10, 10))
(tensor([[1], [5]]),
 tensor([[2], [6]]),
 tensor([[3], [7]]),
 tensor([[4], [8]]))

>>> torch.unravel_index(torch.tensor([[1234], [5678]]), (100, 100))
(tensor([[12], [56]]),
 tensor([[34], [78]]))