torch.reshape
举例:
将一维tensor reshape成二维:
aa = torch.tensor([1,2,3,4,4,5,5,6,7,7,8,9,8,7,6,5,4,3,3,2,1])
bb = aa.reshape(7,3)
bb
Out[12]:
tensor([[1, 2, 3],
[4, 4, 5],
[5, 6, 7],
[7, 8, 9],
[8, 7, 6],
[5, 4, 3],
[3, 2, 1]])
将二维reshape 成三维:
(-1代表不知道是多少,根据确定的维度计算的)
b = torch.tensor([[1,2],[3,4],[5,6],[3,4],[5,6],[1,2],[3,4],[1,2]])
b.shape
Out[6]: torch.Size([8, 2])
b.reshape(4,2,-1)
Out[7]:
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[3, 4]],
[[5, 6],
[1, 2]],
[[3, 4],
[1, 2]]])
reshape是从第一维开始填充更改tensor形状的。
reshape是从第一维开始排列的是因为数据存储的时候是有顺序的。