捷徑

torch.narrow

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

返回一個新張量,它是 input 張量的變窄版本。維度 dim 是從 start 輸入到 start + length。返回的張量和 input 張量共享相同的底層儲存空間。

參數
  • input ( Tensor) – 要進行窄化 (narrow) 的 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 的完整開發者文件

檢視文件

教學

取得適用於初學者和進階開發人員的深入教學

檢視教學

資源

尋找開發資源並取得問題解答

檢視資源