将张量的shape改为一维张量
a = torch.arange(12).reshape(3,4)
a
# tensor([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
b = torch.reshape(a,shape=[-1])
b
# tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
b.shape
# torch.Size([12])
x = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
x
# array([[[ 1, 2, 3],
# [ 4, 5, 6]],
# [[ 7, 8, 9],
# [10, 11, 12]]])
x = torch.from_numpy(x)
x
# tensor([[[ 1, 2, 3],
# [ 4, 5, 6]],
# [[ 7, 8, 9],
# [10, 11, 12]]], dtype=torch.int32)
res = torch.reshape(x,shape=[-1])
res
# tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=torch.int32)