第37个方法
torch.narrow(input, dim, start, length) → Tensor
- 此方法返回一个新的张量,它是输入张量的缩小版本。指定的dim维度是从头开始输入+长度。**返回的张量和输入张量共享相同的基础存储。**其实就是返回张量是输入张量的视图。
接下来介绍参数:
input
:需要缩小的张量。dim
:指定要缩小的维度。start
指定维度开始的位置。length
:维度上需要的长度。
接下来看例子:
>>> 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]])