pytorch中tensor进行reshape操作后原始数据的顺序

在pytorch中,经常需要对tensor进行reshape操作,使其符合特定网络的输入格式。在将网络的输

出重新reshape回输入前的形状时,tensor的特征是否还是按输入的顺序进行排列?

带着疑问做了下面的实验

x1 = torch.randn(2, 3)

x2 = torch.randn(2, 3)

x3 = torch.randn(2, 3)


x4 = torch.stack((x1, x2, x3), 0)


shape = x4.shape

print("x4:", x4.shape)

print("x4:\n", x4)

x4 = x4.reshape(x4.shape[0]*x4.shape[1], x4.shape[-1])

print("reshaped x4:", x4.shape)

print("reshaped x4:\n", x4)

x4 = x4.reshape(shape[0], shape[1], shape[-1])

print("recovered x4:\n", x4, x4.shape)
# print("x5:\n", x5)

输出

x4: torch.Size([3, 2, 3])
x4:
 tensor([[[-1.2061,  0.0617,  1.1632],
         [-1.5008, -1.5944, -0.0187]],

        [[-2.1325, -0.5270, -0.1021],
         [ 0.0099, -0.4454, -1.4976]],

        [[-0.9475, -0.6130, -0.1291],
         [-0.4107,  1.3931, -0.0984

你可能感兴趣的:(pytorch,python,pytorch)