2

我有一个torch.tensor看起来像这样的:

tensor([[[A,B,C],
         [D,E,F],
         [G,H,I]],

        [[J,K,L],
         [M,N,O],
         [P,Q,R]]]

我想重塑这个张量,使其尺寸为(18, 1). 我希望新张量看起来像这样:

tensor([A,
        J,
        B,
        K,
        C,
        L,
        D,
        M,
       ...
        I,
        R])

我试过tensor.view(-1,1)了,但这不起作用..

4

2 回答 2

4
a = torch.arange(18).view(2,3,3)

print(a)
#tensor([[[ 0,  1,  2],
#         [ 3,  4,  5],
#         [ 6,  7,  8]],
#
#        [[ 9, 10, 11],
#         [12, 13, 14],
#         [15, 16, 17]]])

aa = a.permute(1,2,0).flatten()

print(aa)
#tensor([ 0,  9,  1, 10,  2, 11,  3, 12,  4, 13,  5, 14,  6, 15,  7, 16,  8, 17])
于 2019-09-17T17:39:04.660 回答
1

两者viewreshape在这里工作:

t = torch.tensor([[[1,2,3],
         [4,5,6],
         [7,8,9]],

        [[1,2,3],
         [1,2,3],
         [1,2,3]]])
print(t.size())
t = t.permute(1,2,0).reshape(-1,1)
print(t)
print(t.size())
于 2019-09-17T16:47:36.380 回答