torch.narrow

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

返回一个新的张量,它是 input 张量的一个缩小版本。这个新的张量在维度 dim 上从 startstart + 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]])
本页目录