評價此頁

torch.functional.unravel_index#

torch.functional.unravel_index(indices, shape)[原始碼]#

將平坦索引張量轉換為座標張量元組,這些元組可以索引到任意指定形狀的張量。

引數
  • indices (Tensor) – 一個整數張量,包含任意形狀為 shape 的張量的扁平化版本的索引。所有元素必須在 [0, prod(shape) - 1] 的範圍內。

  • shape (int, intssequence, or torch.Size) – 任意張量的形狀。所有元素必須是非負的。

返回

輸出中的每個 i-th 張量對應於 shape 的第 i 個維度。每個張量與 indices 具有相同的形狀,並且包含一個索引到第 i 個維度,對應於 indices 中提供的每個扁平索引。

返回型別

tuple of Tensors

示例

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