評價此頁

torch.narrow#

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

返回一個經過裁剪的 input 張量的新張量。維度 dimstartstart + 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]])