评价此页

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