快捷方式

torch.narrow

torch.narrow(input, dim, start, length) Tensor

返回 input 張量的一個窄化(narrowed)版本的新張量。將維度 dimstart 窄化到 start + length。返回的張量和 input 張量共享同一底層儲存。

引數
  • input (Tensor) – 要窄化的張量

  • dim (int) – 沿哪個維度進行窄化

  • start (intTensor) – 開始窄化維度的元素的索引。可以是負數,表示從 dim 的末尾開始索引。如果為 Tensor,它必須是一個 0 維整型 Tensor(不允許布林值)

  • length (int) – 窄化維度的長度,必須是弱正數(即大於或等於零)

示例

>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> torch.narrow(x, 0, 0, 2)
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])
>>> torch.narrow(x, 1, 1, 2)
tensor([[ 2,  3],
        [ 5,  6],
        [ 8,  9]])
>>> torch.narrow(x, -1, torch.tensor(-1), 1)
tensor([[3],
        [6],
        [9]])

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取適合初學者和高階開發者的深度教程

檢視教程

資源

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

檢視資源