narrow可以理解为对张量的一种剪裁,且他们共用相同的storge
a=torch.tensor([i for i in range(0,9)]).reshape(3,3)
print(a)
b=torch.narrow(input=a,dim=0,start=0,length=2)
b
#output:
tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
tensor([[0, 1, 2],
[3, 4, 5]])
c=a.narrow(dim=1,start=1,length=1)
c
#output:
tensor([[1],
[4],
[7]])
dim代表沿着哪个维度剪裁。start代表从dim维的第几位维开始剪裁。length代表沿着这一维度剪裁的长度