Shortcuts

torch.narrow

torch.narrow(input, dim, start, length) 张量

返回一个新的张量,它是 input 张量的缩小版本。维度 dimstartstart + length。返回的张量和 input 张量共享相同的底层存储。

Parameters
  • 输入 (Tensor) – 要缩小的张量

  • dim (int) – 要缩小的维度

  • 开始 (intTensor) – 从窄化维度开始的元素索引。可以是负数,表示从dim的末尾开始索引。如果Tensor,它必须是一个0维的整数Tensor(不允许布尔值)

  • 长度 (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]])
优云智算