快捷方式

torch.unravel_index

torch.unravel_index(indices, shape)[source][source]

將扁平索引的張量轉換為座標張量元組,用於索引指定形狀的任意張量。

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

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

返回

輸出中的第 i 個張量對應於 shape 的第 i 個維度。每個張量的形狀與 indices 相同,幷包含 indices 給定的每個扁平索引在第 i 個維度上的索引。

返回型別

Tensor 元組

示例

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

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源